domainforge-core 0.14.0

Rust core library implementing the SEA DSL primitives and validation engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{OnceLock, RwLock};

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Dimension {
    Mass,
    Length,
    Volume,
    Currency,
    Time,
    Temperature,
    Count,
    Custom(String),
}

impl Dimension {
    /// Parse a dimension name in a case-insensitive way and map to builtin dimension
    pub fn parse(name: &str) -> Self {
        match name.to_ascii_lowercase().as_str() {
            "mass" => Dimension::Mass,
            "length" => Dimension::Length,
            "volume" => Dimension::Volume,
            "currency" => Dimension::Currency,
            "time" => Dimension::Time,
            "temperature" => Dimension::Temperature,
            "count" => Dimension::Count,
            other => Dimension::Custom(other.to_string()),
        }
    }
}

impl std::str::FromStr for Dimension {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Dimension::parse(s))
    }
}

impl std::fmt::Display for Dimension {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Dimension::Mass => write!(f, "Mass"),
            Dimension::Length => write!(f, "Length"),
            Dimension::Volume => write!(f, "Volume"),
            Dimension::Currency => write!(f, "Currency"),
            Dimension::Time => write!(f, "Time"),
            Dimension::Temperature => write!(f, "Temperature"),
            Dimension::Count => write!(f, "Count"),
            Dimension::Custom(s) => write!(f, "{}", s),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Unit {
    symbol: String,
    name: String,
    dimension: Dimension,
    base_factor: Decimal,
    base_unit: String,
}

impl std::fmt::Display for Unit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.symbol)
    }
}

impl Unit {
    pub fn new(
        symbol: impl Into<String>,
        name: impl Into<String>,
        dimension: Dimension,
        base_factor: Decimal,
        base_unit: impl Into<String>,
    ) -> Self {
        let symbol = symbol.into();
        Self {
            symbol,
            name: name.into(),
            dimension,
            base_factor,
            base_unit: base_unit.into(),
        }
    }

    pub fn new_base(
        symbol: impl Into<String>,
        name: impl Into<String>,
        dimension: Dimension,
    ) -> Result<Self, UnitError> {
        let symbol = symbol.into();
        let base_unit = symbol.clone();
        Ok(Self {
            symbol,
            name: name.into(),
            dimension,
            base_factor: Decimal::ONE,
            base_unit,
        })
    }

    pub fn with_base(mut self, base_unit: impl Into<String>) -> Self {
        self.base_unit = base_unit.into();
        self
    }

    pub fn symbol(&self) -> &str {
        &self.symbol
    }
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn dimension(&self) -> &Dimension {
        &self.dimension
    }
    pub fn base_factor(&self) -> Decimal {
        self.base_factor
    }
    pub fn base_unit(&self) -> &str {
        &self.base_unit
    }
}

pub trait UnitConversion {
    fn convert_to_base(&self, value: Decimal) -> Decimal;
    fn convert_from_base(&self, value: Decimal) -> Decimal;
}

impl UnitConversion for Unit {
    fn convert_to_base(&self, value: Decimal) -> Decimal {
        value * self.base_factor
    }

    fn convert_from_base(&self, value: Decimal) -> Decimal {
        value / self.base_factor
    }
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum UnitError {
    UnitNotFound(String),
    IncompatibleDimensions { from: Dimension, to: Dimension },
    ConversionNotDefined { from: String, to: String },
    ZeroBaseFactor,
    DuplicateUnit(String),
}

impl std::fmt::Display for UnitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnitError::UnitNotFound(symbol) => write!(f, "Unit not found: {}", symbol),
            UnitError::IncompatibleDimensions { from, to } => {
                write!(f, "Cannot convert between {:?} and {:?}", from, to)
            }
            UnitError::ConversionNotDefined { from, to } => {
                write!(f, "Conversion not defined from {} to {}", from, to)
            }
            UnitError::ZeroBaseFactor => {
                write!(f, "Unit base_factor cannot be zero")
            }
            UnitError::DuplicateUnit(symbol) => {
                write!(f, "Unit already registered: {}", symbol)
            }
        }
    }
}

impl std::error::Error for UnitError {}

#[derive(Debug, Clone)]
pub struct UnitRegistry {
    units: HashMap<String, Unit>,
    base_units: HashMap<Dimension, String>,
}

impl Default for UnitRegistry {
    fn default() -> Self {
        let mut registry = Self {
            units: HashMap::new(),
            base_units: HashMap::new(),
        };

        // Mass units
        registry.register_base(Dimension::Mass, "kg");
        registry.register_builtin(Unit::new(
            "kg",
            "kilogram",
            Dimension::Mass,
            Decimal::from(1),
            "kg",
        ));
        registry.register_builtin(Unit::new(
            "g",
            "gram",
            Dimension::Mass,
            Decimal::new(1, 3),
            "kg",
        ));
        registry.register_builtin(Unit::new(
            "lb",
            "pound",
            Dimension::Mass,
            Decimal::new(45359237, 8),
            "kg",
        ));

        // Length units
        registry.register_base(Dimension::Length, "m");
        registry.register_builtin(Unit::new(
            "m",
            "meter",
            Dimension::Length,
            Decimal::from(1),
            "m",
        ));
        registry.register_builtin(Unit::new(
            "cm",
            "centimeter",
            Dimension::Length,
            Decimal::new(1, 2),
            "m",
        ));
        registry.register_builtin(Unit::new(
            "in",
            "inch",
            Dimension::Length,
            Decimal::new(254, 4),
            "m",
        ));

        // Volume units
        registry.register_base(Dimension::Volume, "L");
        registry.register_builtin(Unit::new(
            "L",
            "liter",
            Dimension::Volume,
            Decimal::from(1),
            "L",
        ));
        registry.register_builtin(Unit::new(
            "mL",
            "milliliter",
            Dimension::Volume,
            Decimal::new(1, 3),
            "L",
        ));

        // Currency units (no conversion without exchange rates). U5: each
        // currency below is its own `base_unit` with `base_factor: 1`, so
        // `Unit::convert_to_base`/`convert_from_base` map every currency to
        // itself at 1.0. That's only correct because `UnitRegistry::convert`
        // special-cases `Dimension::Currency` and refuses cross-currency
        // conversion outright (see below) rather than trusting
        // `base_factor`. Any new code path that reads `base_factor`
        // directly instead of going through `convert` would silently treat
        // EUR/GBP as equal to USD at parity — keep the special-case in sync
        // if this ever needs real exchange rates.
        registry.register_base(Dimension::Currency, "USD");
        registry.register_builtin(Unit::new(
            "USD",
            "US Dollar",
            Dimension::Currency,
            Decimal::from(1),
            "USD",
        ));
        registry.register_builtin(Unit::new(
            "EUR",
            "Euro",
            Dimension::Currency,
            Decimal::from(1),
            "EUR",
        ));
        registry.register_builtin(Unit::new(
            "GBP",
            "British Pound",
            Dimension::Currency,
            Decimal::from(1),
            "GBP",
        ));

        // Time units
        registry.register_base(Dimension::Time, "s");
        registry.register_builtin(Unit::new(
            "s",
            "second",
            Dimension::Time,
            Decimal::from(1),
            "s",
        ));
        registry.register_builtin(Unit::new(
            "min",
            "minute",
            Dimension::Time,
            Decimal::from(60),
            "s",
        ));
        registry.register_builtin(Unit::new(
            "h",
            "hour",
            Dimension::Time,
            Decimal::from(3600),
            "s",
        ));
        registry.register_builtin(Unit::new(
            "ms",
            "millisecond",
            Dimension::Time,
            Decimal::new(1, 3),
            "s",
        ));
        registry.register_builtin(Unit::new(
            "us",
            "microsecond",
            Dimension::Time,
            Decimal::new(1, 6),
            "s",
        ));
        registry.register_builtin(Unit::new(
            "ns",
            "nanosecond",
            Dimension::Time,
            Decimal::new(1, 9),
            "s",
        ));

        // Count (dimensionless)
        registry.register_base(Dimension::Count, "units");
        registry.register_builtin(Unit::new(
            "units",
            "units",
            Dimension::Count,
            Decimal::from(1),
            "units",
        ));
        registry.register_builtin(Unit::new(
            "items",
            "items",
            Dimension::Count,
            Decimal::from(1),
            "items",
        ));
        // Common metric unit (M1): dimensionless, so it shares the Count
        // dimension rather than inventing a new one just for a label.
        registry.register_builtin(Unit::new(
            "percent",
            "percent",
            Dimension::Count,
            Decimal::from(1),
            "units",
        ));

        registry
    }
}

impl UnitRegistry {
    pub fn new() -> Self {
        Self {
            units: HashMap::new(),
            base_units: HashMap::new(),
        }
    }

    /// U3: previously this only checked for a duplicate symbol. It now also
    /// rejects a zero `base_factor` (division by zero in
    /// `convert_from_base`; the JSON-registration path already caught this,
    /// direct `register()` calls didn't) and a `base_unit` that references
    /// an already-registered unit of a *different* dimension (a copy-paste
    /// mistake that would silently cross-convert two unrelated dimensions).
    /// It deliberately does not require `base_unit` to already be
    /// registered — callers legitimately build a registry incrementally
    /// (e.g. `register_from_json` into a fresh `UnitRegistry::new()`, where
    /// the base symbol is asserted by convention, not by an existing
    /// entry).
    ///
    /// U2: Temperature is rejected outright. `Unit::convert` is a pure
    /// linear scale (`value * base_factor`), which cannot express the
    /// affine Celsius/Fahrenheit conversions real temperature units need;
    /// registering one here would silently produce wrong numbers rather
    /// than erroring, which is worse than not supporting it.
    pub fn register(&mut self, unit: Unit) -> Result<(), UnitError> {
        if self.units.contains_key(&unit.symbol) {
            return Err(UnitError::DuplicateUnit(unit.symbol.clone()));
        }
        if unit.base_factor == Decimal::ZERO {
            return Err(UnitError::ZeroBaseFactor);
        }
        if matches!(unit.dimension, Dimension::Temperature) {
            return Err(UnitError::ConversionNotDefined {
                from: unit.symbol.clone(),
                to: "Temperature (needs affine/offset conversion, unsupported by this linear unit model)"
                    .to_string(),
            });
        }
        if unit.base_unit != unit.symbol {
            if let Some(base) = self.units.get(&unit.base_unit) {
                if base.dimension != unit.dimension {
                    return Err(UnitError::IncompatibleDimensions {
                        from: unit.dimension.clone(),
                        to: base.dimension.clone(),
                    });
                }
            }
        }
        self.units.insert(unit.symbol.clone(), unit);
        Ok(())
    }

    fn register_builtin(&mut self, unit: Unit) {
        self.register(unit)
            .expect("builtin unit registration must be valid");
    }

    /// Records that a dimension is known, without asserting a base unit for
    /// it. Previously this inserted an empty-string placeholder into
    /// `base_units` when the dimension had none yet (via `or_default`),
    /// which poisoned any later `base_units()` consumer with a bogus ""
    /// base symbol (U3). `register_base` is the only source of truth for a
    /// dimension's actual base unit, so this is now a no-op until that's
    /// called.
    pub fn register_dimension(&mut self, _dimension: Dimension) {}

    pub fn register_base(&mut self, dimension: Dimension, base_unit: impl Into<String>) {
        self.base_units.insert(dimension, base_unit.into());
    }

    pub fn get_unit(&self, symbol: &str) -> Result<&Unit, UnitError> {
        self.units
            .get(symbol)
            .ok_or_else(|| UnitError::UnitNotFound(symbol.to_string()))
    }

    pub fn units(&self) -> &HashMap<String, Unit> {
        &self.units
    }

    pub fn base_units(&self) -> &HashMap<Dimension, String> {
        &self.base_units
    }

    pub fn convert(&self, value: Decimal, from: &Unit, to: &Unit) -> Result<Decimal, UnitError> {
        if from.dimension != to.dimension {
            return Err(UnitError::IncompatibleDimensions {
                from: from.dimension.clone(),
                to: to.dimension.clone(),
            });
        }

        if matches!(from.dimension, Dimension::Currency) && from.symbol != to.symbol {
            return Err(UnitError::ConversionNotDefined {
                from: from.symbol.clone(),
                to: to.symbol.clone(),
            });
        }

        // Cheap invariant check (debug builds only, zero release cost):
        // same-dimension units should always share a base_unit symbol.
        // `register()` enforces this at insertion time (U3), so tripping
        // this in a test/dev build means the registry was built
        // inconsistently — e.g. two units of the same dimension registered
        // against different `base_unit` strings.
        debug_assert_eq!(
            from.base_unit, to.base_unit,
            "same-dimension units '{}' and '{}' disagree on base_unit ('{}' vs '{}')",
            from.symbol, to.symbol, from.base_unit, to.base_unit
        );

        let in_base = from.convert_to_base(value);
        let in_target = to.convert_from_base(in_base);

        Ok(in_target)
    }

    /// U4: this is a process-wide mutable singleton. Any model's custom
    /// unit registrations (via the DSL's `Dimension`/`Unit` declarations,
    /// see `parser/ast.rs`) persist for the lifetime of the process and are
    /// visible to every other model parsed afterward in the same process —
    /// harmless for the one-shot CLI, but a cross-request leak risk for any
    /// long-lived host (an LSP server, a WASM/Python binding kept alive
    /// across calls). If that ever becomes a problem, thread an explicit
    /// `&UnitRegistry` through instead of reaching for this singleton.
    pub fn global() -> &'static RwLock<UnitRegistry> {
        static GLOBAL_REGISTRY: OnceLock<RwLock<UnitRegistry>> = OnceLock::new();
        GLOBAL_REGISTRY.get_or_init(|| RwLock::new(UnitRegistry::default()))
    }

    /// Register units defined in a JSON string of the form:
    /// [{ "symbol": "X", "name": "Name", "dimension": "Currency", "base_factor": 1.0, "base_unit": "USD" }]
    pub fn register_from_json(&mut self, json: &str) -> Result<(), UnitError> {
        #[derive(Deserialize)]
        struct UnitConfig {
            symbol: String,
            name: String,
            dimension: String,
            base_factor: f64,
            base_unit: String,
        }

        let parsed: Vec<UnitConfig> =
            serde_json::from_str(json).map_err(|e| UnitError::ConversionNotDefined {
                from: "json".to_string(),
                to: e.to_string(),
            })?;
        for cfg in parsed {
            let dim = Dimension::parse(&cfg.dimension);
            let factor = Decimal::from_f64(cfg.base_factor).ok_or(UnitError::ZeroBaseFactor)?;
            if factor == Decimal::ZERO {
                return Err(UnitError::ZeroBaseFactor);
            }
            let unit = Unit::new(cfg.symbol, cfg.name, dim, factor, cfg.base_unit);
            self.register(unit)?;
        }
        Ok(())
    }
}

pub fn get_default_registry() -> &'static RwLock<UnitRegistry> {
    UnitRegistry::global()
}

/// Fallible unit lookup (U1). Returns `UnitError::UnitNotFound` for an
/// unregistered symbol instead of silently fabricating a Count-dimension
/// unit. Prefer this in new code; `unit_from_string` exists only for the
/// large pre-existing call-site surface that assumes an infallible lookup.
pub fn try_unit_from_string(symbol: &str) -> Result<Unit, UnitError> {
    let registry = get_default_registry();
    let registry = registry.read().unwrap_or_else(|e| e.into_inner());
    registry.get_unit(symbol).cloned()
}

/// Helper function to get a Unit from a string symbol, using the default
/// registry. Fabricates a unit for an unregistered symbol instead of
/// erroring (kept infallible for the large existing call-site surface —
/// see [`try_unit_from_string`] for the fallible alternative).
///
/// U1 fix: the fabricated unit's dimension is `Custom(symbol)`, not
/// `Count`. Previously every unregistered symbol landed in the shared
/// `Count` dimension, so two different typos (or two unrelated free-form
/// business units, e.g. "orders" vs "widgets") were treated as the same
/// dimension and converted 1:1 with no error — the actual bug this gap
/// caused. Keying the fabricated dimension by the symbol itself means
/// `convert()` now correctly reports `IncompatibleDimensions` between any
/// two distinct unregistered units, while a symbol converted against
/// itself (the only sane use of an ad-hoc unit) still works.
pub fn unit_from_string(symbol: impl Into<String>) -> Unit {
    let symbol = symbol.into();
    try_unit_from_string(&symbol).unwrap_or_else(|_| {
        Unit::new(
            symbol.clone(),
            symbol.clone(),
            Dimension::Custom(symbol.clone()),
            Decimal::from(1),
            symbol.clone(),
        )
    })
}