summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-22 22:58:23 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-22 22:58:23 -0500
commitec91ed9cbbac192c7a8012b88b5db0c7e500a6f4 (patch)
tree6e9e3eedd1f70a552fce90087da733d1d2dd3993 /src/parse.rs
parentb516404f35621cc84f5c64a0d3ba25907e8f7288 (diff)
downloadnbsh-ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4.tar.gz
nbsh-ec91ed9cbbac192c7a8012b88b5db0c7e500a6f4.zip
error handling for parse errors
Diffstat (limited to 'src/parse.rs')
-rw-r--r--src/parse.rs30
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
+ }
+}