rudof_lib/
rudof_config.rs1#[cfg(feature = "dctap")]
2use dctap::TapConfig;
3use serde::{Deserialize, Serialize};
4use shapes_converter::{ShEx2HtmlConfig, ShEx2SparqlConfig, ShEx2UmlConfig, Shacl2ShExConfig};
5
6#[cfg(feature = "dctap")]
7use shapes_converter::Tap2ShExConfig;
8use shex_validation::{ShExConfig, ValidatorConfig};
9use sparql_service::ServiceConfig;
10use srdf::RdfDataConfig;
11use std::io::Read;
12use std::path::Path;
13use std::str::FromStr;
14
15use crate::RudofError;
16
17#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
20pub struct RudofConfig {
21 rdf_data: Option<RdfDataConfig>,
22 shex: Option<ShExConfig>,
23 shex_validator: Option<ValidatorConfig>,
24 shex2uml: Option<ShEx2UmlConfig>,
25 shex2html: Option<ShEx2HtmlConfig>,
26 shacl2shex: Option<Shacl2ShExConfig>,
27 #[cfg(feature = "dctap")]
28 tap2shex: Option<Tap2ShExConfig>,
29 #[cfg(feature = "dctap")]
30 tap: Option<TapConfig>,
31 shex2sparql: Option<ShEx2SparqlConfig>,
32 service: Option<ServiceConfig>,
33}
34
35impl RudofConfig {
36 pub fn new() -> RudofConfig {
37 Self::default()
38 }
39
40 pub fn with_rdf_data_config(mut self, rdf_data_config: RdfDataConfig) -> Self {
41 self.rdf_data = Some(rdf_data_config);
42 self
43 }
44
45 pub fn with_shex_validator_config(mut self, shex_validator_config: ValidatorConfig) -> Self {
46 self.shex_validator = Some(shex_validator_config);
47 self
48 }
49
50 pub fn with_shex_config(mut self, shex_config: ShExConfig) -> Self {
51 self.shex = Some(shex_config);
52 self
53 }
54
55 pub fn from_path<P: AsRef<Path>>(path: P) -> Result<RudofConfig, RudofError> {
57 let path_name = path.as_ref().display().to_string();
58 let mut f =
59 std::fs::File::open(path).map_err(|e| RudofError::RudofConfigFromPathError {
60 path: path_name.clone(),
61 error: e,
62 })?;
63 let mut s = String::new();
64 f.read_to_string(&mut s)
65 .map_err(|e| RudofError::RudofConfigFromPathError {
66 path: path_name.clone(),
67 error: e,
68 })?;
69
70 let config: RudofConfig =
71 toml::from_str(s.as_str()).map_err(|e| RudofError::RudofConfigTomlError {
72 path: path_name.clone(),
73 error: e,
74 })?;
75 Ok(config)
76 }
77
78 pub fn validator_config(&self) -> ValidatorConfig {
79 match &self.shex_validator {
80 None => ValidatorConfig::default(),
81 Some(cfg) => cfg.clone(),
82 }
83 }
84
85 pub fn shex_config(&self) -> ShExConfig {
86 match &self.shex {
87 None => ShExConfig::default(),
88 Some(cfg) => cfg.clone(),
89 }
90 }
91
92 pub fn show_extends(&self) -> bool {
93 self.shex_config().show_extends.unwrap_or(true)
94 }
95
96 pub fn show_imports(&self) -> bool {
97 self.shex_config().show_extends.unwrap_or(true)
98 }
99
100 pub fn show_shapes(&self) -> bool {
101 self.shex_config().show_shapes.unwrap_or(true)
102 }
103
104 pub fn show_dependencies(&self) -> bool {
105 self.shex_config().show_dependencies.unwrap_or(false)
106 }
107
108 pub fn show_ir(&self) -> bool {
109 self.shex_config().show_ir.unwrap_or(true)
110 }
111
112 pub fn rdf_data_config(&self) -> RdfDataConfig {
113 self.rdf_data.clone().unwrap_or_default()
114 }
115
116 #[cfg(feature = "dctap")]
117 pub fn tap_config(&self) -> TapConfig {
118 self.tap.clone().unwrap_or_default()
119 }
120
121 #[cfg(feature = "dctap")]
122 pub fn tap2shex_config(&self) -> Tap2ShExConfig {
123 self.tap2shex.clone().unwrap_or_default()
124 }
125
126 pub fn shex2uml_config(&self) -> ShEx2UmlConfig {
127 self.shex2uml.clone().unwrap_or_default()
128 }
129
130 pub fn shex2html_config(&self) -> ShEx2HtmlConfig {
131 self.shex2html.clone().unwrap_or_default()
132 }
133
134 pub fn service_config(&self) -> ServiceConfig {
135 self.service.clone().unwrap_or_default()
136 }
137
138 pub fn shex2sparql_config(&self) -> ShEx2SparqlConfig {
139 self.shex2sparql.clone().unwrap_or_default()
140 }
141
142 pub fn shacl2shex_config(&self) -> Shacl2ShExConfig {
143 self.shacl2shex.clone().unwrap_or_default()
144 }
145
146 pub fn rdf_data_base(&self) -> Option<&str> {
147 match &self.rdf_data {
148 None => None,
149 Some(rdf_data_config) => rdf_data_config.base.as_ref().map(|i| i.as_str()),
150 }
151 }
152
153 pub fn automatic_base(&self) -> bool {
154 match &self.rdf_data {
155 None => true,
156 Some(rdf_data_config) => rdf_data_config.automatic_base.unwrap_or(true),
157 }
158 }
159}
160
161impl FromStr for RudofConfig {
162 type Err = String;
163
164 fn from_str(s: &str) -> Result<Self, Self::Err> {
165 toml::from_str(s).map_err(|e| format!("Failed to parse RudofConfig: {}", e))
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use iri_s::iri;
172
173 use super::*;
174
175 #[test]
176 fn test_rudof_config() {
177 let s = r#"[tap2shex]
178base_iri = "http://example.org/"
179
180[tap2shex.prefixmap]
181dct = "http://purl.org/dc/terms/"
182rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
183foaf = "http://xmlns.com/foaf/0.1/"
184xsd = "http://www.w3.org/2001/XMLSchema#"
185sdo = "https://schema.org/"
186ex = "http://example.org/"
187"#;
188 let config = RudofConfig::from_str(s).unwrap();
189 assert_eq!(
190 config.tap2shex_config().base_iri.unwrap(),
191 iri!("http://example.org/")
192 );
193 assert_eq!(
194 config
195 .tap2shex_config()
196 .prefixmap()
197 .find("sdo")
198 .unwrap()
199 .clone(),
200 iri!("https://schema.org/")
201 );
202 }
203}