diff --git a/src/eval/command.rs b/src/eval/command.rs index f27932e..c1957dd 100644 --- a/src/eval/command.rs +++ b/src/eval/command.rs @@ -2,7 +2,8 @@ use std::collections::HashMap; use chrono::NaiveDate; -use crate::files::commands::{BirthdaySpec, Command, Done, Note, Span, Spec, Statement, Task}; +use crate::files::commands::{BirthdaySpec, Command, Done, Note, Spec, Statement, Task}; +use crate::files::primitives::Span; use crate::files::SourcedCommand; use super::date::Dates; diff --git a/src/eval/command/date.rs b/src/eval/command/date.rs index 7de75c5..bc52dc8 100644 --- a/src/eval/command/date.rs +++ b/src/eval/command/date.rs @@ -1,6 +1,7 @@ use chrono::NaiveDate; -use crate::files::commands::{self, Command, Spanned, Time}; +use crate::files::commands::{self, Command}; +use crate::files::primitives::{Spanned, Time}; use super::super::command::CommandState; use super::super::date::Dates; diff --git a/src/eval/command/formula.rs b/src/eval/command/formula.rs index b8f26d0..141f555 100644 --- a/src/eval/command/formula.rs +++ b/src/eval/command/formula.rs @@ -1,4 +1,5 @@ -use crate::files::commands::{self, Expr, Spanned, Time, Var}; +use crate::files::commands::{self, Expr, Var}; +use crate::files::primitives::{Spanned, Time}; use super::super::command::CommandState; use super::super::delta::{Delta, DeltaStep}; diff --git a/src/eval/date.rs b/src/eval/date.rs index 33750f8..ac3a3a8 100644 --- a/src/eval/date.rs +++ b/src/eval/date.rs @@ -1,6 +1,7 @@ use chrono::NaiveDate; -use crate::files::commands::{DoneDate, Time}; +use crate::files::commands::DoneDate; +use crate::files::primitives::Time; #[derive(Debug, Clone, Copy)] struct Times { diff --git a/src/eval/delta.rs b/src/eval/delta.rs index cc533ff..6221a77 100644 --- a/src/eval/delta.rs +++ b/src/eval/delta.rs @@ -2,7 +2,8 @@ use std::cmp::Ordering; use chrono::{Datelike, Duration, NaiveDate}; -use crate::files::commands::{self, Span, Spanned, Time, Weekday}; +use crate::files::commands; +use crate::files::primitives::{Span, Spanned, Time, Weekday}; use super::{util, Error, Result}; diff --git a/src/eval/error.rs b/src/eval/error.rs index dde8a1b..5b89ad1 100644 --- a/src/eval/error.rs +++ b/src/eval/error.rs @@ -2,7 +2,7 @@ use std::result; use chrono::NaiveDate; -use crate::files::commands::{Span, Time}; +use crate::files::primitives::{Span, Time}; #[derive(Debug)] pub enum Error { diff --git a/src/files.rs b/src/files.rs index 7b7e97f..1044739 100644 --- a/src/files.rs +++ b/src/files.rs @@ -10,6 +10,7 @@ use self::commands::{Command, File}; pub mod commands; mod format; mod parse; +pub mod primitives; #[derive(Debug)] struct LoadedFile { diff --git a/src/files/commands.rs b/src/files/commands.rs index d069de7..a764baa 100644 --- a/src/files/commands.rs +++ b/src/files/commands.rs @@ -1,134 +1,6 @@ -use std::fmt; - use chrono::NaiveDate; -#[derive(Debug, Clone, Copy)] -pub struct Span { - pub start: usize, - pub end: usize, -} - -impl<'a> From<&pest::Span<'a>> for Span { - fn from(pspan: &pest::Span<'a>) -> Self { - Self { - start: pspan.start(), - end: pspan.end(), - } - } -} - -#[derive(Clone, Copy)] -pub struct Spanned { - pub span: Span, - pub value: T, -} - -impl fmt::Debug for Spanned { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - self.value.fmt(f) - } -} - -impl Spanned { - pub fn new(span: Span, value: T) -> Self { - Self { span, value } - } -} - -// I don't know how one would write this. It works as a polymorphic standalone -// function, but not in an impl block. -// impl> Spanned { -// pub fn convert(&self) -> Spanned { -// Self::new(self.span, self.value.into()) -// } -// } - -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct Time { - pub hour: u8, - pub min: u8, -} - -impl fmt::Debug for Time { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:02}:{:02}", self.hour, self.min) - } -} - -impl Time { - pub fn new(hour: u32, min: u32) -> Option { - if hour < 24 && min < 60 || hour == 24 && min == 0 { - Some(Self { - hour: hour as u8, - min: min as u8, - }) - } else { - None - } - } -} - -#[derive(Debug, Clone, Copy)] -pub enum Weekday { - Monday, - Tuesday, - Wednesday, - Thursday, - Friday, - Saturday, - Sunday, -} - -impl From for Weekday { - fn from(wd: chrono::Weekday) -> Self { - match wd { - chrono::Weekday::Mon => Self::Monday, - chrono::Weekday::Tue => Self::Tuesday, - chrono::Weekday::Wed => Self::Wednesday, - chrono::Weekday::Thu => Self::Thursday, - chrono::Weekday::Fri => Self::Friday, - chrono::Weekday::Sat => Self::Saturday, - chrono::Weekday::Sun => Self::Sunday, - } - } -} - -impl Weekday { - pub fn name(&self) -> &'static str { - match self { - Weekday::Monday => "mon", - Weekday::Tuesday => "tue", - Weekday::Wednesday => "wed", - Weekday::Thursday => "thu", - Weekday::Friday => "fri", - Weekday::Saturday => "sat", - Weekday::Sunday => "sun", - } - } - - pub fn num(&self) -> u8 { - match self { - Weekday::Monday => 1, - Weekday::Tuesday => 2, - Weekday::Wednesday => 3, - Weekday::Thursday => 4, - Weekday::Friday => 5, - Weekday::Saturday => 6, - Weekday::Sunday => 7, - } - } - - /// How many days from now until the other weekday. - pub fn until(&self, other: Weekday) -> u8 { - let num_self = self.num(); - let num_other = other.num(); - if num_self <= num_other { - num_other - num_self - } else { - num_other + 7 - num_self - } - } -} +use super::primitives::{Span, Spanned, Time, Weekday}; #[derive(Debug, Clone, Copy)] pub enum DeltaStep { diff --git a/src/files/format.rs b/src/files/format.rs index e3eab50..90b719a 100644 --- a/src/files/format.rs +++ b/src/files/format.rs @@ -4,8 +4,9 @@ use chrono::Datelike; use super::commands::{ BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, Expr, File, FormulaSpec, - Note, Repeat, Spanned, Spec, Statement, Task, Time, Var, Weekday, WeekdaySpec, + Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec, }; +use super::primitives::{Spanned, Time, Weekday}; impl fmt::Display for Spanned { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/files/parse.rs b/src/files/parse.rs index 7155e05..e3d1979 100644 --- a/src/files/parse.rs +++ b/src/files/parse.rs @@ -7,12 +7,11 @@ use pest::iterators::Pair; use pest::prec_climber::{Assoc, Operator, PrecClimber}; use pest::{Parser, Span}; -use crate::files::commands::{Repeat, Spanned}; - use super::commands::{ BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, Expr, File, FormulaSpec, - Note, Spec, Statement, Task, Time, Var, Weekday, WeekdaySpec, + Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec, }; +use super::primitives::{Spanned, Time, Weekday}; #[derive(pest_derive::Parser)] #[grammar = "files/grammar.pest"] diff --git a/src/files/primitives.rs b/src/files/primitives.rs new file mode 100644 index 0000000..8fb34c6 --- /dev/null +++ b/src/files/primitives.rs @@ -0,0 +1,129 @@ +use std::fmt; + +#[derive(Debug, Clone, Copy)] +pub struct Span { + pub start: usize, + pub end: usize, +} + +impl<'a> From<&pest::Span<'a>> for Span { + fn from(pspan: &pest::Span<'a>) -> Self { + Self { + start: pspan.start(), + end: pspan.end(), + } + } +} + +#[derive(Clone, Copy)] +pub struct Spanned { + pub span: Span, + pub value: T, +} + +impl fmt::Debug for Spanned { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + self.value.fmt(f) + } +} + +impl Spanned { + pub fn new(span: Span, value: T) -> Self { + Self { span, value } + } +} + +// I don't know how one would write this. It works as a polymorphic standalone +// function, but not in an impl block. +// impl> Spanned { +// pub fn convert(&self) -> Spanned { +// Self::new(self.span, self.value.into()) +// } +// } + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct Time { + pub hour: u8, + pub min: u8, +} + +impl fmt::Debug for Time { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:02}:{:02}", self.hour, self.min) + } +} + +impl Time { + pub fn new(hour: u32, min: u32) -> Option { + if hour < 24 && min < 60 || hour == 24 && min == 0 { + Some(Self { + hour: hour as u8, + min: min as u8, + }) + } else { + None + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum Weekday { + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, + Sunday, +} + +impl From for Weekday { + fn from(wd: chrono::Weekday) -> Self { + match wd { + chrono::Weekday::Mon => Self::Monday, + chrono::Weekday::Tue => Self::Tuesday, + chrono::Weekday::Wed => Self::Wednesday, + chrono::Weekday::Thu => Self::Thursday, + chrono::Weekday::Fri => Self::Friday, + chrono::Weekday::Sat => Self::Saturday, + chrono::Weekday::Sun => Self::Sunday, + } + } +} + +impl Weekday { + pub fn name(&self) -> &'static str { + match self { + Weekday::Monday => "mon", + Weekday::Tuesday => "tue", + Weekday::Wednesday => "wed", + Weekday::Thursday => "thu", + Weekday::Friday => "fri", + Weekday::Saturday => "sat", + Weekday::Sunday => "sun", + } + } + + pub fn num(&self) -> u8 { + match self { + Weekday::Monday => 1, + Weekday::Tuesday => 2, + Weekday::Wednesday => 3, + Weekday::Thursday => 4, + Weekday::Friday => 5, + Weekday::Saturday => 6, + Weekday::Sunday => 7, + } + } + + /// How many days from now until the other weekday. + pub fn until(&self, other: Self) -> u8 { + let num_self = self.num(); + let num_other = other.num(); + if num_self <= num_other { + num_other - num_self + } else { + num_other + 7 - num_self + } + } +}