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
use crate::{Error, Meta};
use std::{
    convert::{From, Into},
    fs,
    path::PathBuf,
};

/// The main struct in etc
pub struct Etc<'e> {
    /// root directory
    pub root: &'e PathBuf,
}

impl<'e> Etc<'e> {
    /// abstract an etc
    pub fn new(root: &'e PathBuf) -> Result<Etc, Error> {
        if !root.exists() {
            fs::create_dir(root)?;
        }

        Ok(Etc { root })
    }
}

impl<'m> Meta<'m> for Etc<'m> {
    fn real_path(&'m self) -> Result<PathBuf, Error> {
        Ok(self.root.to_owned())
    }
}

impl<'s> From<&'s PathBuf> for Etc<'s> {
    fn from(p: &'s PathBuf) -> Etc<'s> {
        Etc { root: p }
    }
}

impl<'s> Into<String> for Etc<'s> {
    fn into(self) -> String {
        self.root.to_string_lossy().into()
    }
}