shapes_converter/shex_to_html/
shex2html_config.rs

1use std::{
2    fs, io,
3    path::{Path, PathBuf},
4};
5
6use iri_s::IriS;
7use serde::{Deserialize, Serialize};
8use shex_validation::ShExConfig;
9use srdf::RDFS_LABEL_STR;
10use thiserror::Error;
11
12use crate::ShEx2UmlConfig;
13
14pub const DEFAULT_COLOR_PROPERTY_NAME: &str = "blue";
15pub const DEFAULT_LANDING_PAGE_NAME: &str = "index.html";
16pub const DEFAULT_SHAPE_TEMPLATE_NAME: &str = "shape.html";
17
18#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
19pub struct ShEx2HtmlConfig {
20    pub landing_page_name: String,
21    pub shape_template_name: String,
22    pub css_file_name: Option<String>,
23    pub title: String,
24    pub target_folder: Option<PathBuf>,
25    pub color_property_name: Option<String>,
26    pub replace_iri_by_label: Option<bool>,
27    pub annotation_label: Vec<IriS>,
28    pub embed_svg_schema: bool,
29    pub embed_svg_shape: bool,
30    pub shex2uml: Option<ShEx2UmlConfig>,
31    pub shex: Option<ShExConfig>,
32}
33
34impl Default for ShEx2HtmlConfig {
35    fn default() -> Self {
36        Self {
37            title: "ShEx schema".to_string(),
38            landing_page_name: DEFAULT_LANDING_PAGE_NAME.to_string(),
39            shape_template_name: DEFAULT_SHAPE_TEMPLATE_NAME.to_string(),
40            css_file_name: Some("shex2html.css".to_string()),
41            target_folder: None,
42            color_property_name: Some(DEFAULT_COLOR_PROPERTY_NAME.to_string()),
43            annotation_label: vec![IriS::new_unchecked(RDFS_LABEL_STR)],
44            replace_iri_by_label: None,
45            embed_svg_schema: true,
46            embed_svg_shape: true,
47            shex2uml: Some(ShEx2UmlConfig::new()),
48            shex: Some(ShExConfig::default()),
49        }
50    }
51}
52
53impl ShEx2HtmlConfig {
54    /// Get the ShEx config if it has been declared or the default one
55    pub fn shex_config(&self) -> ShExConfig {
56        match &self.shex {
57            None => ShExConfig::default(),
58            Some(sc) => sc.clone(),
59        }
60    }
61
62    pub fn with_target_folder<P: AsRef<Path>>(mut self, target_folder: P) -> Self {
63        self.target_folder = Some(target_folder.as_ref().to_path_buf());
64        self
65    }
66
67    pub fn target_folder(&self) -> PathBuf {
68        match &self.target_folder {
69            Some(tf) => tf.to_owned(),
70            None => Path::new(".").to_path_buf(),
71        }
72    }
73
74    pub fn landing_page(&self) -> PathBuf {
75        match &self.target_folder {
76            Some(tf) => tf.as_path().join(self.landing_page_name.as_str()),
77            None => Path::new(self.landing_page_name.as_str()).to_path_buf(),
78        }
79    }
80
81    pub fn landing_page_name(&self) -> String {
82        self.landing_page().to_string_lossy().to_string()
83    }
84
85    pub fn from_file(file_name: &str) -> Result<ShEx2HtmlConfig, ShEx2HtmlConfigError> {
86        let config_str = fs::read_to_string(file_name).map_err(|e| {
87            ShEx2HtmlConfigError::ReadingConfigError {
88                path_name: file_name.to_string(),
89                error: e,
90            }
91        })?;
92        toml::from_str::<ShEx2HtmlConfig>(&config_str).map_err(|e| {
93            ShEx2HtmlConfigError::TomlError {
94                path_name: file_name.to_string(),
95                error: e,
96            }
97        })
98    }
99
100    pub fn shex2uml_config(&self) -> ShEx2UmlConfig {
101        match &self.shex2uml {
102            None => ShEx2UmlConfig::default(),
103            Some(s) => s.clone(),
104        }
105    }
106}
107
108#[derive(Error, Debug)]
109pub enum ShEx2HtmlConfigError {
110    #[error("Reading path {path_name:?} error: {error:?}")]
111    ReadingConfigError { path_name: String, error: io::Error },
112
113    #[error("Reading TOML from {path_name:?}. Error: {error:?}")]
114    TomlError {
115        path_name: String,
116        error: toml::de::Error,
117    },
118}