sysml_parser/
parse_error.rs1use crate::prelude::{fmt, String, ToString, Vec};
4use crate::{SyntaxError, SyntaxErrorKind};
5
6#[cfg(feature = "std")]
7extern crate std;
8
9pub type ParseResult<T, E = ParseError> = Result<T, E>;
10
11#[derive(Clone, Debug, PartialEq)]
12pub enum ParseError {
13 #[cfg(feature = "std")]
14 Io(std::io::ErrorKind),
15 Syntax(Vec<(SyntaxErrorKind, String)>),
16 Other(String),
17}
18
19#[cfg(feature = "std")]
20impl std::error::Error for ParseError {}
21
22impl fmt::Display for ParseError {
23 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 ParseError::Io(kind) => write!(fmt, "IO error: {:?}", kind),
26 ParseError::Syntax(error) => write!(fmt, "Syntax error: {:?}", error),
27 ParseError::Other(error) => write!(fmt, "Other error: {:?}", error),
28 }
29 }
30}
31
32#[cfg(feature = "std")]
33impl From<std::io::Error> for ParseError {
34 fn from(error: std::io::Error) -> Self {
35 ParseError::Io(error.kind())
36 }
37}
38
39impl<'a> From<nom::Err<SyntaxError<'a>>> for ParseError {
40 fn from(err: nom::Err<SyntaxError<'a>>) -> Self {
41 match err {
42 nom::Err::Error(error) | nom::Err::Failure(error) => ParseError::from(error),
43 nom::Err::Incomplete(_) => unreachable!(),
44 }
45 }
46}
47
48impl<'a> From<SyntaxError<'a>> for ParseError {
49 fn from(error: SyntaxError<'a>) -> Self {
50 ParseError::Syntax(
51 error
52 .errors
53 .iter()
54 .map(|(kind, span)| (kind.clone(), span.to_string()))
55 .collect(),
56 )
57 }
58}
59
60#[cfg(all(feature = "error-stack", not(feature = "std")))]
61impl error_stack::Context for ParseError {}