shapemap/
shapemap_config.rs

1use std::io::Read;
2use std::path::Path;
3
4use colored::*;
5use prefixmap::PrefixMap;
6use serde::{Deserialize, Serialize};
7use thiserror::Error;
8
9#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]
10
11pub struct ShapemapConfigMain {
12    /// Specific shapemap configuration
13    pub shex: Option<ShapemapConfig>,
14}
15
16impl ShapemapConfigMain {
17    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<ShapemapConfigMain, ShapemapConfigError> {
18        let path_name = path.as_ref().display().to_string();
19        let mut f = std::fs::File::open(path).map_err(|e| ShapemapConfigError::FromPathError {
20            path: path_name.clone(),
21            error: e.to_string(),
22        })?;
23        let mut s = String::new();
24        f.read_to_string(&mut s)
25            .map_err(|e| ShapemapConfigError::FromFileError {
26                file: path_name.clone(),
27                error: e.to_string(),
28            })?;
29
30        let config: ShapemapConfigMain =
31            toml::from_str(s.as_str()).map_err(|e| ShapemapConfigError::TomlError {
32                path: path_name.clone(),
33                error: e.to_string(),
34            })?;
35        Ok(config)
36    }
37}
38
39#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]
40
41pub struct ShapemapConfig {
42    nodes_prefixmap: PrefixMap,
43    shapes_prefixmap: PrefixMap,
44
45    // TODO: Color doesn't implement Serialize/Deserialize...
46    #[serde(skip)]
47    ok_color: Option<Color>,
48
49    #[serde(skip)]
50    fail_color: Option<Color>,
51
52    #[serde(skip)]
53    pending_color: Option<Color>,
54}
55
56impl Default for ShapemapConfig {
57    fn default() -> Self {
58        Self {
59            nodes_prefixmap: Default::default(),
60            shapes_prefixmap: Default::default(),
61            ok_color: Some(Color::Green),
62            fail_color: Some(Color::Red),
63            pending_color: Some(Color::Magenta),
64        }
65    }
66}
67
68impl ShapemapConfig {
69    pub fn ok_color(&self) -> Option<Color> {
70        self.ok_color
71    }
72    pub fn fail_color(&self) -> Option<Color> {
73        self.fail_color
74    }
75    pub fn pending_color(&self) -> Option<Color> {
76        self.pending_color
77    }
78    pub fn set_ok_color(&mut self, color: Color) {
79        self.ok_color = Some(color);
80    }
81
82    pub fn set_fail_color(&mut self, color: Color) {
83        self.fail_color = Some(color);
84    }
85
86    pub fn set_pending_color(&mut self, color: Color) {
87        self.pending_color = Some(color)
88    }
89
90    pub fn nodes_prefixmap(&self) -> PrefixMap {
91        self.nodes_prefixmap.clone()
92    }
93
94    pub fn shapes_prefixmap(&self) -> PrefixMap {
95        self.shapes_prefixmap.clone()
96    }
97
98    pub fn with_nodes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
99        self.nodes_prefixmap = prefixmap.clone();
100        self
101    }
102
103    pub fn with_shapes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
104        self.shapes_prefixmap = prefixmap.clone();
105        self
106    }
107}
108
109#[derive(Error, Debug, Clone)]
110pub enum ShapemapConfigError {
111    #[error("Error reading config file from path {path}: {error}")]
112    FromPathError { path: String, error: String },
113
114    #[error("Error reading config file from file {file}: {error}")]
115    FromFileError { file: String, error: String },
116
117    #[error("Error reading config file from path {path}: {error}")]
118    TomlError { path: String, error: String },
119}