rusty-tasks/src/file/mod.rs

211 lines
6.9 KiB
Rust
Raw Normal View History

2024-03-06 14:50:03 -05:00
use crate::todo::{File as TodoFile, Status as TaskStatus};
2024-03-04 14:58:30 -05:00
use crate::NaiveDate;
2024-03-06 14:50:03 -05:00
use crate::TaskGroup;
use chrono::Datelike;
2024-03-04 14:58:30 -05:00
use comrak::nodes::{AstNode, NodeValue};
2024-03-06 14:50:03 -05:00
use comrak::parse_document;
2024-03-04 14:58:30 -05:00
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use std::collections::HashMap;
use std::fs::{read, read_dir, File};
2024-03-06 14:50:03 -05:00
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str;
#[derive(Debug, Clone, PartialEq)]
pub struct DatedPathBuf {
path: PathBuf,
date: NaiveDate,
}
#[derive(Debug)]
pub enum FileNameParseError {
TypeConversionError(&'static str),
ParseError(chrono::ParseError),
}
2024-03-04 14:58:30 -05:00
pub fn get_filepath(data_dir: &PathBuf, date: &NaiveDate) -> PathBuf {
let file_name = format!("{}-{:02}-{:02}.md", date.year(), date.month(), date.day());
let mut file_path = data_dir.clone();
file_path.push(file_name);
file_path
}
pub fn generate_file_content(data: &Vec<TaskGroup>, date: &NaiveDate) -> String {
let mut content = format!(
"# Today's tasks {}-{:02}-{:02}\n",
date.year(),
date.month(),
date.day()
);
data.iter()
.for_each(|task_group| content.push_str(format!("\n{}", task_group.to_string()).as_str()));
content
}
pub fn write_file(path: &PathBuf, content: &String) {
let mut new_file = File::create(&path).expect("Could not open today's file: {today_file_path}");
write!(new_file, "{}", content).expect("Could not write to file: {today_file_path}");
}
pub fn load_file(file: &TodoFile) -> String {
let contents_utf8 = read(file.file.path())
.expect(format!("Could not read file {}", file.file.path().to_string_lossy()).as_str());
str::from_utf8(&contents_utf8)
.expect(
format!(
"failed to convert contents of file to string: {}",
file.file.path().to_string_lossy()
)
.as_str(),
)
.to_string()
}
pub fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> {
let options = &ComrakOptions {
extension: ComrakExtensionOptions {
tasklist: true,
..ComrakExtensionOptions::default()
},
parse: ComrakParseOptions {
relaxed_tasklist_matching: true,
..ComrakParseOptions::default()
},
..ComrakOptions::default()
};
parse_document(arena, contents, options)
}
pub fn extract_secitons<'a>(
root: &'a AstNode<'a>,
sections: &Vec<String>,
) -> HashMap<String, TaskGroup> {
let mut groups: HashMap<String, TaskGroup> = HashMap::new();
for node in root.reverse_children() {
let node_ref = &node.data.borrow();
if let NodeValue::Heading(heading) = node_ref.value {
if heading.level < 2 {
continue;
}
let first_child_ref = &node.first_child();
let first_child = if let Some(child) = first_child_ref {
child
} else {
continue;
};
let data_ref = &first_child.data.borrow();
let title = if let NodeValue::Text(value) = &data_ref.value {
value
} else {
continue;
};
if sections.iter().any(|section| section.eq(title)) {
if let Ok(mut group) = TaskGroup::try_from(node) {
group.tasks = group
.tasks
.into_iter()
.filter(|task| !matches!(task.status, TaskStatus::Done(_)))
.collect();
groups.insert(title.to_string(), group);
}
}
};
}
groups
}
pub fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
let dir = read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str());
dir.filter_map(|f| f.ok())
.filter_map(|file| TodoFile::try_from(file).ok())
.reduce(|a, b| TodoFile::latest_file(a, b))
.ok_or("Could not reduce items".to_string())
}
2024-03-06 14:50:03 -05:00
fn try_get_date(file: &PathBuf) -> Result<NaiveDate, FileNameParseError> {
let file_name = file
.file_name()
.ok_or(FileNameParseError::TypeConversionError(
"Could not get filename from path: {:?}",
))?
.to_str()
.ok_or(FileNameParseError::TypeConversionError(
"Could not get filename from path: {:?}",
))?;
NaiveDate::parse_from_str(file_name, "%Y-%m-%d.md")
.or_else(|e| Err(FileNameParseError::ParseError(e)))
}
impl TryFrom<PathBuf> for DatedPathBuf {
type Error = FileNameParseError;
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
Ok(Self {
date: try_get_date(&path)?,
path,
})
}
}
pub fn get_closest_files(files: &Vec<PathBuf>, target: NaiveDate, n: usize) -> Vec<DatedPathBuf> {
let mut dated_files = files
.clone()
.into_iter()
.filter_map(|file| DatedPathBuf::try_from(file).ok())
.collect::<Vec<_>>();
dated_files.sort_by_cached_key(|dated_file| (dated_file.date - target).num_days().abs());
dated_files[..n].to_vec()
}
#[cfg(test)]
mod test {
use super::*;
use chrono::NaiveDate;
use std::path::PathBuf;
#[test]
fn test_get_closest_date() {
let files = vec![
PathBuf::from("./2024-01-01.md"),
PathBuf::from("./2024-01-02.md"),
PathBuf::from("./2024-01-03.md"),
PathBuf::from("./2024-02-01.md"),
PathBuf::from("./2024-03-01.md"),
PathBuf::from("./2024-04-01.md"),
PathBuf::from("./2024-04-02.md"),
PathBuf::from("./2024-04-03.md"),
PathBuf::from("./2024-04-04.md"),
];
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2023, 12, 30).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-01-01.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-02.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-03.md")).unwrap(),
];
assert_eq!(res, expected_res);
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-02-01.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-03.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-03-01.md")).unwrap(),
];
assert_eq!(res, expected_res);
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2024, 5, 2).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-04-04.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-04-03.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-04-02.md")).unwrap(),
];
assert_eq!(res, expected_res);
}
}