shex_validation/
validator_config.rs

1use serde::{Deserialize, Serialize};
2use shapemap::ShapemapConfig;
3use srdf::RdfDataConfig;
4use std::io::Read;
5use std::path::Path;
6
7use crate::{ShExConfig, ValidatorError, MAX_STEPS};
8
9/// This struct can be used to customize the behavour of ShEx validators
10#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
11
12pub struct ValidatorConfig {
13    /// Maximum numbers of validation steps
14    pub max_steps: usize,
15
16    /// Configuration of RDF data readers
17    pub rdf_data: Option<RdfDataConfig>,
18
19    /// Configuration of ShEx schemas
20    pub shex: Option<ShExConfig>,
21
22    /// Configuration of Shapemaps
23    pub shapemap: Option<ShapemapConfig>,
24
25    pub check_negation_requirement: Option<bool>,
26}
27
28impl Default for ValidatorConfig {
29    fn default() -> Self {
30        Self {
31            max_steps: MAX_STEPS,
32            rdf_data: Some(RdfDataConfig::default()),
33            shex: Some(ShExConfig::default()),
34            shapemap: Some(ShapemapConfig::default()),
35            check_negation_requirement: Some(true),
36        }
37    }
38}
39
40impl ValidatorConfig {
41    /// Obtain a `ValidatorConfig` from a path file in TOML format
42    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ValidatorConfig, ValidatorError> {
43        let path_name = path.as_ref().display().to_string();
44        let mut f = std::fs::File::open(path).map_err(|e| {
45            ValidatorError::ValidatorConfigFromPathError {
46                path: path_name.clone(),
47                error: e.to_string(),
48            }
49        })?;
50        let mut s = String::new();
51        f.read_to_string(&mut s)
52            .map_err(|e| ValidatorError::ValidatorConfigFromPathError {
53                path: path_name.clone(),
54                error: e.to_string(),
55            })?;
56
57        let config: ValidatorConfig =
58            toml::from_str(s.as_str()).map_err(|e| ValidatorError::ValidatorConfigTomlError {
59                path: path_name.clone(),
60                error: e.to_string(),
61            })?;
62        Ok(config)
63    }
64
65    pub fn set_max_steps(&mut self, max_steps: usize) {
66        self.max_steps = max_steps;
67    }
68
69    pub fn max_steps(&self) -> usize {
70        self.max_steps
71    }
72
73    pub fn rdf_data_config(&self) -> RdfDataConfig {
74        match &self.rdf_data {
75            None => RdfDataConfig::default(),
76            Some(sc) => sc.clone(),
77        }
78    }
79
80    pub fn shex_config(&self) -> ShExConfig {
81        match &self.shex {
82            None => ShExConfig::default(),
83            Some(sc) => sc.clone(),
84        }
85    }
86
87    pub fn shapemap_config(&self) -> ShapemapConfig {
88        match &self.shapemap {
89            None => ShapemapConfig::default(),
90            Some(sc) => sc.clone(),
91        }
92    }
93}