use crate::plugins::{integer_queries, units::Unit};
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{One, Signed, ToPrimitive};
const MAX_BITS: u64 = 4_096;
#[derive(Clone)]
pub(super) struct Exact {
pub(super) amount: BigRational,
pub(super) currency: Option<String>,
}
pub(super) fn bounded(amount: BigRational, currency: Option<String>) -> Option<Exact> {
(amount.numer().bits() <= MAX_BITS && amount.denom().bits() <= MAX_BITS)
.then_some(Exact { amount, currency })
}
pub(super) fn decimal(input: &str) -> Option<BigRational> {
let input = input.trim().replace(',', "");
if input.len() > 256 {
return None;
}
let (numerator, denominator) = integer_queries::ratio_value(&input).ok()?;
bounded(BigRational::new(numerator, denominator), None).map(|value| value.amount)
}
pub(super) fn linear_si_scale(unit: &Unit) -> Option<BigRational> {
if unit.offset != 0.0 {
return None;
}
if unit.scale == 1.0 {
return bounded(BigRational::from_integer(1.into()), None).map(|v| v.amount);
}
let length = [1, 0, 0, 0, 0, 0, 0, 0];
let mass = [0, 1, 0, 0, 0, 0, 0, 0];
let time = [0, 0, 1, 0, 0, 0, 0, 0];
let volume = [3, 0, 0, 0, 0, 0, 0, 0];
let (power, dimension): (i32, [i8; 8]) = match unit.symbol.as_str() {
"km" => (3, length),
"cm" => (-2, length),
"mm" => (-3, length),
"μm" => (-6, length),
"nm" => (-9, length),
"g" => (-3, mass),
"mg" => (-6, mass),
"μg" => (-9, mass),
"ng" => (-12, mass),
"t" => (3, mass),
"ms" => (-3, time),
"µs" => (-6, time),
"L" => (-3, volume),
"mL" => (-6, volume),
"µL" => (-9, volume),
"nL" => (-12, volume),
_ => return None,
};
if unit.dimension != dimension {
return None;
}
let power_of_ten = BigInt::from(10).pow(power.unsigned_abs());
let factor = if power >= 0 {
BigRational::from_integer(power_of_ten)
} else {
BigRational::new(1.into(), power_of_ten)
};
(factor.to_f64()? == unit.scale).then_some(factor)
}
pub(super) fn combine(left: Option<Exact>, right: Option<Exact>, operator: char) -> Option<Exact> {
let (left, right) = (left?, right?);
let currency = match (left.currency, right.currency) {
(Some(a), Some(b)) if a != b => return None,
(Some(a), _) | (_, Some(a)) => Some(a),
_ => None,
};
let amount = match operator {
'+' => left.amount + right.amount,
'-' => left.amount - right.amount,
'*' => left.amount * right.amount,
'/' if right.amount != BigRational::from_integer(0.into()) => left.amount / right.amount,
_ => return None,
};
bounded(amount, currency)
}
pub(super) fn ordinary_money(amount: &BigRational, currency: &str) -> Option<f64> {
if matches!(currency, "BTC" | "JPY") {
return None;
}
let small = BigRational::new(1.into(), 100.into());
let large = BigRational::from_integer(BigInt::from(1_000_000_000_000u64));
if amount.abs() < small || amount.abs() >= large {
return None;
}
let cents = amount.abs() * BigRational::from_integer(100.into());
let mut whole = cents.numer() / cents.denom();
let remainder = cents.numer() % cents.denom();
if remainder * 2 >= cents.denom().clone() {
whole += BigInt::one();
}
let whole = if amount.is_negative() { -whole } else { whole };
let rounded = whole.to_f64()? / 100.0;
(rounded.abs() < 1e12).then_some(rounded)
}