-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move error helpers to their own modules
- Loading branch information
Showing
11 changed files
with
243 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use super::*; | ||
|
||
#[cold] | ||
pub(super) fn one_or_more_elems(node: xml::Node<'_, '_>, name: &str) -> io::Error { | ||
format_err!( | ||
"<{}> element must be contain one or more <{}> elements ({})", | ||
node.tag_name().name(), | ||
name, | ||
node.node_location() | ||
) | ||
} | ||
|
||
#[cold] | ||
pub(super) fn exactly_one_elem(node: xml::Node<'_, '_>, name: &str) -> io::Error { | ||
format_err!( | ||
"<{}> element must be contain exactly one <{}> element ({})", | ||
node.tag_name().name(), | ||
name, | ||
node.node_location() | ||
) | ||
} | ||
|
||
#[cold] | ||
pub(super) fn multiple_elems(node: xml::Node<'_, '_>) -> io::Error { | ||
format_err!( | ||
"multiple <{}> elements ({})", | ||
node.tag_name().name(), | ||
node.node_location() | ||
) | ||
} | ||
|
||
#[cold] | ||
pub(super) fn unexpected_child_elem(child: xml::Node<'_, '_>) -> io::Error { | ||
format_err!( | ||
"unexpected child element <{}> in <{}> element ({})", | ||
child.tag_name().name(), | ||
child.parent_element().unwrap().tag_name().name(), | ||
child.node_location() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::{fmt, io, path::Path, str}; | ||
|
||
#[cfg_attr(test, derive(Debug))] | ||
pub(super) enum ErrorKind { | ||
ExpectedSpace(&'static str, usize), | ||
ExpectedNewline(&'static str, usize), | ||
Expected(&'static str, usize), | ||
Float(usize), | ||
Int(usize), | ||
InvalidW(usize), | ||
InvalidFaceIndex(usize), | ||
Oob(usize, usize), | ||
Io(io::Error), | ||
} | ||
|
||
impl ErrorKind { | ||
#[cold] | ||
#[inline(never)] | ||
pub(super) fn into_io_error(self, start: &[u8], path: Option<&Path>) -> io::Error { | ||
let remaining = match self { | ||
Self::Expected(.., n) | ||
| Self::ExpectedNewline(.., n) | ||
| Self::ExpectedSpace(.., n) | ||
| Self::Float(n) | ||
| Self::Int(n) | ||
| Self::InvalidW(n) | ||
| Self::InvalidFaceIndex(n) | ||
| Self::Oob(.., n) => n, | ||
Self::Io(e) => return e, | ||
}; | ||
crate::error::with_location( | ||
&crate::error::invalid_data(self.to_string()), | ||
&crate::error::Location::find(remaining, start, path), | ||
) | ||
} | ||
} | ||
|
||
impl fmt::Display for ErrorKind { | ||
#[cold] | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match *self { | ||
Self::ExpectedSpace(msg, ..) => write!(f, "expected space after {msg}"), | ||
Self::ExpectedNewline(msg, ..) => write!(f, "expected newline after {msg}"), | ||
Self::Expected(msg, ..) => write!(f, "expected {msg}"), | ||
Self::InvalidW(..) => write!(f, "w in homogeneous vector must not zero"), | ||
Self::InvalidFaceIndex(..) => write!(f, "invalid face index"), | ||
Self::Float(..) => write!(f, "error while parsing a float"), | ||
Self::Int(..) => write!(f, "error while parsing an integer"), | ||
Self::Oob(i, ..) => write!(f, "face index out of bounds ({i})"), | ||
Self::Io(ref e) => write!(f, "{e}"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.