| Crates.io | octofhir-ucum-core |
| lib.rs | octofhir-ucum-core |
| version | 0.4.0 |
| created_at | 2025-07-20 21:29:44.257081+00 |
| updated_at | 2025-07-28 13:33:39.805222+00 |
| description | UCUM (Unified Code for Units of Measure) core library for FHIRPath quantity operations |
| homepage | https://github.com/octofhir/ucum-rs |
| repository | https://github.com/octofhir/ucum-rs |
| max_upload_size | |
| id | 1761419 |
| size | 432,984 |
UCUM (Unified Code for Units of Measure) core library for FHIRPath quantity operations, written in Rust (2024 edition).
no_std optional)Add to your Cargo.toml:
[dependencies]
octofhir-ucum-core = "0.2.0"
The core library provides functions for parsing, evaluating, and converting UCUM expressions. For detailed code examples, please refer to the UCUM-RS User Guide.
Use parse_expression to parse a UCUM expression into an abstract syntax tree (AST). This function takes a string representation of a UCUM expression and returns a Result<UnitExpr, String>.
Use evaluate to evaluate a parsed expression and determine its canonical form, conversion factor, and dimensions. This function takes a reference to a UnitExpr and returns a Result<EvaluationResult, String>.
Use find_unit to look up a unit by its code. This function takes a string code and returns an Option<&Unit>.
To convert a value from one unit to another, you need to:
The library can handle complex unit expressions, including:
Temperature units require special handling due to their offsets:
When converting between temperature units, you need to handle the offsets:
Arbitrary units are units that are not defined in terms of any other unit:
Arbitrary units:
The library provides comprehensive error handling for:
parse_expression(expr: &str) -> Result<UnitExpr, UcumError>Parse a UCUM expression into an abstract syntax tree (AST).
evaluate(expr: &UnitExpr) -> Result<EvalResult, UcumError>Evaluate a parsed UCUM expression to determine its canonical form, conversion factor, and dimensions.
analyse(expression: &str) -> Result<UnitAnalysis, UcumError>Perform comprehensive analysis of a UCUM expression, returning detailed information about its properties.
validate(expression: &str) -> Result<bool, UcumError>Validate a UCUM expression for correctness.
validate_in_property(expression: &str, property: &str) -> Result<bool, UcumError>Validate that a unit expression is appropriate for a given physical property.
is_comparable(expr1: &str, expr2: &str) -> Result<bool, UcumError>Check if two unit expressions are commensurable (have the same dimensions).
unit_multiply(left: &str, right: &str) -> Result<String, UcumError>Multiply two unit expressions.
unit_divide(left: &str, right: &str) -> Result<String, UcumError>Divide two unit expressions.
find_unit(code: &str) -> Option<&UnitRecord>Look up a unit by its code.
UnitExprRepresents a parsed UCUM expression.
pub enum UnitExpr {
Simple(String),
Annotated(String, String),
Prefixed(String, Box<UnitExpr>),
Power(Box<UnitExpr>, i32),
Multiply(Box<UnitExpr>, Box<UnitExpr>),
Divide(Box<UnitExpr>, Box<UnitExpr>),
}
EvalResultRepresents the result of evaluating a UCUM expression.
pub struct EvalResult {
pub factor: Number,
pub dim: Dimension,
pub offset: Number,
}
UnitAnalysisRepresents detailed analysis of a UCUM expression.
pub struct UnitAnalysis {
pub expression: String,
pub parsed_ast: UnitExpr,
pub dimension: Dimension,
pub factor: f64,
pub offset: f64,
pub is_dimensionless: bool,
pub has_offset: bool,
}
UnitRecordRepresents a UCUM unit record.
pub struct UnitRecord {
pub code: String,
pub name: String,
pub print_symbol: Option<String>,
pub property: Option<String>,
pub is_metric: bool,
pub is_special: bool,
pub is_arbitrary: bool,
pub class: String,
pub factor: f64,
pub dim: Dimension,
pub offset: f64,
}
DimensionRepresents the physical dimensions of a unit.
pub struct Dimension(pub [i32; 7]);
The seven components represent:
UcumErrorRepresents errors that can occur when working with UCUM expressions.
pub enum UcumError {
ParseError(String),
EvaluationError(String),
IncommensurableUnits(String),
// Other error variants...
}
This library implements the Unified Code for Units of Measure (UCUM) specification, which defines a system for unambiguous representation of units of measure.
The library implements all base units defined in the UCUM specification:
| Dimension | Base Unit | Symbol | UCUM Code |
|---|---|---|---|
| Mass | kilogram | kg | kg |
| Length | meter | m | m |
| Time | second | s | s |
| Electric current | ampere | A | A |
| Temperature | kelvin | K | K |
| Amount of substance | mole | mol | mol |
| Luminous intensity | candela | cd | cd |
The library implements all derived units defined in the UCUM specification, including:
| Unit | Symbol | UCUM Code | Definition |
|---|---|---|---|
| Newton | N | N | kg·m/s² |
| Pascal | Pa | Pa | N/m² |
| Joule | J | J | N·m |
| Watt | W | W | J/s |
| Coulomb | C | C | A·s |
| Volt | V | V | W/A |
| Farad | F | F | C/V |
| Ohm | Ω | Ohm | V/A |
| Siemens | S | S | A/V |
| Weber | Wb | Wb | V·s |
| Tesla | T | T | Wb/m² |
| Henry | H | H | Wb/A |
| Lumen | lm | lm | cd·sr |
| Lux | lx | lx | lm/m² |
| Becquerel | Bq | Bq | 1/s |
| Gray | Gy | Gy | J/kg |
| Sievert | Sv | Sv | J/kg |
| Katal | kat | kat | mol/s |
To add a custom unit, you need to define:
The library provides a registry system for adding custom units. See the UCUM-RS User Guide for detailed examples of how to extend the library with custom units.
MIT OR Apache-2.0