Extract primitive types to separate file

This commit is contained in:
Joscha 2021-12-05 19:21:49 +01:00
parent d105d959fa
commit 54ffb7a70a
11 changed files with 146 additions and 139 deletions

View file

@ -2,7 +2,8 @@ use std::collections::HashMap;
use chrono::NaiveDate; 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 crate::files::SourcedCommand;
use super::date::Dates; use super::date::Dates;

View file

@ -1,6 +1,7 @@
use chrono::NaiveDate; 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::command::CommandState;
use super::super::date::Dates; use super::super::date::Dates;

View file

@ -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::command::CommandState;
use super::super::delta::{Delta, DeltaStep}; use super::super::delta::{Delta, DeltaStep};

View file

@ -1,6 +1,7 @@
use chrono::NaiveDate; use chrono::NaiveDate;
use crate::files::commands::{DoneDate, Time}; use crate::files::commands::DoneDate;
use crate::files::primitives::Time;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
struct Times { struct Times {

View file

@ -2,7 +2,8 @@ use std::cmp::Ordering;
use chrono::{Datelike, Duration, NaiveDate}; 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}; use super::{util, Error, Result};

View file

@ -2,7 +2,7 @@ use std::result;
use chrono::NaiveDate; use chrono::NaiveDate;
use crate::files::commands::{Span, Time}; use crate::files::primitives::{Span, Time};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {

View file

@ -10,6 +10,7 @@ use self::commands::{Command, File};
pub mod commands; pub mod commands;
mod format; mod format;
mod parse; mod parse;
pub mod primitives;
#[derive(Debug)] #[derive(Debug)]
struct LoadedFile { struct LoadedFile {

View file

@ -1,134 +1,6 @@
use std::fmt;
use chrono::NaiveDate; use chrono::NaiveDate;
#[derive(Debug, Clone, Copy)] use super::primitives::{Span, Spanned, Time, Weekday};
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
}
}
}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum DeltaStep { pub enum DeltaStep {

View file

@ -4,8 +4,9 @@ use chrono::Datelike;
use super::commands::{ use super::commands::{
BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, Expr, File, FormulaSpec, 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> { impl<T: fmt::Display> fmt::Display for Spanned<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View file

@ -7,12 +7,11 @@ use pest::iterators::Pair;
use pest::prec_climber::{Assoc, Operator, PrecClimber}; use pest::prec_climber::{Assoc, Operator, PrecClimber};
use pest::{Parser, Span}; use pest::{Parser, Span};
use crate::files::commands::{Repeat, Spanned};
use super::commands::{ use super::commands::{
BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, Expr, File, FormulaSpec, 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)] #[derive(pest_derive::Parser)]
#[grammar = "files/grammar.pest"] #[grammar = "files/grammar.pest"]

129
src/files/primitives.rs Normal file
View 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
}
}
}