From ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 22 Dec 2021 22:58:23 -0500 Subject: error handling for parse errors --- src/parse.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/parse.rs') diff --git a/src/parse.rs b/src/parse.rs index 03b865c..69b5135 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -77,16 +77,16 @@ pub struct Commands { } impl Commands { - pub fn parse(full_cmd: &str) -> Self { - Self::build_ast( + pub fn parse(full_cmd: &str) -> Result { + Ok(Self::build_ast( Shell::parse(Rule::line, full_cmd) - .unwrap() + .map_err(|e| Error::new(full_cmd, anyhow::anyhow!(e)))? .next() .unwrap() .into_inner() .next() .unwrap(), - ) + )) } pub fn pipelines(&self) -> &[Pipeline] { @@ -109,3 +109,25 @@ impl Commands { } } } + +pub struct Error { + input: String, + e: anyhow::Error, +} + +impl Error { + fn new(input: &str, e: anyhow::Error) -> Self { + Self { + input: input.to_string(), + e, + } + } + + pub fn input(&self) -> &str { + &self.input + } + + pub fn error(&self) -> &anyhow::Error { + &self.e + } +} -- cgit v1.2.3-54-g00ecf