code cleanup

mostly dead code removal
some formating
This commit is contained in:
Andrei Stoica 2024-04-01 13:25:04 -04:00
parent 48df1fd9e0
commit ddd620a021
2 changed files with 3 additions and 54 deletions

View File

@ -1,10 +1,7 @@
use chrono::naive::NaiveDate;
use regex::Regex;
use std::cmp::min;
use std::convert::TryFrom;
use std::fs::DirEntry;
use std::path::PathBuf;
use std::str::FromStr;
use crate::file::FileNameParseError;
@ -14,56 +11,6 @@ pub struct File {
pub date: NaiveDate,
}
pub enum FileError {
//IOError(&'static str),
ParseError(&'static str),
}
impl File {
fn capture_as_number<T: FromStr>(capture: &regex::Captures, name: &str) -> Result<T, String> {
Ok(capture
.name(name)
.unwrap()
.as_str()
.parse::<T>()
.ok()
.ok_or("Something went wrong".to_owned())?)
}
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 File {
type Error = FileError;
fn try_from(direntry: DirEntry) -> Result<Self, Self::Error> {
let re = File::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.path(),
date: NaiveDate::from_ymd_opt(year, month, day).unwrap(),
});
};
Err(FileError::ParseError("Could not parse file name"))
}
}
fn try_get_date(file: &PathBuf) -> Result<NaiveDate, FileNameParseError> {
let file_name = file
.file_name()

View File

@ -81,7 +81,7 @@ impl ToString for Task {
.map(|task| task.to_string())
.collect::<Vec<_>>()
.join("\n");
format!("\n{}", text).trim_end().replace("\n", "\n ")
format!("\n{}", text).trim_end().replace("\n", "\n ")
} else {
"".into()
};
@ -92,6 +92,7 @@ impl ToString for Task {
impl<'a> TryFrom<&'a AstNode<'a>> for Task {
type Error = TaskError;
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
let data_ref = &node.data.borrow();
if let NodeValue::TaskItem(ch) = data_ref.value {
@ -158,6 +159,7 @@ impl ToString for TaskGroup {
impl<'a> TryFrom<&'a AstNode<'a>> for TaskGroup {
type Error = TaskError;
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
let node_ref = &node.data.borrow();
if let NodeValue::Heading(heading) = node_ref.value {