parent
48df1fd9e0
commit
ddd620a021
|
|
@ -1,10 +1,7 @@
|
||||||
use chrono::naive::NaiveDate;
|
use chrono::naive::NaiveDate;
|
||||||
use regex::Regex;
|
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fs::DirEntry;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::file::FileNameParseError;
|
use crate::file::FileNameParseError;
|
||||||
|
|
||||||
|
|
@ -14,56 +11,6 @@ pub struct File {
|
||||||
pub date: NaiveDate,
|
pub date: NaiveDate,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum FileError {
|
|
||||||
//IOError(&'static str),
|
|
||||||
ParseError(&'static str),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl File {
|
|
||||||
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())?)
|
|
||||||
}
|
|
||||||
|
|
||||||
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> {
|
fn try_get_date(file: &PathBuf) -> Result<NaiveDate, FileNameParseError> {
|
||||||
let file_name = file
|
let file_name = file
|
||||||
.file_name()
|
.file_name()
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,7 @@ impl ToString for Task {
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a AstNode<'a>> for Task {
|
impl<'a> TryFrom<&'a AstNode<'a>> for Task {
|
||||||
type Error = TaskError;
|
type Error = TaskError;
|
||||||
|
|
||||||
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
|
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
|
||||||
let data_ref = &node.data.borrow();
|
let data_ref = &node.data.borrow();
|
||||||
if let NodeValue::TaskItem(ch) = data_ref.value {
|
if let NodeValue::TaskItem(ch) = data_ref.value {
|
||||||
|
|
@ -158,6 +159,7 @@ impl ToString for TaskGroup {
|
||||||
|
|
||||||
impl<'a> TryFrom<&'a AstNode<'a>> for TaskGroup {
|
impl<'a> TryFrom<&'a AstNode<'a>> for TaskGroup {
|
||||||
type Error = TaskError;
|
type Error = TaskError;
|
||||||
|
|
||||||
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
|
fn try_from(node: &'a AstNode<'a>) -> Result<Self, Self::Error> {
|
||||||
let node_ref = &node.data.borrow();
|
let node_ref = &node.data.borrow();
|
||||||
if let NodeValue::Heading(heading) = node_ref.value {
|
if let NodeValue::Heading(heading) = node_ref.value {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue