Skip to main content

ucum/
error.rs

1//! The crate error type.
2
3/// Errors produced by parsing, validation, analysis and conversion.
4///
5/// Every public function in this crate is *total*: it returns one of these
6/// errors rather than panicking or looping forever on malformed input.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8#[non_exhaustive]
9pub enum UcumError {
10    /// The input is not syntactically a valid UCUM expression.
11    ///
12    /// `pos` is the byte offset into the input at which the problem was
13    /// detected.
14    #[error("parse error at byte {pos}: {msg}")]
15    Parse {
16        /// Byte offset into the original input.
17        pos: usize,
18        /// Human-readable description of the problem.
19        msg: String,
20    },
21
22    /// A unit token was syntactically well-formed but is not a known UCUM atom
23    /// (optionally with a prefix).
24    #[error("unknown unit atom: {code}")]
25    UnknownAtom {
26        /// The offending token.
27        code: String,
28    },
29
30    /// Two units were compared or converted but have different dimensions.
31    #[error("units not commensurable: {from} vs {to}")]
32    NotComparable {
33        /// The source unit expression.
34        from: String,
35        /// The target unit expression.
36        to: String,
37    },
38
39    /// Conversion was requested for a special (non-multiplicative) unit whose
40    /// conversion is not supported, e.g. a logarithmic, arbitrary, or
41    /// otherwise non-affine unit, or an affine unit used inside a compound
42    /// term.
43    #[error("conversion unsupported for special unit: {unit}")]
44    UnsupportedSpecial {
45        /// The unit expression that could not be converted.
46        unit: String,
47    },
48}