Compare commits

..

No commits in common. "5ba22cb6c97357157f7726611ccb628d129862b5" and "175a6c20a83549dcc70cdf46f16fff629de9277f" have entirely different histories.

1 changed files with 17 additions and 23 deletions

View File

@ -1,49 +1,43 @@
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use chrono::{Datelike, Local}; use chrono::{Datelike, Local};
use std::env; use std::env;
use std::fs::{copy, read_dir}; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command;
use todo_file::TodoFile; use todo_file::TodoFile;
//TODO handle unwraps and errors more uniformly //TODO handle unwraps and errors more uniformly
//TODO move TodoFile into its file //TODO move TodoFile into its file
//TODO clean up verbose printing
fn main() { fn main() {
let data_dir = get_data_dir("notes"); let data_dir = get_data_dir("notes");
println!("{}", data_dir.to_str().unwrap()); println!("{}", data_dir.to_str().unwrap());
let latest_file = let latest_file = get_latest_file(&data_dir).unwrap();
get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str()); //.expect(
// format!(
// "Could not find any notes files please use format: {}",
// get_file_regex().to_string()
// )
// .as_str(),
//);
println!("Latest file: {:?}", latest_file); println!("Latest file: {:?}", latest_file);
let now = Local::now(); let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()); let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day());
match today { match today {
Some(today) if latest_file.date < today => { Some(today) if latest_file.date == today => println!("Todays file was created"),
println!("Today's file does not exist, creating"); Some(today) if latest_file.date < today => println!("Todays file was not created"),
let today_file_name = format!("{}-{:02}-{:02}.md", today.year(), today.month(), today.day()); Some(today) if latest_file.date > today => println!("Future files were created"),
let mut today_file_path = data_dir.clone();
today_file_path.push(today_file_name);
copy(latest_file.file.path(), today_file_path).unwrap(); _ => println!("Today never happend!"),
}
Some(_) => {
println!("Todays file was created");
Command::new("vim")
.args([latest_file.file.path()])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
}
_ => println!("Could not get today's date"),
} }
} }
mod todo_file { mod todo_file {
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use regex::Regex;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs::DirEntry; use std::fs::DirEntry;
use std::str::FromStr; use std::str::FromStr;
use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
pub struct TodoFile { pub struct TodoFile {
@ -74,7 +68,7 @@ mod todo_file {
} }
fn get_file_regex() -> Regex { fn get_file_regex() -> Regex {
//TODO This would ideally be configurable //TODO This would ideally be configurable
Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md") Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
.expect("could not create regex") .expect("could not create regex")
} }
@ -125,7 +119,7 @@ fn get_data_dir(dir_name: &str) -> PathBuf {
} }
fn get_latest_file(dir: &Path) -> Result<TodoFile, String> { fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
let dir = read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str()); let dir = fs::read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str());
dir.filter_map(|f| f.ok()) dir.filter_map(|f| f.ok())
.filter_map(|file| TodoFile::try_from(file).ok()) .filter_map(|file| TodoFile::try_from(file).ok())
.reduce(|a, b| TodoFile::latest_file(a, b)) .reduce(|a, b| TodoFile::latest_file(a, b))