diff options
Diffstat (limited to 'apps/cassiopeia/src/format/parser.rs')
-rw-r--r-- | apps/cassiopeia/src/format/parser.rs | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/apps/cassiopeia/src/format/parser.rs b/apps/cassiopeia/src/format/parser.rs index cc4b1b7c77df..bb2c56be0f33 100644 --- a/apps/cassiopeia/src/format/parser.rs +++ b/apps/cassiopeia/src/format/parser.rs @@ -4,8 +4,7 @@ //! parsed time file. use crate::format::{LineLexer, LineToken, Token}; -use chrono::{DateTime, FixedOffset as Offset}; -use logos::Lexer; +use chrono::{NaiveDate, DateTime, FixedOffset as Offset}; use std::collections::BTreeMap; use std::iter::Iterator; @@ -19,9 +18,7 @@ pub enum LineCfg { /// A session stop line with a date and time Stop(Option<DateTime<Offset>>), /// An invoice line with a date - Invoice(Option<DateTime<Offset>>), - /// An empty line - Empty, + Invoice(Option<NaiveDate>), /// A temporary value that is invalid #[doc(hidden)] Ignore, @@ -39,7 +36,7 @@ impl LineCfg { pub(crate) fn parse<'l>(lex: LineLexer<'l>) -> LineCfg { use LineCfg::*; use Token as T; - + #[cfg_attr(rustfmt, rustfmt_skip)] lex.get_all().into_iter().fold(Ignore, |cfg, tok| match (cfg, tok) { // If the first token is a comment, we ignore it @@ -52,13 +49,10 @@ pub(crate) fn parse<'l>(lex: LineLexer<'l>) -> LineCfg { // If the first token _was_ a keyword, fill in the data (Header(map), LineToken { tt: T::HeaderData, slice }) => Header(append_data(map, slice)), - (Start(_), LineToken { tt: T::Date, slice }) => Start(parse_date(slice)), - (Stop(_), LineToken { tt: T::Date, slice }) => Stop(parse_date(slice)), + (Start(_), LineToken { tt: T::Date, slice }) => Start(parse_datetime(slice)), + (Stop(_), LineToken { tt: T::Date, slice }) => Stop(parse_datetime(slice)), (Invoice(_), LineToken { tt: T::Date, slice }) => Invoice(parse_date(slice)), - // Pass empty lines through, - (Empty, _) => Empty, - // Ignore everything else (which will be filtered) _ => Ignore, }) @@ -70,9 +64,13 @@ fn append_data(mut map: BTreeMap<String, String>, slice: &str) -> BTreeMap<Strin map } -fn parse_date(slice: &str) -> Option<DateTime<Offset>> { +fn parse_datetime(slice: &str) -> Option<DateTime<Offset>> { Some( DateTime::parse_from_str(slice, "%Y-%m-%d %H:%M:%S%:z") .expect("Failed to parse date; invalid format!"), ) } + +fn parse_date(slice: &str) -> Option<NaiveDate> { + Some(NaiveDate::parse_from_str(slice, "%Y-%m-%d").expect("Failed to parse date; invalid format!")) +} |