shacl_validation/
shacl_config.rs1use srdf::RdfDataConfig;
2use std::io::Read;
3use std::{io, path::Path};
4use thiserror::Error;
5
6use serde::{Deserialize, Serialize};
7
8#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
10pub struct ShaclConfig {
11 pub data: Option<RdfDataConfig>,
12}
13
14impl ShaclConfig {
15 pub fn new() -> ShaclConfig {
16 Self {
17 data: Some(RdfDataConfig::default()),
18 }
19 }
20
21 pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ShaclConfig, ShaclConfigError> {
22 let path_name = path.as_ref().display().to_string();
23 let mut f =
24 std::fs::File::open(path).map_err(|e| ShaclConfigError::ReadingConfigError {
25 path_name: path_name.clone(),
26 error: e,
27 })?;
28 let mut s = String::new();
29 f.read_to_string(&mut s)
30 .map_err(|e| ShaclConfigError::ReadingConfigError {
31 path_name: path_name.clone(),
32 error: e,
33 })?;
34 let config: ShaclConfig =
35 toml::from_str(s.as_str()).map_err(|e| ShaclConfigError::TomlError {
36 path_name: path_name.to_string(),
37 error: e,
38 })?;
39 Ok(config)
40 }
41}
42
43impl Default for ShaclConfig {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49#[derive(Error, Debug)]
50pub enum ShaclConfigError {
51 #[error("Reading SHACL Config path {path_name:?} error: {error:?}")]
52 ReadingConfigError { path_name: String, error: io::Error },
53
54 #[error("Reading SHACL config TOML from {path_name:?}. Error: {error:?}")]
55 TomlError {
56 path_name: String,
57 error: toml::de::Error,
58 },
59}