shapes_converter/shex_to_uml/
shex2uml_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
81
82
83
84
85
86
87
88
89
90
91
use std::{
    env::{self, VarError},
    fs, io,
    path::{Path, PathBuf},
};

use iri_s::IriS;
use serde::{Deserialize, Serialize};
use shex_validation::ShExConfig;
use srdf::RDFS_LABEL_STR;
use thiserror::Error;

/// Name of Environment variable where we search for plantuml JAR
pub const PLANTUML: &str = "PLANTUML";

pub const DEFAULT_REPLACE_IRI_BY_LABEL: bool = true;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct ShEx2UmlConfig {
    pub plantuml_path: Option<PathBuf>,
    pub annotation_label: Vec<IriS>,
    pub replace_iri_by_label: Option<bool>,
    pub shex: Option<ShExConfig>,
}

impl ShEx2UmlConfig {
    pub fn new() -> ShEx2UmlConfig {
        let plantuml_path = match env::var(PLANTUML) {
            Ok(value) => Some(Path::new(value.as_str()).to_path_buf()),
            Err(_) => None,
        };
        Self {
            plantuml_path,
            annotation_label: vec![IriS::new_unchecked(RDFS_LABEL_STR)],
            replace_iri_by_label: None,
            shex: Some(ShExConfig::default()),
        }
    }

    pub fn shex_config(&self) -> ShExConfig {
        match &self.shex {
            None => ShExConfig::default(),
            Some(sc) => sc.clone(),
        }
    }

    pub fn replace_iri_by_label(&self) -> bool {
        self.replace_iri_by_label
            .unwrap_or(DEFAULT_REPLACE_IRI_BY_LABEL)
    }

    pub fn from_file(file_name: &str) -> Result<ShEx2UmlConfig, ShEx2UmlConfigError> {
        let config_str =
            fs::read_to_string(file_name).map_err(|e| ShEx2UmlConfigError::ReadingConfigError {
                path_name: file_name.to_string(),
                error: e,
            })?;
        serde_yml::from_str::<ShEx2UmlConfig>(&config_str).map_err(|e| {
            ShEx2UmlConfigError::YamlError {
                path_name: file_name.to_string(),
                error: e,
            }
        })
    }

    pub fn with_plantuml_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.plantuml_path = Some(path.as_ref().to_owned());
        self
    }
}

#[derive(Error, Debug)]
pub enum ShEx2UmlConfigError {
    #[error("Reading path {path_name:?} error: {error:?}")]
    ReadingConfigError { path_name: String, error: io::Error },

    #[error("Reading YAML from {path_name:?}. Error: {error:?}")]
    YamlError {
        path_name: String,
        error: serde_yml::Error,
    },

    #[error("Accessing environment variable {var_name}: {error}")]
    EnvVarError { var_name: String, error: VarError },
}

impl Default for ShEx2UmlConfig {
    fn default() -> Self {
        Self::new()
    }
}