Expand description
§qubit-measure
Persistent typed Decimal measurement values with explicit units.
The default build contains the exact Decimal core only. Enable the
default-off uom Cargo feature to expose approximate uom/f64 adapters.
Built-in exact conversion definitions share crate-wide constants grouped by
quantity.
§Examples
Parse a measurement, derive a rounded conversion, and persist the original value with its unit. The converted measurement is a derived value; JSON serialization retains the original value and unit, including the Decimal scale supplied by the input.
use qubit_measure::{ConversionOptions, measurement, unit};
use rust_decimal::RoundingStrategy;
use serde_json::json;
let original = measurement::Length::parse_strict("50.0 cm")?;
let meters = original.convert_to_with_options(
unit::Length::Meter,
ConversionOptions::fixed_scale(4, RoundingStrategy::MidpointNearestEven)?,
)?;
assert_eq!(meters.to_string(), "0.5000 m");
let stored = serde_json::to_value(original)?;
assert_eq!(stored, json!({"quantity":"length","value":"50.0","unit":"cm"}));
let restored: measurement::Length = serde_json::from_value(stored)?;
assert_eq!(restored.value, original.value);
assert_eq!(restored.unit, unit::Length::Centimeter);
assert_eq!(restored.value.scale(), 1);
assert_eq!(restored.to_string(), "50.0 cm");§Further reading
Explore Measurement, ConversionOptions, Unit, and
measurement_text for the core value model, conversion policies, unit
definitions, and text parsing and formatting APIs. The English and Chinese
guides are available in the README
and 中文 README.
Modules§
- measurement
- Persisted measurement aliases for supported quantity families.
- measurement_
text - Opt-in Serde adapter for canonical compact measurement text.
- unit
- Unit families supported by persisted measurements.
Macros§
- define_
unit_ family - Defines an externally extensible unit family with exact Decimal factors.
- impl_
uom_ unit - Implements an explicit approximate
uombridge for a unit family.
Structs§
- Conversion
Factor - A positive unit coefficient represented as reduced Decimal ratio terms.
- Conversion
Options - Controls the final scale and rounding applied to a converted value.
- Measurement
- A persisted measurement value for one concrete quantity.
- Measurement
Parse Options - Options controlling resource limits for measurement text parsing.
- Unit
Definition - Defines a unit relative to its quantity family’s base unit.
Enums§
- Measurement
Error - Errors returned by parsing, converting, or adapting measurements.
Traits§
- Unit
- Exact metadata and Decimal conversion definition for one unit family.
- UomUnit
- A unit family that can bridge through an approximate
uom/f64quantity.
Functions§
- assert_
unit_ family_ valid - Asserts the observable metadata contract of a
Unitimplementation.