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