1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::errors::ConfigError;
use dctap::TapConfig;
use rudof_rdf::rdf_core::RdfDataConfig;
use serde::{Deserialize, Serialize};
use shapes_comparator::ComparatorConfig;
use shapes_converter::{ShEx2HtmlConfig, ShEx2SparqlConfig, ShEx2UmlConfig, Shacl2ShExConfig, Tap2ShExConfig};
use shex_validation::{ShExConfig, ValidatorConfig};
use sparql_service::ServiceConfig;
use std::env;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;
/// Embedded default configuration in TOML format.
const DEFAULT_CONFIG: &str = include_str!("default_config.toml");
/// Main configuration structure for Rudof.
///
/// This structure encapsulates all configuration options for Rudof operations,
/// including RDF data handling, schema validation (ShEx and SHACL), conversions,
/// and visualization settings.
#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
pub struct RudofConfig {
pub(crate) rdf_data: Option<RdfDataConfig>,
pub(crate) shex: Option<ShExConfig>,
pub(crate) shex_validator: Option<ValidatorConfig>,
pub(crate) shex2uml: Option<ShEx2UmlConfig>,
pub(crate) shex2html: Option<ShEx2HtmlConfig>,
pub(crate) shacl2shex: Option<Shacl2ShExConfig>,
pub(crate) tap: Option<TapConfig>,
pub(crate) tap2shex: Option<Tap2ShExConfig>,
pub(crate) shex2sparql: Option<ShEx2SparqlConfig>,
pub(crate) service: Option<ServiceConfig>,
pub(crate) plantuml_path: Option<PathBuf>,
pub(crate) comparator: Option<ComparatorConfig>,
}
impl RudofConfig {
/// Creates a new `RudofConfig` with default settings.
pub fn new() -> Self {
RudofConfig::from_str(DEFAULT_CONFIG).unwrap()
}
/// Loads a `RudofConfig` from a TOML file.
///
/// # Arguments
///
/// * `path` - Path to the TOML configuration file
///
/// # Errors
///
/// * [`ConfigError::ReadFromPath`] - If the file cannot be opened or read
/// * [`ConfigError::TomlParseFromPath`] - If the TOML content is invalid
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
let path_name = path.as_ref().display().to_string();
let mut f = std::fs::File::open(path).map_err(|e| ConfigError::ReadFromPath {
path: path_name.clone(),
error: e,
})?;
let mut s = String::new();
f.read_to_string(&mut s).map_err(|e| ConfigError::ReadFromPath {
path: path_name.clone(),
error: e,
})?;
let config: RudofConfig = toml::from_str(s.as_str()).map_err(|e| ConfigError::TomlParseFromPath {
path: path_name.clone(),
error: e,
})?;
Ok(config)
}
// ---------------------------------------------------------------------------
// Dctap configuration
// ---------------------------------------------------------------------------
/// Returns the configuration for Dctap.
pub fn dctap_config(&self) -> TapConfig {
self.tap.clone().unwrap_or_default()
}
// ---------------------------------------------------------------------------
// ShEx configuration
// ---------------------------------------------------------------------------
/// Returns whether to show shape extends in ShEx schemas.
///
/// Defaults to `false` if not configured.
pub fn show_extends(&self) -> bool {
self.shex_config().show_extends.unwrap_or(false)
}
/// Returns whether to show imports in ShEx schemas.
///
/// Defaults to `false` if not configured.
pub fn show_imports(&self) -> bool {
self.shex_config().show_extends.unwrap_or(false)
}
/// Returns whether to show shapes in ShEx schemas.
///
/// Defaults to `false` if not configured.
pub fn show_shapes(&self) -> bool {
self.shex_config().show_shapes.unwrap_or(false)
}
/// Returns whether to show dependencies in ShEx schemas.
///
/// Defaults to `false` if not configured.
pub fn show_dependencies(&self) -> bool {
self.shex_config().show_dependencies.unwrap_or(false)
}
/// Returns whether to show the internal representation (IR) of ShEx schemas.
///
/// Defaults to `true` if not configured.
pub fn show_ir(&self) -> bool {
self.shex_config().show_ir.unwrap_or(true)
}
/// Disables statistics display in ShEx operations.
///
/// If no ShEx configuration exists, creates one with statistics disabled.
pub fn shex_without_showing_stats(&mut self) {
if let Some(shex_config) = &mut self.shex {
shex_config.without_showing_stats();
} else {
let mut shex_config = ShExConfig::default();
shex_config.without_showing_stats();
self.shex = Some(shex_config);
}
}
/// Returns the ShEx validator configuration.
///
/// Returns a default configuration if none was specified.
pub fn validator_config(&self) -> ValidatorConfig {
match &self.shex_validator {
None => ValidatorConfig::default(),
Some(cfg) => cfg.clone(),
}
}
// ---------------------------------------------------------------------------
// RDF data configuration
// ---------------------------------------------------------------------------
/// Returns the base IRI for RDF data, if configured.
///
/// Returns `None` if no base IRI is set in the configuration.
pub fn rdf_data_base(&self) -> Option<&str> {
match &self.rdf_data {
None => None,
Some(rdf_data_config) => rdf_data_config.base.as_ref().map(|i| i.as_str()),
}
}
/// Returns whether automatic base IRI detection is enabled.
///
/// Defaults to `true` if not configured.
pub fn automatic_base(&self) -> bool {
match &self.rdf_data {
None => true,
Some(rdf_data_config) => rdf_data_config.automatic_base.unwrap_or(true),
}
}
/// Sets the PlantUML executable path using the builder pattern.
///
/// # Arguments
///
/// * `path` - Path to the PlantUML executable or JAR file
pub fn with_plantuml_path<P: AsRef<Path>>(mut self, path: P) -> Self {
self.plantuml_path = Some(path.as_ref().to_owned());
self
}
/// Returns the path to the PlantUML executable.
///
/// The path is determined in the following order:
/// 1. The explicitly configured path via [`with_plantuml_path`](Self::with_plantuml_path)
/// 2. The `PLANTUML` environment variable
/// 3. The current working directory
pub fn plantuml_path(&self) -> PathBuf {
if let Some(path) = &self.plantuml_path {
path.to_owned()
} else {
match env::var("PLANTUML") {
Ok(value) => Path::new(value.as_str()).to_path_buf(),
Err(_) => env::current_dir().unwrap(),
}
}
}
// ---------------------------------------------------------------------------
// Conversion configurations
// ---------------------------------------------------------------------------
/// Returns the configuration for converting ShEx to SPARQL.
pub fn shex2sparql_config(&self) -> ShEx2SparqlConfig {
self.shex2sparql.clone().unwrap_or_default()
}
/// Returns the configuration for converting ShEx to UML.
pub fn shex2uml_config(&self) -> ShEx2UmlConfig {
self.shex2uml.clone().unwrap_or_default()
}
/// Returns the configuration for converting ShEx to HTML.
pub fn shex2html_config(&self) -> ShEx2HtmlConfig {
self.shex2html.clone().unwrap_or_default()
}
/// Returns the configuration for converting SHACL to ShEx.
pub fn shacl2shex_config(&self) -> Shacl2ShExConfig {
self.shacl2shex.clone().unwrap_or_default()
}
/// Returns the configuration for converting Dctap to ShEx.
pub fn tap2shex_config(&self) -> Tap2ShExConfig {
self.tap2shex.clone().unwrap_or_default()
}
// ---------------------------------------------------------------------------
// Comparison configuration
// ---------------------------------------------------------------------------
/// Returns the configuration used for comparing shapes.
pub fn comparator_config(&self) -> ComparatorConfig {
match self.comparator {
None => ComparatorConfig::new(),
Some(ref cfg) => cfg.clone(),
}
}
// ----------------------------------------------------------------------------
// Helper methods to access specific configurations with defaults
// ----------------------------------------------------------------------------
pub(crate) fn rdf_data_config(&self) -> RdfDataConfig {
self.rdf_data.clone().unwrap_or_default()
}
/// Returns the ShEx schema configuration.
///
/// Returns a default configuration if none was specified.
pub(crate) fn shex_config(&self) -> ShExConfig {
match &self.shex {
None => ShExConfig::default(),
Some(cfg) => cfg.clone(),
}
}
}
impl Default for RudofConfig {
fn default() -> Self {
Self::new()
}
}
impl FromStr for RudofConfig {
type Err = ConfigError;
/// Parses a `RudofConfig` from a TOML string.
///
/// # Errors
///
/// Returns [`ConfigError::TomlParseFromString`] if the TOML content is invalid.
fn from_str(s: &str) -> Result<Self, Self::Err> {
toml::from_str(s).map_err(|e| ConfigError::TomlParseFromString {
content: s.to_string(),
error: e,
})
}
}