create Path mod implementation

This commit is contained in:
yggverse 2025-02-11 21:16:43 +02:00
parent 3fefd038f1
commit 120876fef3
2 changed files with 45 additions and 30 deletions

View file

@ -1,5 +1,6 @@
mod argument;
mod output;
mod path;
use output::Output;
use std::error::Error;
@ -21,6 +22,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Error>> {
use path::Path;
use reqwest::blocking::get;
use rss::Channel;
use std::{
@ -43,8 +45,8 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Erro
let path = match item.pub_date() {
Some(pub_data) => {
let path = path(target, pub_data, true)?;
if metadata(&path).is_ok() {
let path = Path::build(target, pub_data, true)?;
if metadata(&path.absolute).is_ok() {
exist += 1;
continue; // skip existing records
}
@ -78,7 +80,7 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Erro
}
// record item to static file
File::create(&path)?.write_all(data.join("\n\n").as_bytes())?;
File::create(&path.absolute)?.write_all(data.join("\n\n").as_bytes())?;
}
output.debug(&format!(
@ -88,30 +90,3 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Erro
Ok(())
}
fn path(base: &str, pub_date: &str, mkdir: bool) -> Result<String, 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 dir = 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(&dir)?;
}
Ok(format!("{dir}{MAIN_SEPARATOR}{file}"))
}

40
src/path.rs Normal file
View 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,
})
}
}