From 120876fef3103e1f685cf7af55c46b699558395a Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 11 Feb 2025 21:16:43 +0200 Subject: [PATCH] create Path mod implementation --- src/main.rs | 35 +++++------------------------------ src/path.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 src/path.rs diff --git a/src/main.rs b/src/main.rs index e230598..b81894e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { } fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box> { + 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 { - 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 Result<(), Box Result> { - 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}")) -} diff --git a/src/path.rs b/src/path.rs new file mode 100644 index 0000000..d987f15 --- /dev/null +++ b/src/path.rs @@ -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> { + 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, + }) + } +}