pub mod check;
pub mod expand;
#[cfg(feature = "fetch-bytes")]
pub mod fetch;
pub mod node_types;
#[cfg(feature = "pack")]
pub mod pack;
pub mod roles;
#[cfg(feature = "pack")]
pub use pack::Pack;
use std::sync::OnceLock;
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
pub struct Term {
pub name: String,
pub definition: String,
}
#[derive(Debug, Deserialize)]
pub struct Vocabulary {
pub version: String,
pub table: Vec<Term>,
pub facets: Vec<Term>,
#[serde(default)]
pub either_tier: Vec<String>,
pub containments: Vec<(String, String)>,
}
impl Vocabulary {
pub fn table_terms(&self) -> impl Iterator<Item = &str> {
self.table.iter().map(|t| t.name.as_str())
}
pub fn facet_terms(&self) -> impl Iterator<Item = &str> {
self.facets.iter().map(|t| t.name.as_str())
}
pub fn is_table_term(&self, name: &str) -> bool {
self.table.iter().any(|t| t.name == name)
}
pub fn is_facet_term(&self, name: &str) -> bool {
self.facets.iter().any(|t| t.name == name)
}
pub fn is_either_tier(&self, name: &str) -> bool {
self.either_tier.iter().any(|t| t == name)
}
}
pub fn vocabulary() -> &'static Vocabulary {
static VOCAB: OnceLock<Vocabulary> = OnceLock::new();
VOCAB.get_or_init(|| {
serde_json::from_str(include_str!("../vocabulary/vocabulary.json"))
.expect("embedded vocabulary.json is malformed")
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn embedded_vocabulary_parses_and_is_closed_and_underscored() {
let v = vocabulary();
assert_eq!(v.version, "0.1.0");
assert_eq!(v.table.len(), 22);
assert_eq!(v.facets.len(), 7);
for t in v.table.iter().chain(v.facets.iter()) {
assert!(t.name.starts_with('_'), "{} must be underscored", t.name);
assert!(!t.definition.is_empty());
}
for f in &v.facets {
assert!(!v.is_table_term(&f.name), "{} is in both tiers", f.name);
}
for name in &v.either_tier {
assert!(v.is_table_term(name), "{name} is not a table term");
assert!(!v.is_facet_term(name), "{name} is already a facet term");
}
for (inner, outer) in &v.containments {
assert!(v.is_table_term(inner), "{inner} not a table term");
assert!(v.is_table_term(outer), "{outer} not a table term");
}
}
}