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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::error::{Error, Result};
use crate::path;
use crate::script_type::{ScriptType, ScriptTypeConfig};
use crate::tag::{TagControlFlow, TagFilter, TagFilterGroup};
use crate::util;
use chrono::{DateTime, Utc};
use colored::Color;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ops::DerefMut;
use std::path::PathBuf;
use std::str::FromStr;

const CONFIG_FILE: &'static str = ".config.toml";
lazy_static::lazy_static! {
    static ref CONFIG: Result<Config> = RawConfig::load().map(|c| {
        Config {
            changed: false,
            raw_config: c,
            open_time: Utc::now(),
        }
    });
}

fn config_file() -> PathBuf {
    path::get_home().join(CONFIG_FILE)
}

fn is_false(t: &bool) -> bool {
    !t
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct NamedTagFilter {
    pub filter: TagControlFlow,
    #[serde(default, skip_serializing_if = "is_false")]
    pub obligation: bool,
    pub name: String,
}

#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct Alias {
    pub after: Vec<String>,
}
impl From<Vec<String>> for Alias {
    fn from(after: Vec<String>) -> Self {
        Alias { after }
    }
}

fn gen_alias(from: &str, after: &[&str]) -> (String, Alias) {
    (
        from.to_owned(),
        Alias {
            after: after.iter().map(|s| s.to_string()).collect(),
        },
    )
}

#[derive(Deserialize, Serialize, PartialEq, Eq, Debug, Clone)]
pub struct RawConfig {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recent: Option<u32>,
    pub main_tag_filter: TagFilter,
    pub tag_filters: Vec<NamedTagFilter>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub alias: HashMap<String, Alias>,
    pub categories: HashMap<ScriptType, ScriptTypeConfig>,
}
#[derive(Debug, Clone, Deref)]
pub struct Config {
    changed: bool,
    open_time: DateTime<Utc>,
    #[deref]
    raw_config: RawConfig,
}
impl Default for RawConfig {
    fn default() -> Self {
        RawConfig {
            tag_filters: vec![
                NamedTagFilter {
                    filter: FromStr::from_str("+pin,util").unwrap(),
                    obligation: false,
                    name: "pin".to_owned(),
                },
                NamedTagFilter {
                    filter: FromStr::from_str("+all,^hide").unwrap(),
                    obligation: true,
                    name: "no-hidden".to_owned(),
                },
                NamedTagFilter {
                    filter: FromStr::from_str("+all,^removed").unwrap(),
                    obligation: true,
                    name: "no-removed".to_owned(),
                },
            ],
            main_tag_filter: FromStr::from_str("+all").unwrap(),
            categories: ScriptTypeConfig::default_script_types(),
            alias: vec![
                // FIXME: 一旦陣列實作了 intoiterator 就用陣列
                gen_alias("la", &["ls", "-a"]),
                gen_alias("ll", &["ls", "-l"]),
                gen_alias("!", &["-a", "-"]),
                gen_alias("l", &["ls"]),
                gen_alias("e", &["edit"]),
                gen_alias("gc", &["rm", "--purge", "-f", "removed", "*"]),
                gen_alias("t", &["ls", "--grouping", "tree"]),
            ]
            .into_iter()
            .collect(),
            recent: Some(999999), // NOTE: 顯示兩千多年份的資料!
        }
    }
}
impl RawConfig {
    #[cfg(test)]
    fn load() -> Result<Self> {
        Ok(Self::default())
    }
    #[cfg(not(test))]
    fn load() -> Result<Self> {
        use crate::error::FormatCode;

        let path = config_file();
        log::info!("載入設定檔:{:?}", path);
        match util::read_file(&path) {
            Ok(s) => toml::from_str(&s)
                .map_err(|_| Error::Format(FormatCode::Config, path.to_string_lossy().into())),
            Err(Error::PathNotFound(_)) => {
                log::debug!("找不到設定檔,使用預設值");
                Ok(Default::default())
            }
            Err(e) => Err(e),
        }
    }
}
impl DerefMut for Config {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.changed = true;
        &mut self.raw_config
    }
}
impl Config {
    pub fn store(&self) -> Result<()> {
        log::info!("寫入設定檔…");
        let path = config_file();
        if !self.changed && path.exists() {
            log::info!("設定檔未改變,不寫入");
            return Ok(());
        }
        match util::handle_fs_res(&[&path], std::fs::metadata(&path)) {
            Ok(meta) => {
                let modified = util::handle_fs_res(&[&path], meta.modified())?;
                let modified = modified.duration_since(std::time::UNIX_EPOCH)?.as_secs();
                if self.open_time.timestamp() < modified as i64 {
                    log::info!("設定檔已被修改,不寫入");
                    return Ok(());
                }
            }
            Err(Error::PathNotFound(_)) => {
                log::debug!("設定檔不存在,寫就對了");
            }
            Err(err) => return Err(err),
        }
        util::write_file(&path, &toml::to_string_pretty(&**self)?)
    }
    pub fn get() -> Result<&'static Config> {
        match &*CONFIG {
            Ok(c) => Ok(c),
            Err(e) => Err(e.clone()),
        }
    }
    pub fn get_color(&self, ty: &ScriptType) -> Result<Color> {
        let c = self.get_script_conf(ty)?.color.as_str();
        Ok(Color::from(c))
    }
    pub fn get_script_conf(&self, ty: &ScriptType) -> Result<&ScriptTypeConfig> {
        self.categories
            .get(ty)
            .ok_or(Error::UnknownCategory(ty.to_string()))
    }
    pub fn get_tag_filter_group(&self) -> TagFilterGroup {
        let mut group = TagFilterGroup::default();
        for f in self.tag_filters.iter() {
            group.push(TagFilter {
                obligation: f.obligation,
                filter: f.filter.clone(),
            });
        }
        group.push(self.main_tag_filter.clone());
        group
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use toml::{from_str, to_string_pretty};
    #[test]
    fn test_config_serde() {
        path::set_path_from_sys().unwrap();
        let c1 = RawConfig {
            main_tag_filter: FromStr::from_str("a,^b,c").unwrap(),
            ..Default::default()
        };
        let s = to_string_pretty(&c1).unwrap();
        let c2: RawConfig = from_str(&s).unwrap();
        assert_eq!(c1, c2);
    }
}