shapemap/
association.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::{NodeSelector, ShapeSelector};
use serde_derive::Serialize;
use shex_ast::{object_value::ObjectValue, ShapeExprLabel};
use srdf::SRDF;
use std::iter::once;

/// Combines a [`NodeSelector`] with a [`ShapeExprLabel`]
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct Association {
    pub node_selector: NodeSelector,
    pub shape_selector: ShapeSelector,
}

impl Association {
    pub fn new(node_selector: NodeSelector, shape_selector: ShapeSelector) -> Self {
        Association {
            node_selector,
            shape_selector,
        }
    }

    pub fn iter_node_shape<S>(
        &self,
        rdf: &S,
    ) -> impl Iterator<Item = (&ObjectValue, &ShapeExprLabel)>
    where
        S: SRDF,
    {
        self.node_selector.iter_node(rdf).flat_map(move |node| {
            self.shape_selector
                .iter_shape()
                .flat_map(move |label| once((node, label)))
        })
    }
}