pub enum Numeric {
Integer(i64),
Decimal(Decimal),
Double(f64),
Float(f64),
}Expand description
XPath 2.0 numeric type discriminator carried by Value::Number.
XML Schema / F&O distinguishes four numeric primitives —
xs:integer, xs:decimal, xs:double, xs:float — and the
distinction is observable: instance of must tell an integer from
a double, and the number→string rule (F&O §17.1.2) stringifies
doubles/floats in scientific notation but integers/decimals in
decimal form. Tracking the kind on the value itself lets those
operators answer correctly without a second type carrier.
Decimal is exact (backed by rust_decimal::Decimal — 96-bit
mantissa + scale, ≥28 significant digits), so XPath 2.0’s
0.1 + 0.2 is 0.3, not 0.30000000000000004 (XPath 2.0 §3.1.1
types a literal containing . as xs:decimal, and the spec
demands exact arithmetic on it). Double/Float stay f64-backed.
All four payloads are inline and Copy, so Value::Number stays a
cheap non-allocating variant — a positional predicate’s literal 2
is Numeric::Integer(2) with no heap cost.
Variants§
Implementations§
Source§impl Numeric
impl Numeric
Sourcepub fn as_f64(self) -> f64
pub fn as_f64(self) -> f64
The numeric value as an f64 — the universal accessor every
XPath 1.0-style consumer reads, regardless of the underlying
numeric type. Integer(i) widens to i as f64; Decimal(d)
projects via rust_decimal::prelude::ToPrimitive (the lossy
step — values past 2^53 lose precision, values beyond f64
range collapse to f64::NAN).
Sourcepub fn integer_from_f64(n: f64) -> Numeric
pub fn integer_from_f64(n: f64) -> Numeric
Build an xs:integer from an f64. i64-backed (no arbitrary
precision): a result outside i64 range falls back to an
xs:decimal so the magnitude survives — saturation at
i64::MAX would pin a growing value and hang recursive
big-integer arithmetic. Non-finite values stay as Double
(NaN / ±∞ have no decimal representation per XSD §3.2.3).
Sourcepub fn of_kind(kind: &str, n: f64) -> Numeric
pub fn of_kind(kind: &str, n: f64) -> Numeric
Build a Numeric of the given XSD primitive kind from an f64
— used by arithmetic promotion to tag a result with its
promoted type. An unrecognised kind falls back to Double.
Note: this conversion goes through f64, so decimal
arithmetic that needs to stay exact must NOT route through
here — see [Numeric::decimal_op] for the exact path.