util/parse: fixed errors caused by merge

This commit is contained in:
lunar-mining
2021-09-30 23:28:18 +02:00
parent f2049059e5
commit ceff4e9e03

View File

@@ -102,165 +102,6 @@ pub fn symbol_to_id(token: &str, tokenlist: TokenList) -> Result<String> {
}
}
pub fn decode_base10(amount: &str, decimals: usize) -> Result<u64> {
const RADIX: u32 = 10;
let mut input_str = amount.to_string();
// remove the decimal point
let mut amount: String = match input_str.find(".") {
Some(v) => {
input_str.remove(v);
input_str
}
None => input_str,
};
// only digits should remain:
for c in amount.chars() {
if c.is_digit(RADIX) == false {
// TODO: Make this an error
println!("Amount is not valid digits!")
}
}
// add digits to the end if there are too few
if amount.len() < decimals {
loop {
amount.push('0');
if amount.len() == decimals {
break;
}
continue;
}
}
// remove digits from the end if there are too many
if amount.len() > decimals {
loop {
amount.pop();
if amount.len() == decimals {
break;
}
continue;
}
}
println!("Resized amount: {}", amount);
// convert to an integer
let number = amount.parse::<u64>().unwrap();
Ok(number)
}
// TODO: implement this
//fn encode_base10() {
// let input = 100000000;
// println!("Original input: {}", input);
// let mut input_str = input.to_string();
//
// input_str.insert(1, '.');
//
// let amount = input_str.trim_end_matches('0');
//
// let amount = if amount.ends_with('.') == true {
// let amount = amount.trim_end_matches('.');
// amount
// } else {
// amount
// };
//
// println!("Encoded output: {}", amount);
//}
mod tests {
#[test]
fn decode_base10() {
const RADIX: u32 = 10;
// TODO: this number varies per token
let decimal_places = 10;
let input = "2.5";
println!("Initial input: {}", input);
let mut input_str = input.to_string();
// remove the decimal point
let mut amount: String = match input_str.find(".") {
Some(v) => {
input_str.remove(v);
input_str
}
None => {
print!("Number isn't a float");
input_str
}
};
println!("Removed decimal point: {}", amount);
// only digits should remain:
for c in amount.chars() {
if c.is_digit(RADIX) == false {
println!("Amount is not valid digits!")
}
}
// add digits to the end if there are too few
if amount.len() < decimal_places {
loop {
amount.push('0');
if amount.len() == decimal_places {
break;
}
continue;
}
}
// remove digits from the end if there are too many
if amount.len() > decimal_places {
loop {
amount.pop();
if amount.len() == decimal_places {
break;
}
continue;
}
}
println!("Resized amount: {}", amount);
// convert to an integer
let number = amount.parse::<u64>().unwrap();
println!("The final number: {:?}", number);
}
#[test]
fn encode_base10() {
let input = 100000000;
println!("Original input: {}", input);
let mut input_str = input.to_string();
input_str.insert(1, '.');
let amount = input_str.trim_end_matches('0');
let amount = if amount.ends_with('.') == true {
let amount = amount.trim_end_matches('.');
amount
} else {
amount
};
println!("Encoded output: {}", amount);
=======
fn is_digit(c: char) -> bool {
('0'..='9').contains(&c)
}
@@ -354,6 +195,5 @@ mod tests {
assert_eq!("234321.1", &encode_base10(2343211, 1));
assert_eq!("2343211", &encode_base10(2343211, 0));
assert_eq!("0.00002343", &encode_base10(2343, 8));
>>>>>>> util-refactor
}
}