Files
FiscusPublic/crates/fiscus-capture/src/amount.rs
T

27 lines
1014 B
Rust

//! An amount as a phone writes it: `129`, `129,50`, `1 234,50 Kč`, `CZK 129.00`. Read from its text with
//! `Haler::parse_czk`, never through a float.
use fiscus_ledger::Haler;
/// The largest single card payment Fiscus believes: 10 000 000 CZK.
pub const MAX: Haler = Haler(1_000_000_000);
/// A positive amount in CZK, with an optional `Kč` or `CZK` before or after it. A no-break space (U+00A0) or
/// narrow no-break space (U+202F) between thousands counts as a space.
pub fn parse_positive(text: &str) -> Option<Haler> {
let t: String = text.chars().map(|c| if c == '\u{a0}' || c == '\u{202f}' { ' ' } else { c }).collect();
let mut t = t.trim();
for unit in ["Kč", "CZK"] {
if let Some(rest) = t.strip_suffix(unit) {
t = rest.trim_end();
break;
}
if let Some(rest) = t.strip_prefix(unit) {
t = rest.trim_start();
break;
}
}
let h = Haler::parse_czk(t).ok()?;
(h.0 > 0 && h <= MAX).then_some(h)
}