Skip to main content

parse

Function parse 

Source
pub fn parse(expr: &str) -> Result<UnitExpr, UcumError>
Expand description

Parse a UCUM expression into an abstract syntax tree (case-sensitive).

This is a total function: it never panics and never hangs. Syntactically malformed input yields a UcumError::Parse carrying the byte offset. Atoms are not checked for existence here; use validate or analyze for that.

use ucum::UcumError;
assert!(ucum::parse("kg.m/s2").is_ok());
assert!(ucum::parse("/s").is_ok());        // leading-slash reciprocal
assert!(ucum::parse("(m/s)").is_ok());
// A malformed expression reports *where* it went wrong.
assert!(matches!(ucum::parse("m/"), Err(UcumError::Parse { pos: 2, .. })));