Parse and format logs

This commit is contained in:
Joscha 2021-12-31 17:54:26 +01:00
parent 0e4ef7fef3
commit 017098da34
4 changed files with 47 additions and 3 deletions

View file

@ -337,6 +337,12 @@ pub struct Note {
pub desc: Vec<String>,
}
#[derive(Debug)]
pub struct Log {
pub date: NaiveDate,
pub desc: Vec<String>,
}
#[derive(Debug)]
pub enum Command {
Task(Task),
@ -371,5 +377,6 @@ pub struct File {
pub contents: String,
pub includes: Vec<String>,
pub timezone: Option<String>,
pub logs: Vec<Log>,
pub commands: Vec<Command>,
}

View file

@ -6,7 +6,7 @@ use crate::files::commands::DoneKind;
use super::commands::{
BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, Expr, File, FormulaSpec,
Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec,
Log, Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec,
};
use super::primitives::{Spanned, Time, Weekday};
@ -306,9 +306,19 @@ impl fmt::Display for Command {
}
}
impl fmt::Display for Log {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "LOG {}", self.date)?;
format_desc(f, &self.desc)?;
Ok(())
}
}
impl fmt::Display for File {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut empty = true;
// TODO Sort includes alphabetically
for include in &self.includes {
writeln!(f, "INCLUDE {}", include)?;
empty = false;
@ -322,6 +332,11 @@ impl fmt::Display for File {
empty = false;
}
// TODO Sort logs from old to new
for log in &self.logs {
writeln!(f, "{}", log)?;
}
for command in &self.commands {
if !empty {
writeln!(f)?;

View file

@ -140,8 +140,11 @@ note = {
~ description
}
log_head = !{ "LOG" ~ datum ~ eol }
log = { log_head ~ description }
empty_line = _{ WHITESPACE* ~ NEWLINE }
command = { include | timezone | task | note }
command = { include | timezone | task | note | log }
file = ${ SOI ~ (empty_line* ~ command)* ~ empty_line* ~ WHITESPACE* ~ EOI }

View file

@ -9,7 +9,7 @@ use pest::{Parser, Span};
use super::commands::{
BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, DoneDate, DoneKind, Expr, File,
FormulaSpec, Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec,
FormulaSpec, Log, Note, Repeat, Spec, Statement, Task, Var, WeekdaySpec,
};
use super::primitives::{Spanned, Time, Weekday};
@ -795,6 +795,23 @@ fn parse_note(p: Pair<'_, Rule>) -> Result<Note> {
})
}
fn parse_log_head(p: Pair<'_, Rule>) -> Result<NaiveDate> {
assert_eq!(p.as_rule(), Rule::log_head);
Ok(parse_datum(p.into_inner().next().unwrap())?.value)
}
fn parse_log(p: Pair<'_, Rule>) -> Result<Log> {
assert_eq!(p.as_rule(), Rule::log);
let mut p = p.into_inner();
let date = parse_log_head(p.next().unwrap())?;
let desc = parse_description(p.next().unwrap())?;
assert_eq!(p.next(), None);
Ok(Log { date, desc })
}
fn parse_command(p: Pair<'_, Rule>, file: &mut File) -> Result<()> {
assert_eq!(p.as_rule(), Rule::command);
@ -807,6 +824,7 @@ fn parse_command(p: Pair<'_, Rule>, file: &mut File) -> Result<()> {
},
Rule::task => file.commands.push(Command::Task(parse_task(p)?)),
Rule::note => file.commands.push(Command::Note(parse_note(p)?)),
Rule::log => file.logs.push(parse_log(p)?),
_ => unreachable!(),
}
@ -820,6 +838,7 @@ pub fn parse_file(p: Pair<'_, Rule>, contents: String) -> Result<File> {
contents,
includes: vec![],
timezone: None,
logs: vec![],
commands: vec![],
};