aboutsummaryrefslogtreecommitdiffstats
path: root/src/dirs.rs
blob: 44f828ba083d1f4821ec84c577e2b7a89594d492 (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
39
40
41
42
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<()> {
        let filename = self.data_dir();
        std::fs::create_dir_all(filename).with_context(|| {
            crate::error::CreateDir {
                filename: filename.to_string_lossy(),
            }
        })?;
        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)
    }
}