pub struct Number(/* private fields */);Expand description
An OICP JSON number: an exact decimal.
§Why not f64
Every number in OICP is either energy or money: ConsumedEnergy, MeterValueStart,
MeterValueEnd, PricePerReferenceUnit, PricingDefaultPrice. All of them end up on an
invoice between two companies, and OICP’s own CDR rule — ConsumedEnergy is the difference
between MeterValueEnd and MeterValueStart — is an exact decimal identity that binary
floating point cannot honour: 10.1 - 0.1 != 10.0 in f64.
So every OICP number in this crate is a rust_decimal::Decimal. No public field of any wire
type is an f32 or f64, the sync engine has none, and
cargo run -p xtask -- no-floats enforces that in CI.
§The JSON boundary
OICP sends these as JSON numbers, not strings. serde_json represents a fractional JSON
number as an f64 unless its arbitrary_precision feature is enabled — a feature that changes
serde_json::Value globally for every crate in the build, so oicp-kit does not impose it.
The boundary therefore behaves as follows:
- Integral values pass through exactly, as JSON integers.
- Fractional values with at most 15 significant decimal digits — which covers every price and
energy OICP carries, with room to spare — pass through exactly, because the shortest decimal
that round-trips an
f64is the original decimal. - Beyond that, a round-trip rounds to the nearest
f64.Number::json_round_tripssays whether a given value is affected andValidate::validatereports it asViolationCode::Imprecise, so this can never happen silently.
A peer that sends a number as a JSON string ("0.25") is tolerated on input and parsed
exactly; output is always a JSON number.
use oicp_kit::types::Number;
let energy: Number = "10.1".parse().unwrap();
let start: Number = "0.1".parse().unwrap();
// The identity OICP requires of every CDR, exactly.
assert_eq!((energy - start).to_string(), "10.0");
assert_eq!(serde_json::to_string(&energy).unwrap(), "10.1");Implementations§
Source§impl Number
impl Number
Sourcepub fn json_round_trips(self) -> bool
pub fn json_round_trips(self) -> bool
Whether this value survives a JSON round-trip unchanged.
See the type documentation. false only for values with more
significant digits than an f64 can carry.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Whether the value is strictly negative.
Sourcepub fn is_integer(self) -> bool
pub fn is_integer(self) -> bool
Whether the value is a whole number.
OICP’s property tables type several fields as Integer that the OpenAPI sources widened
to number; this is how the wire types check them without refusing the value.
Trait Implementations§
impl Copy for Number
Source§impl<'de> Deserialize<'de> for Number
impl<'de> Deserialize<'de> for Number
Source§fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>
impl Eq for Number
Source§impl JsonSchema for Number
Available on crate feature schema only.
impl JsonSchema for Number
schema only.Source§fn json_schema(_g: &mut SchemaGenerator) -> Schema
fn json_schema(_g: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl Ord for Number
impl Ord for Number
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Number
impl PartialOrd for Number
impl StructuralPartialEq for Number
Auto Trait Implementations§
impl Freeze for Number
impl RefUnwindSafe for Number
impl Send for Number
impl Sync for Number
impl Unpin for Number
impl UnsafeUnpin for Number
impl UnwindSafe for Number
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.