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
use crate::config::{read, Conf};
use indexmap::IndexMap;
use nu_protocol::Value;
use nu_source::Tag;
use parking_lot::Mutex;
use std::fmt::Debug;
use std::sync::Arc;

#[derive(Debug, Clone, Default)]
pub struct NuConfig {
    pub vars: Arc<Mutex<IndexMap<String, Value>>>,
}

impl Conf for NuConfig {
    fn env(&self) -> Option<Value> {
        self.env()
    }

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

    fn reload(&self) {
        let mut vars = self.vars.lock();

        if let Ok(variables) = read(Tag::unknown(), &None) {
            vars.extend(variables);
        }
    }

    fn clone_box(&self) -> Box<dyn Conf> {
        Box::new(self.clone())
    }
}

impl NuConfig {
    pub fn new() -> NuConfig {
        let vars = if let Ok(variables) = read(Tag::unknown(), &None) {
            variables
        } else {
            IndexMap::default()
        };

        NuConfig {
            vars: Arc::new(Mutex::new(vars)),
        }
    }

    pub fn env(&self) -> Option<Value> {
        let vars = self.vars.lock();

        if let Some(env_vars) = vars.get("env") {
            return Some(env_vars.clone());
        }

        None
    }

    pub fn path(&self) -> Option<Value> {
        let vars = self.vars.lock();

        if let Some(env_vars) = vars.get("path") {
            return Some(env_vars.clone());
        }

        None
    }
}