implement index file builder

This commit is contained in:
yggverse 2025-02-11 23:34:57 +02:00
parent 120876fef3
commit 4d4c78a4fc
3 changed files with 128 additions and 60 deletions

View file

@ -1,25 +1,19 @@
use std::error::Error;
use std::path::MAIN_SEPARATOR;
pub struct Path {
pub absolute: String,
pub directory: String,
pub file: String,
file: String,
path: String,
}
impl Path {
// Constructors
pub fn build(base: &str, pub_date: &str, mkdir: bool) -> Result<Path, Box<dyn Error>> {
use chrono::{DateTime, Datelike, Timelike};
use std::{fs::create_dir_all, path::MAIN_SEPARATOR};
let date_time = DateTime::parse_from_rfc2822(pub_date)?;
let directory = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
date_time.year(),
date_time.month(),
date_time.day()
);
let file = format!(
"{:02}-{:02}-{:02}.gmi",
date_time.hour(),
@ -27,14 +21,31 @@ impl Path {
date_time.second()
);
let path = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
date_time.year(),
date_time.month(),
date_time.day()
);
if mkdir {
create_dir_all(&directory)?;
std::fs::create_dir_all(&path)?;
}
Ok(Path {
absolute: format!("{directory}{MAIN_SEPARATOR}{file}"),
directory,
file,
})
Ok(Path { file, path })
}
// Getters
pub fn index(&self) -> String {
format!("{}{MAIN_SEPARATOR}index.gmi", self.path)
}
pub fn filepath(&self) -> String {
format!("{}{MAIN_SEPARATOR}{}", self.path, self.file)
}
pub fn filename(&self) -> &str {
&self.file
}
}