shapemap/
query_shape_map.rs1use std::fmt::Display;
2
3use crate::{Association, NodeSelector, ShapeSelector};
4use prefixmap::PrefixMap;
5use serde::Serialize;
6use shex_ast::{object_value::ObjectValue, ShapeExprLabel};
7use srdf::Query;
8
9#[derive(Debug, Default, PartialEq, Clone, Serialize)]
10pub struct QueryShapeMap {
11 associations: Vec<Association>,
12 nodes_prefixmap: PrefixMap,
13 shapes_prefixmap: PrefixMap,
14}
15
16impl QueryShapeMap {
17 pub fn new() -> Self {
18 Self::default()
19 }
20
21 pub fn nodes_prefixmap(&self) -> PrefixMap {
22 self.nodes_prefixmap.clone()
23 }
24
25 pub fn shapes_prefixmap(&self) -> PrefixMap {
26 self.shapes_prefixmap.clone()
27 }
28
29 pub fn with_nodes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
30 self.nodes_prefixmap = prefixmap.clone();
31 self
32 }
33
34 pub fn with_shapes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
35 self.shapes_prefixmap = prefixmap.clone();
36 self
37 }
38
39 pub fn add_association(&mut self, node_selector: NodeSelector, shape_selector: ShapeSelector) {
40 let association = Association::new(node_selector, shape_selector);
41 self.associations.push(association)
42 }
43
44 pub fn iter(&self) -> impl Iterator<Item = &Association> + '_ {
45 self.associations.iter()
46 }
47
48 pub fn iter_node_shape<'a, S>(
49 &'a self,
50 rdf: &'a S,
51 ) -> impl Iterator<Item = (&'a ObjectValue, &'a ShapeExprLabel)> + 'a
52 where
53 S: Query,
54 {
55 self.iter().flat_map(|assoc| assoc.iter_node_shape(rdf))
56 }
57}
58
59impl Display for QueryShapeMap {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(
62 f,
63 "{}",
64 serde_json::to_string_pretty(&self).map_err(|_| std::fmt::Error)?
65 )
66 }
67}