mirror of
https://github.com/YGGverse/pulsarss.git
synced 2026-04-01 01:25:30 +00:00
create Path mod implementation
This commit is contained in:
parent
3fefd038f1
commit
120876fef3
2 changed files with 45 additions and 30 deletions
40
src/path.rs
Normal file
40
src/path.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use std::error::Error;
|
||||
|
||||
pub struct Path {
|
||||
pub absolute: String,
|
||||
pub directory: String,
|
||||
pub file: String,
|
||||
}
|
||||
|
||||
impl Path {
|
||||
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(),
|
||||
date_time.minute(),
|
||||
date_time.second()
|
||||
);
|
||||
|
||||
if mkdir {
|
||||
create_dir_all(&directory)?;
|
||||
}
|
||||
|
||||
Ok(Path {
|
||||
absolute: format!("{directory}{MAIN_SEPARATOR}{file}"),
|
||||
directory,
|
||||
file,
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue