shapes_converter/shex_to_sparql/
select_query.rs

1use std::fmt::Display;
2
3use iri_s::IriS;
4use prefixmap::PrefixMap;
5
6use crate::shex_to_sparql::TriplePattern;
7
8pub struct SelectQuery {
9    prefixmap: Option<PrefixMap>,
10    base: Option<IriS>,
11    patterns: Vec<TriplePattern>,
12}
13
14impl SelectQuery {
15    pub fn new() -> SelectQuery {
16        SelectQuery {
17            prefixmap: None,
18            base: None,
19            patterns: Vec::new(),
20        }
21    }
22
23    pub fn with_prefixmap(mut self, prefixmap: Option<PrefixMap>) -> Self {
24        self.prefixmap = prefixmap;
25        self
26    }
27
28    pub fn without_prefixmap(mut self) -> Self {
29        self.prefixmap = None;
30        self
31    }
32
33    pub fn with_base(mut self, base: Option<IriS>) -> Self {
34        self.base = base;
35        self
36    }
37
38    pub fn with_patterns(mut self, patterns: Vec<TriplePattern>) -> Self {
39        self.patterns = patterns;
40        self
41    }
42}
43
44impl Display for SelectQuery {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        if let Some(base) = &self.base {
47            writeln!(f, "{}", base)?
48        };
49        // TODO: Unify these 2 branches in one...it was giving an move error on prefixmap that I wanted to bypass quickly...
50        if let Some(prefixmap) = &self.prefixmap {
51            writeln!(f, "{}", prefixmap)?;
52            writeln!(f, "SELECT * WHERE {{")?;
53            for pattern in &self.patterns {
54                write!(f, " ")?;
55                pattern.show_qualified(f, prefixmap).map_err(|_e|
56                        // TODO: The following is a hack to make the type checker happy...
57                        std::fmt::Error)?;
58                writeln!(f)?;
59            }
60        } else {
61            let prefixmap = PrefixMap::new();
62            for pattern in &self.patterns {
63                pattern.show_qualified(f, &prefixmap).map_err(|_e|
64                        // TODO: The following is a hack to make the type checker happy...
65                        std::fmt::Error)?;
66            }
67        }
68        writeln!(f, "}}")?;
69        Ok(())
70    }
71}
72
73impl Default for SelectQuery {
74    fn default() -> Self {
75        Self::new()
76    }
77}