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
use crate::config::{Conf, NuConfig, Status};
use nu_protocol::Value;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct FakeConfig {
    pub config: NuConfig,
    source_file: Option<PathBuf>,
}

impl Conf for FakeConfig {
    fn is_modified(&self) -> Result<bool, Box<dyn std::error::Error>> {
        self.is_modified()
    }

    fn var(&self, key: &str) -> Option<Value> {
        self.config.var(key)
    }

    fn env(&self) -> Option<Value> {
        self.config.env()
    }

    fn path(&self) -> Option<Value> {
        self.config.path()
    }

    fn reload(&mut self) {
        self.reload()
    }

    fn clone_box(&self) -> Box<dyn Conf> {
        self.config.clone_box()
    }
}

impl FakeConfig {
    pub fn new(config_file: &Path) -> FakeConfig {
        let config_file = Some(PathBuf::from(config_file));

        FakeConfig {
            config: NuConfig::with(config_file.clone()),
            source_file: config_file,
        }
    }

    pub fn is_modified(&self) -> Result<bool, Box<dyn std::error::Error>> {
        let modified_at = &self.config.modified_at;

        Ok(
            match (NuConfig::get_last_modified(&self.source_file), modified_at) {
                (Status::LastModified(left), Status::LastModified(right)) => {
                    let left = left.duration_since(std::time::UNIX_EPOCH)?;
                    let right = (*right).duration_since(std::time::UNIX_EPOCH)?;

                    left != right
                }
                (_, _) => false,
            },
        )
    }

    pub fn reload(&mut self) {
        self.config = NuConfig::with(self.source_file.clone());
    }
}