From 747e6db8a56cb74b1c57b6aeca4d1ac4c9609710 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 19 Nov 2021 01:15:34 +0100 Subject: [PATCH] Print file path in error message --- src/main.rs | 2 +- src/parse.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 48bb731..4654bf2 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(&content)?; + let commands = parse::parse(&opt.file, &content)?; println!("{:#?}", commands); Ok(()) } diff --git a/src/parse.rs b/src/parse.rs index 9a548b9..140995a 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,3 +1,4 @@ +use std::path::Path; use std::result; use chrono::NaiveDate; @@ -215,12 +216,14 @@ fn parse_command(p: Pair) -> Result { } } -pub fn parse(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() // For some reason, the EOI in `file` always gets captured .take_while(|p| p.as_rule() == Rule::command) .map(parse_command) - .collect() + .collect::>() + .map_err(|e| e.with_path(&path)) }