shapes_converter/shex_to_sparql/
shex2sparql_config.rs

1use std::{fs, io};
2
3use serde::{Deserialize, Serialize};
4use shex_validation::ShExConfig;
5use thiserror::Error;
6
7#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
8pub struct ShEx2SparqlConfig {
9    pub this_variable_name: String,
10    pub shex: Option<ShExConfig>,
11}
12
13impl Default for ShEx2SparqlConfig {
14    fn default() -> Self {
15        Self {
16            this_variable_name: "this".to_string(),
17            shex: Some(ShExConfig::default()),
18        }
19    }
20}
21
22impl ShEx2SparqlConfig {
23    pub fn from_file(file_name: &str) -> Result<ShEx2SparqlConfig, ShEx2SparqlConfigError> {
24        let config_str = fs::read_to_string(file_name).map_err(|e| {
25            ShEx2SparqlConfigError::ReadingConfigError {
26                path_name: file_name.to_string(),
27                error: e,
28            }
29        })?;
30        toml::from_str::<ShEx2SparqlConfig>(&config_str).map_err(|e| {
31            ShEx2SparqlConfigError::TomlError {
32                path_name: file_name.to_string(),
33                error: e,
34            }
35        })
36    }
37
38    /// Get the ShExConfig if it has been declared or the default one
39    pub fn shex_config(&self) -> ShExConfig {
40        match &self.shex {
41            None => ShExConfig::default(),
42            Some(sc) => sc.clone(),
43        }
44    }
45}
46
47#[derive(Error, Debug)]
48pub enum ShEx2SparqlConfigError {
49    #[error("Reading path {path_name:?} error: {error:?}")]
50    ReadingConfigError { path_name: String, error: io::Error },
51
52    #[error("Reading TOML from {path_name:?}. Error: {error:?}")]
53    TomlError {
54        path_name: String,
55        error: toml::de::Error,
56    },
57}