From 3de2959e12dbc52c80487c327cd43422a7e5da7d Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Thu, 15 Jun 2023 08:57:19 -0400 Subject: [PATCH] seperated module into new file --- src/main.rs | 77 ++------------------------------------------ src/todo_file/mod.rs | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 74 deletions(-) create mode 100644 src/todo_file/mod.rs diff --git a/src/main.rs b/src/main.rs index 85a6494..8c6cda6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ +mod todo_file; + use chrono::naive::NaiveDate; use chrono::{Datelike, Local}; use std::env; use std::fs::{copy, read_dir}; use std::path::{Path, PathBuf}; use std::process::Command; -use todo_file::TodoFile; +use crate::todo_file::TodoFile; //TODO handle unwraps and errors more uniformly //TODO move TodoFile into its file @@ -52,79 +54,6 @@ fn main() { } } -mod todo_file { - use chrono::naive::NaiveDate; - use regex::Regex; - use std::convert::TryFrom; - use std::fs::DirEntry; - use std::str::FromStr; - - #[derive(Debug)] - pub struct TodoFile { - pub file: DirEntry, - pub date: NaiveDate, - } - - impl TodoFile { - fn capture_as_number( - capture: ®ex::Captures, - name: &str, - ) -> Result { - Ok(capture - .name(name) - .unwrap() - .as_str() - .parse::() - .ok() - .ok_or("Something went wrong".to_owned())?) - } - - pub fn latest_file(a: TodoFile, b: TodoFile) -> TodoFile { - if a.date > b.date { - a - } else { - b - } - } - - fn get_file_regex() -> Regex { - //TODO This would ideally be configurable - Regex::new(r"(?P\d{4})-(?P\d{2})-(?P\d{2}).md") - .expect("could not create regex") - } - } - - impl TryFrom for TodoFile { - type Error = String; - - fn try_from(direntry: DirEntry) -> Result { - let re = TodoFile::get_file_regex(); - println!("{:?}", re); - let file_name = direntry.file_name(); - let file_name_str = match file_name.to_str() { - Some(name) => name, - _ => "", - }; - println!("{:?}", file_name_str); - - if let Some(caps) = re.captures(file_name_str) { - let year: i32 = Self::capture_as_number(&caps, "year").unwrap(); - let month: u32 = Self::capture_as_number(&caps, "month").unwrap(); - let day: u32 = Self::capture_as_number(&caps, "day").unwrap(); - - return Ok(Self { - file: direntry, - date: NaiveDate::from_ymd_opt(year, month, day).unwrap(), - }); - }; - Err(format!( - "Could not parse file name => {{ name: {:?}, re: {:?} }}", - file_name, re - ) - .to_string()) - } - } -} fn get_editor(fallback: String) -> String { match env::var("EDITOR") { diff --git a/src/todo_file/mod.rs b/src/todo_file/mod.rs new file mode 100644 index 0000000..2d7e09f --- /dev/null +++ b/src/todo_file/mod.rs @@ -0,0 +1,69 @@ + +use chrono::naive::NaiveDate; +use regex::Regex; +use std::convert::TryFrom; +use std::fs::DirEntry; +use std::str::FromStr; + +#[derive(Debug)] +pub struct TodoFile { + pub file: DirEntry, + pub date: NaiveDate, +} + +impl TodoFile { + fn capture_as_number(capture: ®ex::Captures, name: &str) -> Result { + Ok(capture + .name(name) + .unwrap() + .as_str() + .parse::() + .ok() + .ok_or("Something went wrong".to_owned())?) + } + + pub fn latest_file(a: TodoFile, b: TodoFile) -> TodoFile { + if a.date > b.date { + a + } else { + b + } + } + + fn get_file_regex() -> Regex { + //TODO This would ideally be configurable + Regex::new(r"(?P\d{4})-(?P\d{2})-(?P\d{2}).md") + .expect("could not create regex") + } +} + +impl TryFrom for TodoFile { + type Error = String; + + fn try_from(direntry: DirEntry) -> Result { + let re = TodoFile::get_file_regex(); + println!("{:?}", re); + let file_name = direntry.file_name(); + let file_name_str = match file_name.to_str() { + Some(name) => name, + _ => "", + }; + println!("{:?}", file_name_str); + + if let Some(caps) = re.captures(file_name_str) { + let year: i32 = Self::capture_as_number(&caps, "year").unwrap(); + let month: u32 = Self::capture_as_number(&caps, "month").unwrap(); + let day: u32 = Self::capture_as_number(&caps, "day").unwrap(); + + return Ok(Self { + file: direntry, + date: NaiveDate::from_ymd_opt(year, month, day).unwrap(), + }); + }; + Err(format!( + "Could not parse file name => {{ name: {:?}, re: {:?} }}", + file_name, re + ) + .to_string()) + } +}