Skip to main content

shacl_ast/ast/
schema.rs

1use crate::shape::Shape;
2use iri_s::IriS;
3use prefixmap::PrefixMap;
4use rudof_rdf::rdf_core::{Rdf, term::Object};
5use std::collections::hash_map::IntoIter;
6use std::{collections::HashMap, fmt::Display};
7
8#[derive(Debug, Clone, Default)]
9pub struct ShaclSchema<RDF>
10where
11    RDF: Rdf,
12    RDF::Term: Clone,
13{
14    // imports: Vec<IriS>,
15    // entailments: Vec<IriS>,
16    shapes: HashMap<Object, Shape<RDF>>,
17    prefixmap: PrefixMap,
18    base: Option<IriS>,
19}
20
21impl<RDF: Rdf> ShaclSchema<RDF> {
22    pub fn new() -> ShaclSchema<RDF> {
23        ShaclSchema {
24            shapes: HashMap::new(),
25            prefixmap: PrefixMap::new(),
26            base: None,
27        }
28    }
29
30    pub fn is_empty(&self) -> bool {
31        self.shapes.is_empty()
32    }
33
34    pub fn with_prefixmap(mut self, prefixmap: PrefixMap) -> Self {
35        self.prefixmap = prefixmap;
36        self
37    }
38
39    pub fn with_shapes(mut self, shapes: HashMap<Object, Shape<RDF>>) -> Self {
40        self.shapes = shapes;
41        self
42    }
43
44    pub fn prefix_map(&self) -> PrefixMap {
45        self.prefixmap.clone()
46    }
47
48    pub fn base(&self) -> Option<IriS> {
49        self.base.clone()
50    }
51
52    pub fn iter(&self) -> impl Iterator<Item = (&Object, &Shape<RDF>)> {
53        self.shapes.iter()
54    }
55
56    pub fn get_shape(&self, sref: &Object) -> Option<&Shape<RDF>> {
57        self.shapes.get(sref)
58    }
59}
60
61impl<RDF: Rdf> IntoIterator for ShaclSchema<RDF> {
62    type Item = (Object, Shape<RDF>);
63    type IntoIter = IntoIter<Object, Shape<RDF>>;
64
65    fn into_iter(self) -> Self::IntoIter {
66        self.shapes.into_iter()
67    }
68}
69
70impl<RDF: Rdf> Display for ShaclSchema<RDF> {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        for (id, shape) in self.shapes.iter() {
73            writeln!(f, "{id} -> {shape}")?;
74        }
75        Ok(())
76    }
77}