Extract primitive types to separate file
This commit is contained in:
parent
d105d959fa
commit
54ffb7a70a
11 changed files with 146 additions and 139 deletions
|
|
@ -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<T> {
|
||||
pub span: Span,
|
||||
pub value: T,
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for Spanned<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
self.value.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Spanned<T> {
|
||||
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<S, T: Into<S>> Spanned<T> {
|
||||
// pub fn convert(&self) -> Spanned<S> {
|
||||
// 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<Self> {
|
||||
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<chrono::Weekday> 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 {
|
||||
|
|
|
|||
|
|
@ -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<T: fmt::Display> fmt::Display for Spanned<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
|
|
|
|||
129
src/files/primitives.rs
Normal file
129
src/files/primitives.rs
Normal file
|
|
@ -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<T> {
|
||||
pub span: Span,
|
||||
pub value: T,
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for Spanned<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
self.value.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Spanned<T> {
|
||||
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<S, T: Into<S>> Spanned<T> {
|
||||
// pub fn convert(&self) -> Spanned<S> {
|
||||
// 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<Self> {
|
||||
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<chrono::Weekday> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue