Create own Time
This allows me to use "24:00" as time
This commit is contained in:
parent
8143c2bf35
commit
8fb69df548
2 changed files with 39 additions and 29 deletions
|
|
@ -1,4 +1,23 @@
|
|||
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
||||
use chrono::{NaiveDate, NaiveDateTime};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Time {
|
||||
hour: u8,
|
||||
min: u8,
|
||||
}
|
||||
|
||||
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)]
|
||||
pub enum Weekday {
|
||||
|
|
@ -36,34 +55,24 @@ pub enum DeltaStep {
|
|||
#[derive(Debug)]
|
||||
pub struct Delta(pub Vec<DeltaStep>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DateEndSpec {
|
||||
pub end: Option<NaiveDate>,
|
||||
pub delta: Option<Delta>,
|
||||
pub end_time: Option<NaiveTime>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DateSpec {
|
||||
pub start: NaiveDate,
|
||||
pub delta: Option<Delta>,
|
||||
pub start_time: Option<NaiveTime>,
|
||||
pub end: Option<DateEndSpec>,
|
||||
pub start_delta: Option<Delta>,
|
||||
pub start_time: Option<Time>,
|
||||
pub end: Option<NaiveDate>,
|
||||
pub end_delta: Option<Delta>,
|
||||
pub end_time: Option<Time>,
|
||||
pub repeat: Option<Delta>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WeekdayEndSpec {
|
||||
pub end: Option<Weekday>,
|
||||
pub delta: Option<Delta>,
|
||||
pub end_time: Option<NaiveTime>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WeekdaySpec {
|
||||
pub start: Weekday,
|
||||
pub start_time: Option<NaiveTime>,
|
||||
pub end: Option<WeekdayEndSpec>,
|
||||
pub start_time: Option<Time>,
|
||||
pub end: Option<Weekday>,
|
||||
pub end_delta: Option<Delta>,
|
||||
pub end_time: Option<Time>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -180,9 +189,10 @@ pub enum BoolExpr {
|
|||
#[derive(Debug)]
|
||||
pub struct FormulaSpec {
|
||||
pub start: Option<BoolExpr>, // None: *
|
||||
pub start_time: Option<NaiveTime>,
|
||||
pub offset: Option<Delta>,
|
||||
pub start_delta: Option<Delta>,
|
||||
pub start_time: Option<Time>,
|
||||
pub end: Option<Delta>,
|
||||
pub end_time: Option<Time>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -195,7 +205,7 @@ pub enum Spec {
|
|||
#[derive(Debug)]
|
||||
pub struct Done {
|
||||
pub refering_to: Option<NaiveDate>,
|
||||
pub created_at: Option<NaiveDateTime>,
|
||||
pub created_at: Option<(NaiveDate, Time)>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
12
src/parse.rs
12
src/parse.rs
|
|
@ -1,14 +1,14 @@
|
|||
use std::path::Path;
|
||||
use std::result;
|
||||
|
||||
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
||||
use chrono::NaiveDate;
|
||||
use pest::error::{Error, ErrorVariant};
|
||||
use pest::iterators::Pair;
|
||||
use pest::{Parser, Span};
|
||||
|
||||
use crate::commands::{
|
||||
Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, FormulaSpec, Note, Spec,
|
||||
Task, Weekday, WeekdaySpec,
|
||||
Task, Time, Weekday, WeekdaySpec,
|
||||
};
|
||||
|
||||
#[derive(pest_derive::Parser)]
|
||||
|
|
@ -59,7 +59,7 @@ fn parse_datum(p: Pair<Rule>) -> Result<NaiveDate> {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_time(p: Pair<Rule>) -> Result<NaiveTime> {
|
||||
fn parse_time(p: Pair<Rule>) -> Result<Time> {
|
||||
assert_eq!(p.as_rule(), Rule::time);
|
||||
let span = p.as_span();
|
||||
let mut p = p.into_inner();
|
||||
|
|
@ -69,7 +69,7 @@ fn parse_time(p: Pair<Rule>) -> Result<NaiveTime> {
|
|||
|
||||
assert_eq!(p.next(), None);
|
||||
|
||||
match NaiveTime::from_hms_opt(hour, min, 0) {
|
||||
match Time::new(hour, min) {
|
||||
Some(time) => Ok(time),
|
||||
None => fail(span, "invalid time"),
|
||||
}
|
||||
|
|
@ -312,7 +312,7 @@ fn parse_except(p: Pair<Rule>) -> Result<NaiveDate> {
|
|||
parse_datum(p.into_inner().next().unwrap())
|
||||
}
|
||||
|
||||
fn parse_donedate(p: Pair<Rule>) -> Result<NaiveDateTime> {
|
||||
fn parse_donedate(p: Pair<Rule>) -> Result<(NaiveDate, Time)> {
|
||||
assert_eq!(p.as_rule(), Rule::donedate);
|
||||
let mut p = p.into_inner();
|
||||
|
||||
|
|
@ -321,7 +321,7 @@ fn parse_donedate(p: Pair<Rule>) -> Result<NaiveDateTime> {
|
|||
|
||||
assert_eq!(p.next(), None);
|
||||
|
||||
Ok(date.and_time(time))
|
||||
Ok((date, time))
|
||||
}
|
||||
|
||||
fn parse_done(p: Pair<Rule>) -> Result<Done> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue