shapemap/
shapemap_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::path::Path;

use colored::*;
use prefixmap::PrefixMap;
use serde_derive::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)]

pub struct ShapemapConfigMain {
    /// Specific shapemap configuration
    pub shex: Option<ShapemapConfig>,
}

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

#[derive(Deserialize, Serialize, Debug, PartialEq, Clone)]

pub struct ShapemapConfig {
    nodes_prefixmap: PrefixMap,
    shapes_prefixmap: PrefixMap,

    // TODO: Color doesn't implement Serialize/Deserialize...
    #[serde(skip)]
    ok_color: Option<Color>,

    #[serde(skip)]
    fail_color: Option<Color>,

    #[serde(skip)]
    pending_color: Option<Color>,
}

impl Default for ShapemapConfig {
    fn default() -> Self {
        Self {
            nodes_prefixmap: Default::default(),
            shapes_prefixmap: Default::default(),
            ok_color: Some(Color::Green),
            fail_color: Some(Color::Red),
            pending_color: Some(Color::Magenta),
        }
    }
}

impl ShapemapConfig {
    pub fn ok_color(&self) -> Option<Color> {
        self.ok_color
    }
    pub fn fail_color(&self) -> Option<Color> {
        self.fail_color
    }
    pub fn pending_color(&self) -> Option<Color> {
        self.pending_color
    }
    pub fn set_ok_color(&mut self, color: Color) {
        self.ok_color = Some(color);
    }

    pub fn set_fail_color(&mut self, color: Color) {
        self.fail_color = Some(color);
    }

    pub fn set_pending_color(&mut self, color: Color) {
        self.pending_color = Some(color)
    }

    pub fn nodes_prefixmap(&self) -> PrefixMap {
        self.nodes_prefixmap.clone()
    }

    pub fn shapes_prefixmap(&self) -> PrefixMap {
        self.shapes_prefixmap.clone()
    }

    pub fn with_nodes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
        self.nodes_prefixmap = prefixmap.clone();
        self
    }

    pub fn with_shapes_prefixmap(mut self, prefixmap: &PrefixMap) -> Self {
        self.shapes_prefixmap = prefixmap.clone();
        self
    }
}

#[derive(Error, Debug, Clone)]
pub enum ShapemapConfigError {
    #[error("Error reading config file from path {path}: {error}")]
    FromPathError { path: String, error: String },

    #[error("Error reading config file from path {path}: {error}")]
    YamlError { path: String, error: String },
}