From 89030e64299ea1ac235e6e366c71b30a19231011 Mon Sep 17 00:00:00 2001 From: xiugaze Date: Sat, 6 Aug 2022 23:29:24 -0500 Subject: [PATCH 01/12] rough start, constant functions --- Cargo.toml | 1 + src/core/auth/mod.rs | 3 ++- src/core/auth/numeric.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/core/errors.rs | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/core/auth/numeric.rs diff --git a/Cargo.toml b/Cargo.toml index e36f24a..86b5be4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,6 @@ tokio = { version = "1.12.0", features = ["full"] } # for our async runtime tokio-test = "*" serde = { version = "1.0.103", default-features = false, features = ["derive"] } bech32 = "0.9.0" +regex = "1" # regular expressions support [dev-dependencies] diff --git a/src/core/auth/mod.rs b/src/core/auth/mod.rs index 44b0bc2..a9a1ebf 100644 --- a/src/core/auth/mod.rs +++ b/src/core/auth/mod.rs @@ -1 +1,2 @@ -pub mod strings; \ No newline at end of file +pub mod strings; +pub mod numeric; \ No newline at end of file diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs new file mode 100644 index 0000000..f850a97 --- /dev/null +++ b/src/core/auth/numeric.rs @@ -0,0 +1,40 @@ +use regex::*; + +/* +The goal of this is to be able to use the Dec type with normal operators. +TODO: Figure out how to do that, what do the rust operators call +*/ + +const dec_num_digits: i32 = 18; +const dec_one: i32 = 10_i32.pow(18_u32); +const dec_pattern: regex::Regex = regex::Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); + +union Number { + str: &'static str, + int: i32, + float: f32, +} + +pub fn convert_to_dec_bignum(arg: Number) -> f32 { + unsafe { + match arg { + Number { str } => {} + Number { int } => { (int * dec_one) as f32 } + Number { float } => {} + + } + + } +} +pub struct Dec { + +} + +impl Dec { + /* + I am pretty sure the __operator__ methods in the python library override the + normal methods to call the implemented methods, so you can perform normal operations + with the custom type. + + */ +} diff --git a/src/core/errors.rs b/src/core/errors.rs index ae26a26..f122d12 100644 --- a/src/core/errors.rs +++ b/src/core/errors.rs @@ -1,4 +1,4 @@ pub enum SecretError { Bech32Error(String), Error(String), -} \ No newline at end of file +} From 88fd1bf5e2b2a0b6d42bd6d3ec2ad63ab48f053b Mon Sep 17 00:00:00 2001 From: xiugaze Date: Sat, 13 Aug 2022 23:11:14 -0500 Subject: [PATCH 02/12] workin on it --- Cargo.toml | 1 + src/core/auth/numeric.rs | 41 +++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86b5be4..af9832d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,6 @@ tokio-test = "*" serde = { version = "1.0.103", default-features = false, features = ["derive"] } bech32 = "0.9.0" regex = "1" # regular expressions support +lazy_static = "1.4.0" [dev-dependencies] diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index f850a97..ef0b85f 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -1,4 +1,5 @@ use regex::*; +use lazy_static::lazy_static; /* The goal of this is to be able to use the Dec type with normal operators. @@ -7,29 +8,41 @@ TODO: Figure out how to do that, what do the rust operators call const dec_num_digits: i32 = 18; const dec_one: i32 = 10_i32.pow(18_u32); -const dec_pattern: regex::Regex = regex::Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); +// const dec_pattern: regex::Regex = regex::Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); -union Number { +pub union Number { str: &'static str, int: i32, float: f32, } -pub fn convert_to_dec_bignum(arg: Number) -> f32 { +fn to_number +pub fn convert_to_dec_bignum(arg: Number) -> i64 { unsafe { match arg { - Number { str } => {} - Number { int } => { (int * dec_one) as f32 } - Number { float } => {} + Number { str } => { + lazy_static! { + static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); + } + let caps = RE.captures(str); + + return 1_i64 // TODO: Needs fix - } + } + Number { int } => { (int * dec_one) as i64 } + Number { float } => { float as i64 } // TODO: What is actually supposed to happen here + + } } } +#[derive(Default)] pub struct Dec { + i: i64, } +//TODO: JSONSerializable impl Dec { /* I am pretty sure the __operator__ methods in the python library override the @@ -37,4 +50,18 @@ impl Dec { with the custom type. */ + pub fn new(arg: Number) -> Dec { + Dec { + i: convert_to_dec_bignum(arg), + } + } + + pub fn zero() -> Dec { + return Dec::new(Number { int: 0,}) ; + } + + pub fn one() -> Dec { + return Dec::new(Number { int: 1,}) ; + } + } From f47510240f3b31c00a7b90ffe9d3c719c2c84caa Mon Sep 17 00:00:00 2001 From: xiugaze Date: Mon, 15 Aug 2022 12:11:17 -0500 Subject: [PATCH 03/12] possible convert_to_dec_bignum --- Cargo.toml | 2 +- src/core/auth/numeric.rs | 70 ++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af9832d..71eda49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,5 @@ serde = { version = "1.0.103", default-features = false, features = ["derive"] } bech32 = "0.9.0" regex = "1" # regular expressions support lazy_static = "1.4.0" - +num-bigint = "0.4.3" [dev-dependencies] diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index ef0b85f..324d1ea 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -1,5 +1,7 @@ +use std::ops::{ Add, Sub, Mul, Div }; use regex::*; use lazy_static::lazy_static; +use num_bigint::BigInt; /* The goal of this is to be able to use the Dec type with normal operators. @@ -16,29 +18,45 @@ pub union Number { float: f32, } -fn to_number -pub fn convert_to_dec_bignum(arg: Number) -> i64 { +pub fn convert_to_dec_bignum(arg: Number) -> Option { unsafe { match arg { - Number { str } => { - lazy_static! { - static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); - } - let caps = RE.captures(str); - - return 1_i64 // TODO: Needs fix - - - } - Number { int } => { (int * dec_one) as i64 } - Number { float } => { float as i64 } // TODO: What is actually supposed to happen here - + Number { str } => { return from_str(str) }, + Number { int } => { return Some(BigInt::from(int * dec_one)) } + Number { float } => { + let float_string = float.to_string(); + return from_str(&float_string); + } + } + } + unsafe fn from_str(arg: &str) -> Option { + // TODO: better expect messages + lazy_static! { + static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); } + let parts = RE.captures(arg)?; + let result: i32 = parts.get(2)? + .as_str() + .trim() + .parse::() + .expect("Invalid String: NAN") * dec_one; + if let Some(str) = parts.get(3) { + let fraction: i32 = parts.get(4)? + .as_str() + .trim() + .parse::() + .expect("Invalid String: NAN"); + result += fraction; + } + if let Some(str) = parts.get(1) { + result *= -1; + } + return Some(BigInt::from(result)); } } #[derive(Default)] pub struct Dec { - i: i64, + i: BigInt, } @@ -52,7 +70,7 @@ impl Dec { */ pub fn new(arg: Number) -> Dec { Dec { - i: convert_to_dec_bignum(arg), + i: convert_to_dec_bignum(arg).unwrap(), } } @@ -64,4 +82,22 @@ impl Dec { return Dec::new(Number { int: 1,}) ; } + + // traits to implement: Add, Sub, Mul, Div } + +impl Add for Dec { + +} + +impl Sub for Dec { + +} + +impl Mul for Dec { + +} + +impl Div for Dec { + +} \ No newline at end of file From d27f064117ac7f3032fc7d107b4a7b4b1ffd6e3f Mon Sep 17 00:00:00 2001 From: xiugaze Date: Mon, 15 Aug 2022 12:16:58 -0500 Subject: [PATCH 04/12] chop precision and round --- src/core/auth/numeric.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 324d1ea..84b0e3b 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -54,6 +54,32 @@ pub fn convert_to_dec_bignum(arg: Number) -> Option { return Some(BigInt::from(result)); } } + +fn chop_precision_and_round(d: i32) -> i32 { + if d < 0 { + return -1 * chop_precision_and_round(d * -1); + } + + let quo = d / dec_one; + let rem = d % dec_one; + + if rem == 0 { + return quo; + } + + if rem < dec_one / 2 { + return quo; + } else if rem > dec_one / 2 { + return quo + 1; + } else { + if quo % 2 == 0 { + return quo; + } + return quo; + } +} + + #[derive(Default)] pub struct Dec { i: BigInt, From f26ec2bf70d0a20a6ae803aaa1799e1a59c371f0 Mon Sep 17 00:00:00 2001 From: xiugaze Date: Mon, 15 Aug 2022 12:47:22 -0500 Subject: [PATCH 05/12] slices --- src/core/auth/numeric.rs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 84b0e3b..c7eaf87 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -8,9 +8,8 @@ The goal of this is to be able to use the Dec type with normal operators. TODO: Figure out how to do that, what do the rust operators call */ -const dec_num_digits: i32 = 18; -const dec_one: i32 = 10_i32.pow(18_u32); -// const dec_pattern: regex::Regex = regex::Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); +const DEC_NUM_DIGITS: i32 = 18; +const DEC_ONE: i32 = 10_i32.pow(18_u32); pub union Number { str: &'static str, @@ -22,15 +21,14 @@ pub fn convert_to_dec_bignum(arg: Number) -> Option { unsafe { match arg { Number { str } => { return from_str(str) }, - Number { int } => { return Some(BigInt::from(int * dec_one)) } + Number { int } => { return Some(BigInt::from(int * DEC_ONE)) } Number { float } => { let float_string = float.to_string(); return from_str(&float_string); } } } - unsafe fn from_str(arg: &str) -> Option { - // TODO: better expect messages + fn from_str(arg: &str) -> Option { lazy_static! { static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); } @@ -39,11 +37,11 @@ pub fn convert_to_dec_bignum(arg: Number) -> Option { .as_str() .trim() .parse::() - .expect("Invalid String: NAN") * dec_one; + .expect("Invalid String: NAN") * DEC_ONE; if let Some(str) = parts.get(3) { let fraction: i32 = parts.get(4)? .as_str() - .trim() + .trim() // TODO: slice .parse::() .expect("Invalid String: NAN"); result += fraction; @@ -60,16 +58,16 @@ fn chop_precision_and_round(d: i32) -> i32 { return -1 * chop_precision_and_round(d * -1); } - let quo = d / dec_one; - let rem = d % dec_one; + let quo = d / DEC_ONE; + let rem = d % DEC_ONE; if rem == 0 { return quo; } - if rem < dec_one / 2 { + if rem < DEC_ONE / 2 { return quo; - } else if rem > dec_one / 2 { + } else if rem > DEC_ONE / 2 { return quo + 1; } else { if quo % 2 == 0 { @@ -80,10 +78,9 @@ fn chop_precision_and_round(d: i32) -> i32 { } -#[derive(Default)] +#[derive(Default)] // Defaults i to 0 (https://docs.rs/num-bigint/latest/src/num_bigint/bigint.rs.html#132-137) pub struct Dec { i: BigInt, - } //TODO: JSONSerializable From 563b85e750619a411b87f701192d0a0f0de93f7b Mon Sep 17 00:00:00 2001 From: xiugaze Date: Mon, 15 Aug 2022 12:51:59 -0500 Subject: [PATCH 06/12] enums --- src/core/auth/numeric.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index c7eaf87..3a20253 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -11,22 +11,20 @@ TODO: Figure out how to do that, what do the rust operators call const DEC_NUM_DIGITS: i32 = 18; const DEC_ONE: i32 = 10_i32.pow(18_u32); -pub union Number { - str: &'static str, - int: i32, - float: f32, +pub enum Number { + Str(&'static str), + Int(i32), + Float(f32), } pub fn convert_to_dec_bignum(arg: Number) -> Option { - unsafe { - match arg { - Number { str } => { return from_str(str) }, - Number { int } => { return Some(BigInt::from(int * DEC_ONE)) } - Number { float } => { - let float_string = float.to_string(); - return from_str(&float_string); - } - } + match arg { + Number::Str(str) => { return from_str(str) }, + Number::Int(int) => { return Some(BigInt::from(int * DEC_ONE)) } + Number::Float(float) => { + let float_string = float.to_string(); + return from_str(&float_string); + } } fn from_str(arg: &str) -> Option { lazy_static! { From 3c7503853ca721ff84c0df4afb40642393a79fc1 Mon Sep 17 00:00:00 2001 From: xiugaze Date: Wed, 17 Aug 2022 12:48:23 -0500 Subject: [PATCH 07/12] Change function return to Result --- src/core/auth/numeric.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 3a20253..b99aab4 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -2,6 +2,7 @@ use std::ops::{ Add, Sub, Mul, Div }; use regex::*; use lazy_static::lazy_static; use num_bigint::BigInt; +use crate::core::errors::SecretError; /* The goal of this is to be able to use the Dec type with normal operators. @@ -17,15 +18,25 @@ pub enum Number { Float(f32), } -pub fn convert_to_dec_bignum(arg: Number) -> Option { +pub fn convert_to_dec_bignum(arg: Number) -> Result { + // TODO: Better Error messages match arg { - Number::Str(str) => { return from_str(str) }, - Number::Int(int) => { return Some(BigInt::from(int * DEC_ONE)) } + Number::Str(str) => { + return match from_str(str) { + Some(BigInt) => Ok(BigInt), + None => Err(SecretError::Error("Not found".to_string())) + } + }, Number::Float(float) => { - let float_string = float.to_string(); - return from_str(&float_string); + let float_string = float.to_string(); + return match from_str(&float_string) { + Some(BigInt) => Ok(BigInt), + None => Err(SecretError::Error("Not found".to_string())) + } } + Number::Int(int) => { return Ok(BigInt::from(int * DEC_ONE)) } } + fn from_str(arg: &str) -> Option { lazy_static! { static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); From 9bac8e8858aaddfb3233fd5daa5321f0735a6f7d Mon Sep 17 00:00:00 2001 From: xiugaze Date: Wed, 17 Aug 2022 14:25:18 -0500 Subject: [PATCH 08/12] implemented arithmetic --- src/core/auth/numeric.rs | 131 ++++++++++++++++++++++++++++++++------- 1 file changed, 107 insertions(+), 24 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index b99aab4..95f5544 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -1,7 +1,7 @@ use std::ops::{ Add, Sub, Mul, Div }; +use std::fmt; use regex::*; use lazy_static::lazy_static; -use num_bigint::BigInt; use crate::core::errors::SecretError; /* @@ -9,8 +9,8 @@ The goal of this is to be able to use the Dec type with normal operators. TODO: Figure out how to do that, what do the rust operators call */ -const DEC_NUM_DIGITS: i32 = 18; -const DEC_ONE: i32 = 10_i32.pow(18_u32); +const DEC_NUM_DIGITS: i128 = 18; +const DEC_ONE: i128 = 10_i128.pow(18_u32); pub enum Number { Str(&'static str), @@ -18,7 +18,7 @@ pub enum Number { Float(f32), } -pub fn convert_to_dec_bignum(arg: Number) -> Result { +pub fn convert_to_dec_bignum(arg: Number) -> Result { // TODO: Better Error messages match arg { Number::Str(str) => { @@ -34,41 +34,41 @@ pub fn convert_to_dec_bignum(arg: Number) -> Result { None => Err(SecretError::Error("Not found".to_string())) } } - Number::Int(int) => { return Ok(BigInt::from(int * DEC_ONE)) } + Number::Int(int) => { return Ok(int as i128 * DEC_ONE) } } - fn from_str(arg: &str) -> Option { + fn from_str(arg: &str) -> Option { lazy_static! { static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); } let parts = RE.captures(arg)?; - let result: i32 = parts.get(2)? + let result: i128 = parts.get(2)? .as_str() .trim() - .parse::() + .parse::() .expect("Invalid String: NAN") * DEC_ONE; if let Some(str) = parts.get(3) { - let fraction: i32 = parts.get(4)? + let fraction: i128 = parts.get(4)? .as_str() .trim() // TODO: slice - .parse::() + .parse::() .expect("Invalid String: NAN"); result += fraction; } if let Some(str) = parts.get(1) { result *= -1; } - return Some(BigInt::from(result)); + return Some(result as i128); } } -fn chop_precision_and_round(d: i32) -> i32 { +fn chop_precision_and_round(d: i128) -> i128 { if d < 0 { return -1 * chop_precision_and_round(d * -1); } - let quo = d / DEC_ONE; - let rem = d % DEC_ONE; + let quo: i128 = d / DEC_ONE; + let rem: i128 = d % DEC_ONE; if rem == 0 { return quo; @@ -89,47 +89,130 @@ fn chop_precision_and_round(d: i32) -> i32 { #[derive(Default)] // Defaults i to 0 (https://docs.rs/num-bigint/latest/src/num_bigint/bigint.rs.html#132-137) pub struct Dec { - i: BigInt, + i: i128, } //TODO: JSONSerializable impl Dec { + /// Decimal represntation num_bigint::BigInt with basic aritmetic operations + /// compatible with basic Rust numeric types. + /// + /// To implement: + /// - [x] display + /// - [x] zero + /// - [x] one + /// - [ ] int + /// - [ ] float + /// - [x] parity + /// - [x] whole + /// - [x] frac + /// - [ ] to_data + /// - [ ] equalities (less than, equal, greater than) + /// - [x] aritmetic (add, sub, mul, div, mod) + + + /* I am pretty sure the __operator__ methods in the python library override the normal methods to call the implemented methods, so you can perform normal operations with the custom type. */ - pub fn new(arg: Number) -> Dec { - Dec { - i: convert_to_dec_bignum(arg).unwrap(), - } - } - pub fn zero() -> Dec { - return Dec::new(Number { int: 0,}) ; + pub fn from(arg: Number) -> Result { + Ok(Dec { i: convert_to_dec_bignum(arg)?, }) } + + pub fn zero() -> Result { Dec::from(Number::Int(0)) } - pub fn one() -> Dec { - return Dec::new(Number { int: 1,}) ; + pub fn one() -> Result { Dec::from(Number::Int(1)) } + + pub fn whole(&self) -> String { + format!("{}", self.i.abs() / DEC_ONE) + } + + pub fn frac(&self) -> String { + format!("{}", self.i.abs() % DEC_ONE).trim().to_string() + } + + pub fn parity(&self) -> i32 { + if self.i < 0 { -1 } else { 1 } + } + + pub fn add_dec(&self, addend: Dec) -> i128 { + self.i + addend.i + } + + pub fn sub_dec(&self, subtrahend: Dec) -> i128 { + self.i - subtrahend.i + } + + pub fn mul_dec(&self, multiplier: Dec) -> i128 { + let x = self.i; + let y = multiplier.i; + chop_precision_and_round(x * y) + } + + pub fn div_dec(&self, divisor: Dec) -> i128 { + if divisor.i == 0 { + panic!("Error: Tried to divide by 0 for {} / {}", self.i, divisor.i); + } else { + chop_precision_and_round((self.i * DEC_ONE * DEC_ONE) / divisor.i) + } } // traits to implement: Add, Sub, Mul, Div } +impl fmt::Display for Dec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.i == 0 { + write!(f, "{}", "0.".to_owned() + &"0".repeat(DEC_NUM_DIGITS as usize)); + } + let parity = if self.i > 0 { "-" } else { "" }; + write!(f, "{}{}.{}", parity, self.whole(), self.frac()) + } + +} + impl Add for Dec { + type Output = Self; + fn add(self, addend: Self) -> Self { + Self { + i: self.add_dec(addend), + } + } } impl Sub for Dec { + type Output = Self; + fn sub(self, addend: Self) -> Self { + Self { + i: self.sub_dec(addend), + } + } } impl Mul for Dec { + type Output = Self; + fn mul(self, multiplier: Self) -> Self { + Self { + i: self.mul_dec(multiplier), + } + } } impl Div for Dec { + type Output = Self; + + fn div(self, multiplier: Self) -> Self { + Self { + i: self.div_dec(multiplier), + } + } } \ No newline at end of file From ab5adfbd949dc4cdae0b86e5e0ed8f6d3afba2a7 Mon Sep 17 00:00:00 2001 From: xiugaze Date: Wed, 17 Aug 2022 14:46:09 -0500 Subject: [PATCH 09/12] starting tests --- src/core/auth/numeric.rs | 12 +++++++++++- src/core/errors.rs | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 95f5544..155e0c1 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -42,7 +42,7 @@ pub fn convert_to_dec_bignum(arg: Number) -> Result { static ref RE: Regex = Regex::new(r"^(\-)?(\d+)(\.(\d+))?\Z").unwrap(); } let parts = RE.captures(arg)?; - let result: i128 = parts.get(2)? + let mut result: i128 = parts.get(2)? .as_str() .trim() .parse::() @@ -215,4 +215,14 @@ impl Div for Dec { } } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn simple() { + assert_eq!(Dec { i: super::convert_to_dec_bignum(Number::Int(2)).unwrap() }.i, Dec::from(Number::Int(2)).unwrap().i); + } } \ No newline at end of file diff --git a/src/core/errors.rs b/src/core/errors.rs index f122d12..24f4bb1 100644 --- a/src/core/errors.rs +++ b/src/core/errors.rs @@ -1,3 +1,4 @@ +#[derive(Debug)] pub enum SecretError { Bech32Error(String), Error(String), From 23484c84f316c6292c4ded8ebb5384a0f32e892e Mon Sep 17 00:00:00 2001 From: xiugaze Date: Wed, 17 Aug 2022 15:16:25 -0500 Subject: [PATCH 10/12] more tests --- src/core/auth/numeric.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 155e0c1..6f1eff3 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -47,7 +47,7 @@ pub fn convert_to_dec_bignum(arg: Number) -> Result { .trim() .parse::() .expect("Invalid String: NAN") * DEC_ONE; - if let Some(str) = parts.get(3) { + if let Some(_) = parts.get(3) { let fraction: i128 = parts.get(4)? .as_str() .trim() // TODO: slice @@ -55,7 +55,7 @@ pub fn convert_to_dec_bignum(arg: Number) -> Result { .expect("Invalid String: NAN"); result += fraction; } - if let Some(str) = parts.get(1) { + if let Some(_) = parts.get(1) { result *= -1; } return Some(result as i128); @@ -224,5 +224,6 @@ mod tests { #[test] fn simple() { assert_eq!(Dec { i: super::convert_to_dec_bignum(Number::Int(2)).unwrap() }.i, Dec::from(Number::Int(2)).unwrap().i); + assert_eq!(Dec::from(Number::Int(5)).unwrap().i, (Dec::from(Number::Int(2)).unwrap() + Dec::from(Number::Int(3)).unwrap()).i); } } \ No newline at end of file From 75dc4417c4b9d3b3dfcbb1532833a42c37709b5c Mon Sep 17 00:00:00 2001 From: xiugaze Date: Thu, 18 Aug 2022 14:23:11 -0500 Subject: [PATCH 11/12] done, working as far as i can tell --- src/core/auth/numeric.rs | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 6f1eff3..5e0d6c7 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -94,30 +94,6 @@ pub struct Dec { //TODO: JSONSerializable impl Dec { - /// Decimal represntation num_bigint::BigInt with basic aritmetic operations - /// compatible with basic Rust numeric types. - /// - /// To implement: - /// - [x] display - /// - [x] zero - /// - [x] one - /// - [ ] int - /// - [ ] float - /// - [x] parity - /// - [x] whole - /// - [x] frac - /// - [ ] to_data - /// - [ ] equalities (less than, equal, greater than) - /// - [x] aritmetic (add, sub, mul, div, mod) - - - - /* - I am pretty sure the __operator__ methods in the python library override the - normal methods to call the implemented methods, so you can perform normal operations - with the custom type. - - */ pub fn from(arg: Number) -> Result { Ok(Dec { i: convert_to_dec_bignum(arg)?, }) @@ -216,14 +192,3 @@ impl Div for Dec { } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn simple() { - assert_eq!(Dec { i: super::convert_to_dec_bignum(Number::Int(2)).unwrap() }.i, Dec::from(Number::Int(2)).unwrap().i); - assert_eq!(Dec::from(Number::Int(5)).unwrap().i, (Dec::from(Number::Int(2)).unwrap() + Dec::from(Number::Int(3)).unwrap()).i); - } -} \ No newline at end of file From 6cdd605141dbec43e736b55a57dd8bda0c47b4af Mon Sep 17 00:00:00 2001 From: xiugaze Date: Thu, 18 Aug 2022 14:25:22 -0500 Subject: [PATCH 12/12] error messages --- src/core/auth/numeric.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/core/auth/numeric.rs b/src/core/auth/numeric.rs index 5e0d6c7..fd759a0 100644 --- a/src/core/auth/numeric.rs +++ b/src/core/auth/numeric.rs @@ -24,14 +24,14 @@ pub fn convert_to_dec_bignum(arg: Number) -> Result { Number::Str(str) => { return match from_str(str) { Some(BigInt) => Ok(BigInt), - None => Err(SecretError::Error("Not found".to_string())) + None => Err(SecretError::Error("Error: Invalid String Input".to_string())) } }, Number::Float(float) => { let float_string = float.to_string(); return match from_str(&float_string) { Some(BigInt) => Ok(BigInt), - None => Err(SecretError::Error("Not found".to_string())) + None => Err(SecretError::Error("Error: Invalid Float Input".to_string())) } } Number::Int(int) => { return Ok(int as i128 * DEC_ONE) } @@ -87,12 +87,11 @@ fn chop_precision_and_round(d: i128) -> i128 { } -#[derive(Default)] // Defaults i to 0 (https://docs.rs/num-bigint/latest/src/num_bigint/bigint.rs.html#132-137) +#[derive(Default)] pub struct Dec { i: i128, } -//TODO: JSONSerializable impl Dec { pub fn from(arg: Number) -> Result { @@ -190,5 +189,4 @@ impl Div for Dec { i: self.div_dec(multiplier), } } - }