domainforge-core 0.13.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
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)]
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)
        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",
        ));

        registry
    }
}

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

    pub fn register(&mut self, unit: Unit) -> Result<(), UnitError> {
        if self.units.contains_key(&unit.symbol) {
            return Err(UnitError::DuplicateUnit(unit.symbol.clone()));
        }
        self.units.insert(unit.symbol.clone(), unit);
        Ok(())
    }

    fn register_builtin(&mut self, unit: Unit) {
        let _ = self.register(unit);
    }

    pub fn register_dimension(&mut self, dimension: Dimension) {
        self.base_units.entry(dimension).or_default();
    }

    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(),
            });
        }

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

        Ok(in_target)
    }

    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()
}

/// Helper function to get a Unit from a string symbol, using the default registry
/// Returns a Count-based unit if the symbol is not found
pub fn unit_from_string(symbol: impl Into<String>) -> Unit {
    let symbol = symbol.into();
    let registry = get_default_registry();
    let registry = registry.read().unwrap_or_else(|e| e.into_inner());

    registry.get_unit(&symbol).cloned().unwrap_or_else(|_| {
        // Default to Count dimension for unknown units
        Unit::new(
            symbol.clone(),
            symbol.clone(),
            Dimension::Count,
            Decimal::from(1),
            symbol.clone(),
        )
    })
}