shacl_validation/
value_nodes.rs

1use std::collections::HashMap;
2
3use srdf::Rdf;
4
5use crate::focus_nodes::FocusNodes;
6
7pub struct ValueNodes<S: Rdf>(HashMap<S::Term, FocusNodes<S>>);
8
9impl<S: Rdf> ValueNodes<S> {
10    pub fn new(iter: impl Iterator<Item = (S::Term, FocusNodes<S>)>) -> Self {
11        Self(HashMap::from_iter(iter))
12    }
13}
14
15pub trait IterationStrategy<S: Rdf> {
16    type Item;
17
18    fn iterate<'a>(
19        &'a self,
20        value_nodes: &'a ValueNodes<S>,
21    ) -> Box<dyn Iterator<Item = (&'a S::Term, &'a Self::Item)> + 'a>;
22}
23
24pub struct FocusNodeIteration;
25
26impl<S: Rdf> IterationStrategy<S> for FocusNodeIteration {
27    type Item = FocusNodes<S>;
28
29    fn iterate<'a>(
30        &'a self,
31        value_nodes: &'a ValueNodes<S>,
32    ) -> Box<dyn Iterator<Item = (&'a S::Term, &'a Self::Item)> + 'a> {
33        Box::new(value_nodes.0.iter())
34    }
35}
36
37pub struct ValueNodeIteration;
38
39impl<S: Rdf> IterationStrategy<S> for ValueNodeIteration {
40    type Item = S::Term;
41
42    fn iterate<'a>(
43        &'a self,
44        value_nodes: &'a ValueNodes<S>,
45    ) -> Box<dyn Iterator<Item = (&'a S::Term, &'a Self::Item)> + 'a> {
46        Box::new(value_nodes.0.iter().flat_map(|(focus_node, value_nodes)| {
47            value_nodes
48                .iter()
49                .map(move |value_node| (focus_node, value_node))
50        }))
51    }
52}