libaki_gsub/conf/
mod.rs

1pub use self::parse::parse_cmdopts;
2use crate::util::OptUcXParam;
3pub use parse::CmdOptConf;
4
5mod parse;
6
7impl CmdOptConf {
8    pub fn is_opt_uc_x_help(&self) -> bool {
9        for o in self.opt_uc_x.iter() {
10            if let OptUcXParam::Help = o {
11                return true;
12            }
13        }
14        false
15    }
16    pub fn is_opt_uc_x_package_version_info(&self) -> bool {
17        for o in self.opt_uc_x.iter() {
18            if let OptUcXParam::RustVersionInfo = o {
19                return true;
20            }
21        }
22        false
23    }
24}
25
26use regex::Regex;
27use std::env;
28
29#[derive(Debug)]
30pub struct RegexAndFormat {
31    pub regex: Regex,
32    pub format: String,
33}
34
35//
36// ref.) 3-bit and 4-bit color sequence
37//   https://en.wikipedia.org/wiki/ANSI_escape_code
38// * black letters on white background use: ESC[30;47m
39// * red use: ESC[31m
40// * bright red use: ESC[1;31m
41// * reset colors to their defaults: ESC[39;49m (not supported on some terminals)
42// * reset all attributes: ESC[0m
43//
44static COLOR_START: &str = "\u{1B}[1;31m";
45static COLOR_END: &str = "\u{1B}[0m";
46
47#[derive(Debug)]
48pub struct EnvConf {
49    pub color_seq_start: String,
50    pub color_seq_end: String,
51}
52impl EnvConf {
53    pub fn new() -> Self {
54        //
55        let a_color_seq_start = match env::var("AKI_GSUB_COLOR_SEQ_ST") {
56            Ok(v) => v,
57            Err(_) => String::from(COLOR_START),
58        };
59        let a_color_seq_end = match env::var("AKI_GSUB_COLOR_SEQ_ED") {
60            Ok(v) => v,
61            Err(_) => String::from(COLOR_END),
62        };
63        //
64        Self {
65            color_seq_start: a_color_seq_start,
66            color_seq_end: a_color_seq_end,
67        }
68    }
69    pub fn from_array(ary: &[(&str, &str)]) -> Self {
70        let mut r = Self::new();
71        for a in ary {
72            match a.0 {
73                "AKI_GSUB_COLOR_SEQ_ST" => {
74                    r.color_seq_start = a.1.to_string();
75                }
76                "AKI_GSUB_COLOR_SEQ_ED" => {
77                    r.color_seq_end = a.1.to_string();
78                }
79                _ => (),
80            }
81        }
82        r
83    }
84}
85impl std::default::Default for EnvConf {
86    fn default() -> EnvConf {
87        EnvConf::new()
88    }
89}
90
91impl<IKV, K, V> From<IKV> for EnvConf
92where
93    IKV: IntoIterator<Item = (K, V)>,
94    K: AsRef<std::ffi::OsStr>,
95    V: AsRef<std::ffi::OsStr>,
96{
97    fn from(ary: IKV) -> Self {
98        let mut r = Self::new();
99        for a in ary {
100            match a.0.as_ref().to_string_lossy().to_string().as_str() {
101                "AKI_GSUB_COLOR_SEQ_ST" => {
102                    r.color_seq_start = a.1.as_ref().to_string_lossy().to_string();
103                }
104                "AKI_GSUB_COLOR_SEQ_ED" => {
105                    r.color_seq_end = a.1.as_ref().to_string_lossy().to_string();
106                }
107                _ => (),
108            }
109        }
110        r
111    }
112}
113
114#[cfg(test)]
115mod test {
116    use super::*;
117    //
118    #[test]
119    fn test_cmpoptconf_default() {
120        let conf = CmdOptConf::default();
121        assert!(!conf.is_opt_uc_x_help());
122        assert!(!conf.is_opt_uc_x_package_version_info());
123    }
124    #[test]
125    fn test_envconf_default() {
126        let env = EnvConf::default();
127        assert_eq!(env.color_seq_start, COLOR_START);
128        assert_eq!(env.color_seq_end, COLOR_END);
129    }
130}