pulsarss/src/path.rs
2025-02-12 01:31:25 +02:00

47 lines
1 KiB
Rust

use std::error::Error;
use std::path::MAIN_SEPARATOR;
pub struct Path {
pub item: String,
pub 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};
let time = DateTime::parse_from_rfc2822(pub_date)?;
let item = format!(
"{:02}-{:02}-{:02}.gmi",
time.hour(),
time.minute(),
time.second()
);
let path = format!(
"{base}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}{MAIN_SEPARATOR}{:02}",
time.year(),
time.month(),
time.day()
);
if mkdir {
std::fs::create_dir_all(&path)?;
}
Ok(Path { item, path })
}
// Getters
pub fn index(&self) -> String {
format!("{}{MAIN_SEPARATOR}index.gmi", self.path)
}
pub fn item(&self) -> String {
format!("{}{MAIN_SEPARATOR}{}", self.path, self.item)
}
}