shapes_converter/shex_to_html/
html_schema.rs

1use std::{
2    collections::{hash_map::Entry, HashMap},
3    time::SystemTime,
4};
5
6use chrono::{DateTime, Utc};
7use prefixmap::PrefixMap;
8
9use super::{HtmlShape, NodeId, ShEx2HtmlConfig};
10use crate::{
11    landing_html_template::{LandingHtmlTemplate, ShapeRef},
12    ShEx2HtmlError,
13};
14
15#[derive(Debug, PartialEq, Default)]
16pub struct HtmlSchema {
17    labels_counter: usize,
18    labels: HashMap<String, NodeId>,
19    shapes: HashMap<NodeId, HtmlShape>,
20    prefixmap: PrefixMap,
21    svg_schema: String,
22}
23
24impl HtmlSchema {
25    pub fn new() -> HtmlSchema {
26        Default::default()
27    }
28
29    pub fn with_prefixmap(mut self, prefixmap: PrefixMap) -> Self {
30        self.prefixmap = prefixmap;
31        self
32    }
33
34    /// Tries to get a node from a label. If it exists returns the node and true, otherwise, adds the node and returns false
35    pub fn get_node_adding_label(&mut self, label: &str) -> (NodeId, bool) {
36        match self.labels.entry(label.to_string()) {
37            Entry::Occupied(c) => (*c.get(), true),
38            Entry::Vacant(v) => {
39                self.labels_counter += 1;
40                let n = NodeId::new(self.labels_counter);
41                v.insert(n);
42                (n, false)
43            }
44        }
45    }
46
47    pub fn set_svg_schema(&mut self, svg_schema: &str) {
48        self.svg_schema = svg_schema.to_string()
49    }
50
51    pub fn add_component(
52        &mut self,
53        node: NodeId,
54        component: HtmlShape,
55    ) -> Result<(), ShEx2HtmlError> {
56        match self.shapes.entry(node) {
57            Entry::Occupied(mut v) => {
58                v.get_mut().merge(&component);
59                Ok(())
60            }
61            Entry::Vacant(v) => {
62                v.insert(component);
63                Ok(())
64            }
65        }
66    }
67
68    pub fn shapes(&self) -> impl Iterator<Item = &HtmlShape> {
69        self.shapes.values()
70    }
71
72    pub fn shapes_mut(&mut self) -> impl Iterator<Item = &mut HtmlShape> {
73        self.shapes.values_mut()
74    }
75
76    pub fn to_landing_html_schema(&self, config: &ShEx2HtmlConfig) -> LandingHtmlTemplate {
77        let mut shapes = Vec::new();
78        for shape in self.shapes.values() {
79            shapes.push(ShapeRef::new(
80                shape.name().name().as_str(),
81                shape.name().as_relative_href().unwrap_or_default().as_str(),
82                shape.name().label().unwrap_or_default().as_str(),
83            ))
84        }
85        const VERSION: &str = env!("CARGO_PKG_VERSION");
86        let curr_time = SystemTime::now();
87        let dt: DateTime<Utc> = curr_time.into();
88        let created_time = dt.format("%Y-&m-%d %H:%M:%S").to_string();
89
90        LandingHtmlTemplate {
91            title: config.title.clone(),
92            rudof_version: VERSION.to_string(),
93            created_time,
94            svg_schema: self.svg_schema.clone(),
95            shapes,
96        }
97    }
98}