shapes_converter/
converter_config.rs

1#[cfg(feature = "dctap")]
2use dctap::TapConfig;
3use serde::{Deserialize, Serialize};
4use std::io::Read;
5use std::path::Path;
6
7use crate::{ConverterError, ShEx2HtmlConfig, ShEx2SparqlConfig, ShEx2UmlConfig, Shacl2ShExConfig};
8
9#[cfg(feature = "dctap")]
10use crate::Tap2ShExConfig;
11
12#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
13pub struct ConverterConfig {
14    #[cfg(feature = "dctap")]
15    dctap: Option<TapConfig>,
16    shex2html: Option<ShEx2HtmlConfig>,
17    #[cfg(feature = "dctap")]
18    tap2shex: Option<Tap2ShExConfig>,
19    shex2sparql: Option<ShEx2SparqlConfig>,
20    shacl2shex: Option<Shacl2ShExConfig>,
21    shex2uml: Option<ShEx2UmlConfig>,
22}
23
24impl ConverterConfig {
25    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ConverterConfig, ConverterError> {
26        let path_name = path.as_ref().display().to_string();
27        let mut f = std::fs::File::open(path).map_err(|e| {
28            ConverterError::ConverterConfigFromPathError {
29                path: path_name.clone(),
30                error: e,
31            }
32        })?;
33        let mut s = String::new();
34        f.read_to_string(&mut s)
35            .map_err(|e| ConverterError::ConverterConfigFromPathError {
36                path: path_name.clone(),
37                error: e,
38            })?;
39
40        let config: ConverterConfig = toml::from_str(s.as_str()).map_err(|e| {
41            ConverterError::ConverterConfigFromTomlError {
42                path: path_name.clone(),
43                error: e,
44            }
45        })?;
46        Ok(config)
47    }
48
49    #[cfg(feature = "dctap")]
50    pub fn tap_config(&self) -> TapConfig {
51        match &self.dctap {
52            Some(tc) => tc.clone(),
53            None => TapConfig::default(),
54        }
55    }
56
57    #[cfg(feature = "dctap")]
58    pub fn tap2shex_config(&self) -> Tap2ShExConfig {
59        match &self.tap2shex {
60            Some(c) => c.clone(),
61            None => Tap2ShExConfig::default(),
62        }
63    }
64
65    pub fn shex2html_config(&self) -> ShEx2HtmlConfig {
66        match &self.shex2html {
67            Some(c) => c.clone(),
68            None => ShEx2HtmlConfig::default(),
69        }
70    }
71
72    pub fn shex2uml_config(&self) -> ShEx2UmlConfig {
73        match &self.shex2uml {
74            Some(c) => c.clone(),
75            None => ShEx2UmlConfig::default(),
76        }
77    }
78
79    pub fn shacl2shex_config(&self) -> Shacl2ShExConfig {
80        match &self.shacl2shex {
81            Some(c) => c.clone(),
82            None => Shacl2ShExConfig::default(),
83        }
84    }
85
86    pub fn shex2sparql_config(&self) -> ShEx2SparqlConfig {
87        match &self.shex2sparql {
88            Some(c) => c.clone(),
89            None => ShEx2SparqlConfig::default(),
90        }
91    }
92}