Skip to main content

mothra/
fs.rs

1use std::fs::{create_dir};
2use std::path::{Path, PathBuf};
3use std::error::Error;
4
5use dirs;
6
7pub struct FilesManager {
8    pub home_dir: PathBuf,
9    pub full_path: PathBuf,
10}
11
12impl FilesManager {
13    pub fn new() -> Result<FilesManager, Box<dyn Error>> {
14        let home_dir_result = match dirs::home_dir() {
15            Some(dir) => Ok(Path::new(&dir).join(".mothra")),
16            None => Err("Path doesn't exist"),
17        };
18
19        let home_dir = home_dir_result?;
20        let full_path = home_dir.join("tasks.json");
21
22        Ok(FilesManager {
23            home_dir,
24            full_path,
25        })
26    }
27
28    pub fn create_mothra_dir(&self) -> Result<(), Box<dyn Error>> {
29        if !self.home_dir.exists() {
30            println!("Creating dir for: {}", self.home_dir.display());
31            create_dir(&self.home_dir)?;
32        }
33
34        Ok(())
35    }
36}