Skip to main content

haystack_core/ontology/
lib.rs

1// Lib -- a library (namespace) of Haystack 4 defs.
2
3use std::collections::HashMap;
4
5use super::def::Def;
6
7/// A library of Haystack 4 definitions.
8///
9/// Each library groups related defs under a name (e.g. `"phIoT"`) and
10/// tracks its version, documentation, and dependencies on other libraries.
11#[derive(Debug, Clone)]
12pub struct Lib {
13    /// Library name, e.g. `"phIoT"`.
14    pub name: String,
15    /// Version string, e.g. `"4.0.0"`.
16    pub version: String,
17    /// Library description.
18    pub doc: String,
19    /// Names of dependent libraries.
20    pub depends: Vec<String>,
21    /// Symbol -> Def mapping.
22    pub defs: HashMap<String, Def>,
23}
24
25impl Lib {
26    /// Create a new library with the given name and empty defaults.
27    pub fn new(name: impl Into<String>) -> Self {
28        Self {
29            name: name.into(),
30            version: String::new(),
31            doc: String::new(),
32            depends: vec![],
33            defs: HashMap::new(),
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use crate::data::HDict;
42
43    #[test]
44    fn new_lib_defaults() {
45        let lib = Lib::new("phIoT");
46        assert_eq!(lib.name, "phIoT");
47        assert_eq!(lib.version, "");
48        assert_eq!(lib.doc, "");
49        assert!(lib.depends.is_empty());
50        assert!(lib.defs.is_empty());
51    }
52
53    #[test]
54    fn lib_with_defs() {
55        let mut lib = Lib::new("test");
56        lib.version = "1.0.0".to_string();
57        lib.defs.insert(
58            "site".to_string(),
59            Def {
60                symbol: "site".to_string(),
61                lib: "test".to_string(),
62                is_: vec!["marker".to_string()],
63                tag_on: vec![],
64                of: None,
65                mandatory: false,
66                doc: "A site".to_string(),
67                tags: HDict::new(),
68            },
69        );
70        assert_eq!(lib.defs.len(), 1);
71        assert!(lib.defs.contains_key("site"));
72    }
73}