Skip to content

Commit 2777229

Browse files
Adds IllegalState to ParserError (#24)
Also changes the `PairsExt::exactly_one` to use this error for the empty case. Co-authored-by: Reza Handzalah <reza.handzalah@mbiz.co.id>
1 parent f227d0d commit 2777229

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

partiql-parser/src/peg.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! can be exported for users to consume.
55
66
use crate::prelude::*;
7-
use crate::result::syntax_error;
7+
use crate::result::{illegal_state, syntax_error};
88
use pest::iterators::{Pair, Pairs};
99
use pest::{Parser, RuleType};
1010
use pest_derive::Parser;
@@ -33,10 +33,7 @@ impl<'val, R: RuleType> PairsExt<'val, R> for Pairs<'val, R> {
3333
}
3434
Ok(pair)
3535
}
36-
None => syntax_error(
37-
"Expected at one token pair, got nothing!",
38-
Position::Unknown,
39-
),
36+
None => illegal_state("Expected at least one token pair, got nothing!"),
4037
}
4138
}
4239
}

partiql-parser/src/result.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ pub enum ParserError {
190190
/// Indicates that there is a problem with run-time violation of some API.
191191
#[error("Invalid Argument: {message}")]
192192
InvalidArgument { message: String },
193+
194+
/// Indicates that there is an internal error that was not due to user input or API violation.
195+
#[error("Illegal State: {message}")]
196+
IllegalState { message: String },
193197
}
194198

195199
impl ParserError {
@@ -209,6 +213,14 @@ impl ParserError {
209213
message: message.into(),
210214
}
211215
}
216+
217+
/// Convenience function to create a [`IllegalState`](ParserError::IllegalState).
218+
#[inline]
219+
pub fn illegal_state<S: Into<String>>(message: S) -> Self {
220+
Self::IllegalState {
221+
message: message.into(),
222+
}
223+
}
212224
}
213225

214226
/// Convenience function to create an `Err([SyntaxError](ParserError::SyntaxError))`.
@@ -223,6 +235,12 @@ pub fn invalid_argument<T, S: Into<String>>(message: S) -> ParserResult<T> {
223235
Err(ParserError::invalid_argument(message))
224236
}
225237

238+
/// Convenience function to create an `Err([IllegalState](ParserError::IllegalState))`.
239+
#[inline]
240+
pub fn illegal_state<T, S: Into<String>>(message: S) -> ParserResult<T> {
241+
Err(ParserError::illegal_state(message))
242+
}
243+
226244
impl<R> From<pest::error::Error<R>> for ParserError
227245
where
228246
R: fmt::Debug,

0 commit comments

Comments
 (0)