diff --git a/src/commands.rs b/src/commands.rs index 3001d81..1182875 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -237,3 +237,8 @@ pub enum Command { Note(Note), Birthday(Birthday), } + +#[derive(Debug)] +pub struct File { + pub commands: Vec, +} diff --git a/src/main.rs b/src/main.rs index 4654bf2..ffc434b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ pub struct Opt { fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); let content = fs::read_to_string(&opt.file)?; - let commands = parse::parse(&opt.file, &content)?; - println!("{:#?}", commands); + let file = parse::parse(&opt.file, &content)?; + println!("{:#?}", file); Ok(()) } diff --git a/src/parse.rs b/src/parse.rs index 097a4c6..fd68477 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -8,8 +8,8 @@ use pest::prec_climber::{Assoc, Operator, PrecClimber}; use pest::{Parser, Span}; use crate::commands::{ - Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, FormulaSpec, Note, - Spec, Task, Time, Var, Weekday, WeekdaySpec, + Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, File, FormulaSpec, + Note, Spec, Task, Time, Var, Weekday, WeekdaySpec, }; #[derive(pest_derive::Parser)] @@ -702,14 +702,16 @@ fn parse_command(p: Pair) -> Result { } } -pub fn parse(path: &Path, input: &str) -> Result> { +pub fn parse(path: &Path, input: &str) -> Result { let path = path.to_string_lossy(); let mut pairs = TodayfileParser::parse(Rule::file, input)?; let file = pairs.next().unwrap(); - file.into_inner() + let commands = file + .into_inner() // For some reason, the EOI in `file` always gets captured .take_while(|p| p.as_rule() == Rule::command) .map(parse_command) .collect::>() - .map_err(|e| e.with_path(&path)) + .map_err(|e| e.with_path(&path))?; + Ok(File { commands }) }