1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use std::path::{Path, PathBuf};
/// Represents any kind of error that can occur while parsing the UCD.
#[derive(Debug)]
pub struct Error {
pub(crate) kind: ErrorKind,
pub(crate) line: Option<u64>,
pub(crate) path: Option<PathBuf>,
}
/// The kind of error that occurred while parsing the UCD.
#[derive(Debug)]
pub enum ErrorKind {
/// An I/O error.
Io(std::io::Error),
/// A generic parse error.
Parse(String),
}
impl Error {
/// Create a new parse error from the given message.
pub(crate) fn parse(msg: String) -> Error {
Error { kind: ErrorKind::Parse(msg), line: None, path: None }
}
/// Return the specific kind of this error.
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
/// Return the line number at which this error occurred, if available.
pub fn line(&self) -> Option<u64> {
self.line
}
/// Return the file path associated with this error, if one exists.
pub fn path(&self) -> Option<&Path> {
self.path.as_ref().map(|p| &**p)
}
/// Unwrap this error into its underlying kind.
pub fn into_kind(self) -> ErrorKind {
self.kind
}
/// Returns true if and only if this is an I/O error.
///
/// If this returns true, the underlying `ErrorKind` is guaranteed to be
/// `ErrorKind::Io`.
pub fn is_io_error(&self) -> bool {
match self.kind {
ErrorKind::Io(_) => true,
_ => false,
}
}
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(ref path) = self.path {
if let Some(line) = self.line {
write!(f, "{}:{}: ", path.display(), line)?;
} else {
write!(f, "{}: ", path.display())?;
}
} else if let Some(line) = self.line {
write!(f, "error on line {}: ", line)?;
}
match self.kind {
ErrorKind::Io(ref err) => write!(f, "{}", err),
ErrorKind::Parse(ref msg) => write!(f, "{}", msg),
}
}
}