Parse file instead of command vec

This commit is contained in:
Joscha 2021-11-19 22:55:11 +01:00
parent e9cff194d3
commit a0781d574d
3 changed files with 14 additions and 7 deletions

View file

@ -237,3 +237,8 @@ pub enum Command {
Note(Note), Note(Note),
Birthday(Birthday), Birthday(Birthday),
} }
#[derive(Debug)]
pub struct File {
pub commands: Vec<Command>,
}

View file

@ -15,7 +15,7 @@ pub struct Opt {
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let opt = Opt::from_args(); let opt = Opt::from_args();
let content = fs::read_to_string(&opt.file)?; let content = fs::read_to_string(&opt.file)?;
let commands = parse::parse(&opt.file, &content)?; let file = parse::parse(&opt.file, &content)?;
println!("{:#?}", commands); println!("{:#?}", file);
Ok(()) Ok(())
} }

View file

@ -8,8 +8,8 @@ use pest::prec_climber::{Assoc, Operator, PrecClimber};
use pest::{Parser, Span}; use pest::{Parser, Span};
use crate::commands::{ use crate::commands::{
Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, FormulaSpec, Note, Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, File, FormulaSpec,
Spec, Task, Time, Var, Weekday, WeekdaySpec, Note, Spec, Task, Time, Var, Weekday, WeekdaySpec,
}; };
#[derive(pest_derive::Parser)] #[derive(pest_derive::Parser)]
@ -702,14 +702,16 @@ fn parse_command(p: Pair<Rule>) -> Result<Command> {
} }
} }
pub fn parse(path: &Path, input: &str) -> Result<Vec<Command>> { pub fn parse(path: &Path, input: &str) -> Result<File> {
let path = path.to_string_lossy(); let path = path.to_string_lossy();
let mut pairs = TodayfileParser::parse(Rule::file, input)?; let mut pairs = TodayfileParser::parse(Rule::file, input)?;
let file = pairs.next().unwrap(); let file = pairs.next().unwrap();
file.into_inner() let commands = file
.into_inner()
// For some reason, the EOI in `file` always gets captured // For some reason, the EOI in `file` always gets captured
.take_while(|p| p.as_rule() == Rule::command) .take_while(|p| p.as_rule() == Rule::command)
.map(parse_command) .map(parse_command)
.collect::<Result<_>>() .collect::<Result<_>>()
.map_err(|e| e.with_path(&path)) .map_err(|e| e.with_path(&path))?;
Ok(File { commands })
} }