shapes_converter/
converter_config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::path::Path;

use dctap::TapConfig;
use serde_derive::{Deserialize, Serialize};

use crate::{
    ConverterError, ShEx2HtmlConfig, ShEx2SparqlConfig, ShEx2UmlConfig, Shacl2ShExConfig,
    Tap2ShExConfig,
};

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
pub struct ConverterConfig {
    dctap: Option<TapConfig>,
    shex2html: Option<ShEx2HtmlConfig>,
    tap2shex: Option<Tap2ShExConfig>,
    shex2sparql: Option<ShEx2SparqlConfig>,
    shacl2shex: Option<Shacl2ShExConfig>,
    shex2uml: Option<ShEx2UmlConfig>,
}

impl ConverterConfig {
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ConverterConfig, ConverterError> {
        let path_name = path.as_ref().display().to_string();
        let f = std::fs::File::open(path).map_err(|e| {
            ConverterError::ConverterConfigFromPathError {
                path: path_name.clone(),
                error: e,
            }
        })?;
        let config: ConverterConfig = serde_yml::from_reader(f).map_err(|e| {
            ConverterError::ConverterConfigFromYAMLError {
                path: path_name.clone(),
                error: e,
            }
        })?;
        Ok(config)
    }

    pub fn tap_config(&self) -> TapConfig {
        match &self.dctap {
            Some(tc) => tc.clone(),
            None => TapConfig::default(),
        }
    }

    pub fn tap2shex_config(&self) -> Tap2ShExConfig {
        match &self.tap2shex {
            Some(c) => c.clone(),
            None => Tap2ShExConfig::default(),
        }
    }

    pub fn shex2html_config(&self) -> ShEx2HtmlConfig {
        match &self.shex2html {
            Some(c) => c.clone(),
            None => ShEx2HtmlConfig::default(),
        }
    }

    pub fn shex2uml_config(&self) -> ShEx2UmlConfig {
        match &self.shex2uml {
            Some(c) => c.clone(),
            None => ShEx2UmlConfig::default(),
        }
    }

    pub fn shacl2shex_config(&self) -> Shacl2ShExConfig {
        match &self.shacl2shex {
            Some(c) => c.clone(),
            None => Shacl2ShExConfig::default(),
        }
    }

    pub fn shex2sparql_config(&self) -> ShEx2SparqlConfig {
        match &self.shex2sparql {
            Some(c) => c.clone(),
            None => ShEx2SparqlConfig::default(),
        }
    }
}