Skip to main content

om2/
lib.rs

1//! This is an auto-generated wrapper around the `om2:Unit` class, with a few
2//! supporting structs/enums/utils.
3//!
4//! The idea here is to map Unit into a big (de)serializable rust enum, so you
5//! don't need to know the label/symbol up front but can just grab it from the
6//! Unit enum:
7//!
8//! ```rust
9//! use om2::Unit;
10//!
11//! let wh = Unit::WattHour;
12//! assert_eq!(wh.symbol(), Some("W h".to_string()));
13//! assert_eq!(wh.label(), Some("watt hour".to_string()));
14//! ```
15//!
16//! Note that the `symbol()` and `label()` methods return Option because not all
17//! the enum values have those fields defined int he schema.
18//!
19//! This crate also exports `om2:Measure`, useful for holding full measurements.
20//! It also has getters by default, with the option to include setters as well:
21//!
22//! ```rust
23//! use om2::{Unit, Measure, NumericUnion};
24//! let measure = Measure::new(7.3 as f64, Unit::Hour);
25//! assert_eq!(measure.has_unit(), &Unit::Hour);
26//! assert_eq!(measure.has_numerical_value(), &NumericUnion::Double(7.3));
27//! ```
28//!
29//! Note that none of the available RDF/XML rust libs were able to parse the om2
30//! RDF schema, so this library requires conversion to .ttl first (this is done
31//! via the `make schema` command, which uses the `rapper` command line util
32//! under the hood).
33//!
34//! Features:
35//!
36//! - `getset_setters` - implements setters on the generated structs so they can
37//! be mutated in-place via setter methods
38//! - `getset_getmut` - implements mutable getters on the generated structs so
39//! they can be mutated in-place via &mut getters
40//!
41//! Note that *all* features are enabled when building the docs to give a sense
42//! of the library's full abilities.
43
44use getset::Getters;
45#[cfg(feature = "getset_setters")]
46use getset::Setters;
47#[cfg(feature = "getset_getmut")]
48use getset::MutGetters;
49#[cfg(feature = "with_serde")]
50use serde_derive::{Serialize, Deserialize};
51
52mod gen;
53mod dtype;
54
55pub use gen::*;
56pub use dtype::*;
57
58/// A numeric value with its unit of measure.
59///
60/// ID: http://www.ontology-of-units-of-measure.org/resource/om-2/Measure
61#[derive(Debug, PartialEq, Clone, Getters)]
62#[cfg_attr(feature = "getset_setters", derive(Setters))]
63#[cfg_attr(feature = "getset_getmut", derive(MutGetters))]
64#[cfg_attr(feature = "with_serde", derive(Serialize, Deserialize))]
65#[getset(get = "pub", set = "pub", get_mut = "pub")]
66pub struct Measure {
67    pub has_numerical_value: dtype::NumericUnion,
68    pub has_unit: Unit,
69}
70
71impl Measure {
72    pub fn new<T: Into<NumericUnion>>(value: T, unit: Unit) -> Self {
73        Self {
74            has_numerical_value: value.into(),
75            has_unit: unit,
76        }
77    }
78}
79
80#[cfg(test)]
81mod test {
82    use super::*;
83
84    #[test]
85    fn can_build() {
86        let measure = Measure::new(42.0 as f64, Unit::WattHour);
87        assert_eq!(measure.has_numerical_value(), &NumericUnion::Double(42.0));
88        assert_eq!(measure.has_unit(), &Unit::WattHour);
89    }
90}
91