isolated TodoFile into module
This commit is contained in:
parent
679acf1064
commit
175a6c20a8
145
src/main.rs
145
src/main.rs
|
|
@ -1,19 +1,12 @@
|
||||||
use chrono::naive::NaiveDate;
|
use chrono::naive::NaiveDate;
|
||||||
use chrono::{Local, Datelike};
|
use chrono::{Datelike, Local};
|
||||||
use regex::Regex;
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::fs::{self, DirEntry};
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::str::FromStr;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use todo_file::TodoFile;
|
||||||
|
|
||||||
//TODO handle unwraps and errors more uniformly
|
//TODO handle unwraps and errors more uniformly
|
||||||
|
//TODO move TodoFile into its file
|
||||||
#[derive(Debug)]
|
|
||||||
struct TodoFile {
|
|
||||||
file: DirEntry,
|
|
||||||
date: NaiveDate,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let data_dir = get_data_dir("notes");
|
let data_dir = get_data_dir("notes");
|
||||||
|
|
@ -35,7 +28,81 @@ fn main() {
|
||||||
Some(today) if latest_file.date < today => println!("Todays file was not created"),
|
Some(today) if latest_file.date < today => println!("Todays file was not created"),
|
||||||
Some(today) if latest_file.date > today => println!("Future files were created"),
|
Some(today) if latest_file.date > today => println!("Future files were created"),
|
||||||
|
|
||||||
_ => println!("Today never happend!")
|
_ => println!("Today never happend!"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod todo_file {
|
||||||
|
use chrono::naive::NaiveDate;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::fs::DirEntry;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TodoFile {
|
||||||
|
pub file: DirEntry,
|
||||||
|
pub date: NaiveDate,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TodoFile {
|
||||||
|
fn capture_as_number<T: FromStr>(
|
||||||
|
capture: ®ex::Captures,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<T, String> {
|
||||||
|
Ok(capture
|
||||||
|
.name(name)
|
||||||
|
.unwrap()
|
||||||
|
.as_str()
|
||||||
|
.parse::<T>()
|
||||||
|
.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<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
|
||||||
|
.expect("could not create regex")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<DirEntry> for TodoFile {
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(direntry: DirEntry) -> Result<Self, Self::Error> {
|
||||||
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,55 +125,3 @@ fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
|
||||||
.reduce(|a, b| TodoFile::latest_file(a, b))
|
.reduce(|a, b| TodoFile::latest_file(a, b))
|
||||||
.ok_or("Could not reduce items".to_string())
|
.ok_or("Could not reduce items".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_file_regex() -> Regex {
|
|
||||||
Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
|
|
||||||
.expect("could not create regex")
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TodoFile {
|
|
||||||
fn capture_as_number<T: FromStr>(capture: ®ex::Captures, name: &str) -> Result<T, String> {
|
|
||||||
Ok(capture
|
|
||||||
.name(name)
|
|
||||||
.unwrap()
|
|
||||||
.as_str()
|
|
||||||
.parse::<T>()
|
|
||||||
.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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<DirEntry> for TodoFile {
|
|
||||||
type Error = String;
|
|
||||||
|
|
||||||
fn try_from(direntry: DirEntry) -> Result<Self, Self::Error> {
|
|
||||||
let re = 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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue