|
|
@ -4,257 +4,133 @@ use std::{
|
|
|
|
fmt,
|
|
|
|
fmt,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:
|
|
|
|
pub trait Context
|
|
|
|
// this was a complete failure, look at eyre instead or something
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Context for an error
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
|
|
|
|
|
|
|
|
pub struct Context
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
line: Option<u32>,
|
|
|
|
fn line(&self) -> Option<u32>;
|
|
|
|
column: Option<u32>,
|
|
|
|
fn column(&self) -> Option<u32>;
|
|
|
|
file: &'static str,
|
|
|
|
fn file(&self) -> Option<&str>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Context
|
|
|
|
pub trait Reporter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
pub fn new(line: Option<u32>, column: Option<u32>, file: &'static str) -> Self
|
|
|
|
type Error: error::Error + ?Sized;
|
|
|
|
{
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
|
|
line,column,file
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Context
|
|
|
|
#[inline] fn context(&self) -> Option<&dyn Context>
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{} ", self.file)?;
|
|
|
|
None
|
|
|
|
if let Some(line) = self.line {
|
|
|
|
|
|
|
|
write!(f, "{}:", line)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(f, "?:")?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(column) = self.column {
|
|
|
|
|
|
|
|
write!(f, "{}", column)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(f, "?")?;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
fn source(&self) -> &Self::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A wrapper over an error with a nicer error message
|
|
|
|
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
|
|
|
|
///
|
|
|
|
#[inline] fn long_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
/// Like doushio's `Muggle`.
|
|
|
|
|
|
|
|
pub struct Wrap<E,D>(E, D);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub trait WrappedError
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn long_msg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
|
|
|
|
|
|
|
|
fn short_msg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<E,D> WrappedError for Wrap<E,D>
|
|
|
|
|
|
|
|
where D: fmt::Display,
|
|
|
|
|
|
|
|
E: error::Error + 'static
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn long_msg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}", self.long_message())
|
|
|
|
self.short_message(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn short_msg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
write!(f, "{}", self)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<E,D> Wrap<E,D>
|
|
|
|
pub struct ShortMessage<R>(R)
|
|
|
|
where D: fmt::Display,
|
|
|
|
where R: Reporter;
|
|
|
|
E: error::Error + 'static
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Get all sources of the error
|
|
|
|
|
|
|
|
pub fn all_sources(&self) -> Vec<&(dyn error::Error + 'static)>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
std::iter::successors(self.source(), |x| x.source()).collect()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Into the inner error
|
|
|
|
|
|
|
|
pub fn into_inner(self) -> E
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
self.0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get the full and long error message
|
|
|
|
impl<R> fmt::Display for ShortMessage<R>
|
|
|
|
pub fn long_message(&self) -> impl fmt::Display + '_
|
|
|
|
where R: Reporter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct LongOutput<'a, E,D>(&'a Wrap<E,D>);
|
|
|
|
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
impl<'a, E,D> fmt::Display for LongOutput<'a, E,D>
|
|
|
|
|
|
|
|
where Wrap<E,D>: error::Error
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
writeln!(f, "{}", self.0)?;
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
for (i, spec) in (1..).zip(std::iter::successors(self.0.source(), |x| x.source()))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
writeln!(f, " [{}] -> {}", i, spec)?;
|
|
|
|
self.0.short_message(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LongOutput(&self)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub trait WrapExt: Sized
|
|
|
|
pub struct LongMessage<R>(R)
|
|
|
|
{
|
|
|
|
where R: Reporter;
|
|
|
|
/// Wrap this error in another
|
|
|
|
|
|
|
|
fn wrap<T>(self, msg: T) -> Wrap<Self, T>
|
|
|
|
|
|
|
|
where T: fmt::Display;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<E> WrapExt for E where E: error::Error
|
|
|
|
impl<R> fmt::Display for LongMessage<R>
|
|
|
|
|
|
|
|
where R: Reporter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn wrap<T>(self, msg: T) -> Wrap<Self, T>
|
|
|
|
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
where T: fmt::Display
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Wrap(self, msg)
|
|
|
|
self.0.long_message(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub trait WrapErrExt<T,E>: Sized
|
|
|
|
impl<E> Reporter for Box<E>
|
|
|
|
|
|
|
|
where E: error::Error + ?Sized
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// Wrap the error from this result with a nicer error message
|
|
|
|
type Error = E;
|
|
|
|
fn wrap_err<U>(self, msg: U) -> Result<T,Wrap<E, U>>
|
|
|
|
|
|
|
|
where U: fmt::Display;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T,E> WrapErrExt<T,E> for Result<T,E>
|
|
|
|
fn source(&self) -> &Self::Error
|
|
|
|
{
|
|
|
|
|
|
|
|
fn wrap_err<U>(self, msg: U) -> Result<T,Wrap<E, U>>
|
|
|
|
|
|
|
|
where U: fmt::Display
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
self.map_err(|e| Wrap(e, msg))
|
|
|
|
&self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
impl<E,D> fmt::Debug for Wrap<E,D>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}", std::any::type_name::<Self>())
|
|
|
|
write!(f, "{}", &self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<E,D> fmt::Display for Wrap<E,D>
|
|
|
|
impl<'a, R> Reporter for &'a R
|
|
|
|
where D: fmt::Display
|
|
|
|
where R: Reporter
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
type Error = <R as Reporter>::Error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] fn context(&self) -> Option<&dyn Context>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}", self.1)
|
|
|
|
(*self).context()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn source(&self) -> &Self::Error
|
|
|
|
|
|
|
|
|
|
|
|
impl<E,D> error::Error for Wrap<E,D>
|
|
|
|
|
|
|
|
where E: error::Error + 'static,
|
|
|
|
|
|
|
|
D: fmt::Display
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Some(&self.0)
|
|
|
|
(*self).source()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// An error with a context
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
|
|
pub struct ContextError<E>(pub E, pub Context)
|
|
|
|
|
|
|
|
where E: fmt::Debug;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<E> WrappedError for ContextError<E>
|
|
|
|
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
where E: fmt::Debug + fmt::Display
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn long_msg(&self,f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}", self)
|
|
|
|
(*self).short_message(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn short_msg(&self,f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
#[inline] fn long_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
(*self).long_message(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<E> fmt::Display for ContextError<E>
|
|
|
|
pub struct Wrap<E,M>(E, M);
|
|
|
|
where E: fmt::Display + fmt::Debug
|
|
|
|
|
|
|
|
|
|
|
|
impl<E,M> Reporter for Wrap<E,M>
|
|
|
|
|
|
|
|
where E: Reporter,
|
|
|
|
|
|
|
|
M: fmt::Display
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
type Error = E::Error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] fn context(&self) -> Option<&dyn Context>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f, "{}: {}", self.1, self.0)
|
|
|
|
self.0.context()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn source(&self) -> &Self::Error
|
|
|
|
|
|
|
|
|
|
|
|
impl<E> error::Error for ContextError<E>
|
|
|
|
|
|
|
|
where E: fmt::Display + fmt::Debug{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
|
|
|
macro_rules! context {
|
|
|
|
|
|
|
|
() => ($crate::error::Context::new(Some(line!()), Some(column!()), file!()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Construct a new error with context
|
|
|
|
|
|
|
|
#[macro_export] macro_rules! error {
|
|
|
|
|
|
|
|
($err:expr) => ($crate::error::ContextError($err, $crate::context!()));
|
|
|
|
|
|
|
|
($fmt:literal $($tt:tt)*) => ($crate::error!(format!($fmt $($tt)*)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ErrorStack(Box<dyn WrappedError>);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: WrappedError + 'static> From<T> for ErrorStack
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn from(from: T) -> Self
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Self(Box::new(from))
|
|
|
|
self.0.source()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for ErrorStack
|
|
|
|
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
self.0.long_msg(f)
|
|
|
|
write!(f, "{}", self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline] fn long_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for ErrorStack
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
write!(f,"<opaque stack>")
|
|
|
|
write!(f, "{}", self.1)?;
|
|
|
|
|
|
|
|
self.0.long_message(f) //ehh, also failed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl error::Error for ErrorStack{}
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
impl ErrorStack
|
|
|
|
use super::*;
|
|
|
|
{
|
|
|
|
fn test() -> Box<dyn error::Error>
|
|
|
|
/// Get the short message from this stack
|
|
|
|
|
|
|
|
pub fn short_message(&self) -> impl fmt::Display + '_
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
pub struct ShortMessage<'a>(&'a ErrorStack);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> fmt::Display for ShortMessage<'a>
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
Box::new(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "uhhhhh"))
|
|
|
|
{
|
|
|
|
|
|
|
|
self.0.0.short_msg(f)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
ShortMessage(self)
|
|
|
|
fn atest()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let bx = test();
|
|
|
|
|
|
|
|
assert_eq!(format!("{}", LongMessage(&bx)), format!("{}", bx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|