sparql_service/
query_config.rs

1use serde::{Deserialize, Serialize};
2use srdf::RdfDataConfig;
3use std::io::Read;
4use std::{io, path::Path};
5use thiserror::Error;
6
7/// This struct can be used to define configuration of RDF data readers
8#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
9pub struct QueryConfig {
10    /// Default base to resolve relative IRIs, if it is `None` relative IRIs will be marked as errors`
11    pub data_config: Option<RdfDataConfig>,
12}
13
14impl QueryConfig {
15    pub fn new() -> QueryConfig {
16        Self {
17            data_config: Some(RdfDataConfig::default()),
18        }
19    }
20
21    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<QueryConfig, QueryConfigError> {
22        let path_name = path.as_ref().display().to_string();
23        let mut f =
24            std::fs::File::open(path).map_err(|e| QueryConfigError::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| QueryConfigError::ReadingConfigError {
31                path_name: path_name.clone(),
32                error: e,
33            })?;
34        let config: QueryConfig =
35            toml::from_str(s.as_str()).map_err(|e| QueryConfigError::TomlError {
36                path_name: path_name.to_string(),
37                error: e,
38            })?;
39        Ok(config)
40    }
41}
42
43impl Default for QueryConfig {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49#[derive(Error, Debug)]
50pub enum QueryConfigError {
51    #[error("Reading path {path_name:?} error: {error:?}")]
52    ReadingConfigError { path_name: String, error: io::Error },
53
54    #[error("Reading TOML from {path_name:?}. Error: {error:?}")]
55    TomlError {
56        path_name: String,
57        error: toml::de::Error,
58    },
59}