diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-22 22:58:23 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-22 22:58:23 -0500 |
commit | ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4 (patch) | |
tree | 6e9e3eedd1f70a552fce90087da733d1d2dd3993 /src/parse.rs | |
parent | b516404f35621cc84f5c64a0d3ba25907e8f7288 (diff) | |
download | nbsh-ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4.tar.gz nbsh-ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4.zip |
error handling for parse errors
Diffstat (limited to 'src/parse.rs')
-rw-r--r-- | src/parse.rs | 30 |
1 files changed, 26 insertions, 4 deletions
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<Self, Error> { + 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 + } +} |