diary/utils/
file_system.rs

1use std::{
2    fs::create_dir,
3    path::{Path, PathBuf},
4};
5
6use chrono::{DateTime, Local};
7
8use crate::errors::DiaryError;
9
10pub fn month_folder(path_root: &Path, date: &DateTime<Local>) -> PathBuf {
11    let month_folder = PathBuf::from(date.format("%Y-%m").to_string());
12    [path_root, &month_folder].iter().collect()
13}
14
15pub fn create_month_folder(path: &Path) -> Result<(), DiaryError> {
16    if path.exists() {
17        Ok(())
18    } else {
19        match create_dir(path) {
20            Ok(_) => Ok(()),
21            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
22                Err(DiaryError::UnInitialised { source: Some(e) })
23            }
24            Err(e) => panic!("Unexpected IO error {}", e), // uncovered.
25        }
26    }
27}