aboutsummaryrefslogtreecommitdiffstats
path: root/src/dirs.rs
blob: d868a4c4eab530504ec7347becbbb419b284a080 (plain) (blame)
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
use crate::prelude::*;

pub struct Dirs {
    project_dirs: directories::ProjectDirs,
}

impl Dirs {
    pub fn new() -> Self {
        // TODO: fall back to /var, /etc, etc if we're running without $HOME
        // set
        Self {
            project_dirs: directories::ProjectDirs::from("", "", "teleterm")
                .expect("failed to find valid home directory"),
        }
    }

    pub fn create_all(&self) -> Result<()> {
        std::fs::create_dir_all(self.data_dir())
            .context(crate::error::CreateDir)?;
        Ok(())
    }

    fn config_dir(&self) -> &std::path::Path {
        self.project_dirs.config_dir()
    }

    pub fn config_file(&self, name: &str) -> std::path::PathBuf {
        self.config_dir().join(name)
    }

    fn data_dir(&self) -> &std::path::Path {
        self.project_dirs.data_dir()
    }

    pub fn data_file(&self, name: &str) -> std::path::PathBuf {
        self.data_dir().join(name)
    }
}