parent
3d254efc4b
commit
cf6d97d69f
@ -0,0 +1,24 @@
|
||||
|
||||
extern crate rustc_version;
|
||||
use rustc_version::{version, version_meta, Channel};
|
||||
|
||||
fn main() {
|
||||
// Assert we haven't travelled back in time
|
||||
assert!(version().unwrap().major >= 1);
|
||||
|
||||
// Set cfg flags depending on release channel
|
||||
match version_meta().unwrap().channel {
|
||||
Channel::Stable => {
|
||||
println!("cargo:rustc-cfg=stable");
|
||||
}
|
||||
Channel::Beta => {
|
||||
println!("cargo:rustc-cfg=beta");
|
||||
}
|
||||
Channel::Nightly => {
|
||||
println!("cargo:rustc-cfg=nightly");
|
||||
}
|
||||
Channel::Dev => {
|
||||
println!("cargo:rustc-cfg=dev");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,323 +0,0 @@
|
||||
//! Error handling stuffs
|
||||
use std::{
|
||||
error,
|
||||
fmt,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Default)]
|
||||
pub struct Context
|
||||
{
|
||||
file: Option<&'static str>,
|
||||
line: Option<u32>,
|
||||
column: Option<u32>,
|
||||
}
|
||||
|
||||
impl Context
|
||||
{
|
||||
pub fn with_all(file: &'static str, line: u32, column: u32) -> Self
|
||||
{
|
||||
Self {
|
||||
file: Some(file),
|
||||
line: Some(line),
|
||||
column: Some(column)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Context
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
if let Some(file) = self.file {
|
||||
write!(f, "{} ", file)
|
||||
} else {
|
||||
write!(f, "? ")
|
||||
}?;
|
||||
if let Some(line) = self.line {
|
||||
write!(f, "{}:", line)
|
||||
} else {
|
||||
write!(f, "?:")
|
||||
}?;
|
||||
if let Some(column) = self.column {
|
||||
write!(f, "{}", column)
|
||||
} else {
|
||||
write!(f, "?")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! context {
|
||||
() => ($crate::error::Context::with_all(file!(), line!(), column!()));
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ContextError<E>(E, Context)
|
||||
where E: error::Error;
|
||||
|
||||
impl<E> ContextError<E>
|
||||
where E: error::Error
|
||||
{
|
||||
pub fn with_context(from: E, ctx: Context) -> Self
|
||||
{
|
||||
Self(from, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> error::Error for ContextError<E>
|
||||
where E: error::Error + 'static
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
||||
{
|
||||
Some(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> fmt::Display for ContextError<E>
|
||||
where E: error::Error
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}: {}", self.1, self.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! error {
|
||||
($msg:expr) => {
|
||||
{
|
||||
pub struct DisplayError<D>(D) where D: fmt::Display;
|
||||
impl<D> fmt::Debug for DisplayError<D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", std::any::type_name::<Self>())
|
||||
}
|
||||
}
|
||||
impl<D> fmt::Display for DisplayError<D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
impl<D> error::Error for DisplayError<D>
|
||||
where D: fmt::Display{}
|
||||
|
||||
$crate::error::ContextError::with_context(DisplayError($msg), context!())
|
||||
}
|
||||
};
|
||||
(&fmt:literal $($tt:tt)*) => ($crate::error!(format!($literal $($tt)*)));
|
||||
}
|
||||
|
||||
pub struct Wrap<E,D>(E,D);
|
||||
|
||||
|
||||
impl<E,D> Wrap<E,D>
|
||||
where E: error::Error + 'static,
|
||||
D: fmt::Display
|
||||
{
|
||||
pub fn long_message(&self) -> impl fmt::Display + '_
|
||||
{
|
||||
|
||||
pub struct LongMessage<'a, E,D>(&'a Wrap<E,D>);
|
||||
|
||||
impl<'a, E,D> fmt::Display for LongMessage<'a,E,D>
|
||||
where E: error::Error + 'static,
|
||||
D: fmt::Display
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
use error::Error;
|
||||
for (num, next) in (1..).zip(std::iter::successors(self.0.source(), |e| e.source()))
|
||||
{
|
||||
writeln!(f, " [{}] -> {}", num, next)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
LongMessage(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E,D> fmt::Display for Wrap<E,D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", self.1)
|
||||
}
|
||||
}
|
||||
|
||||
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>())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WrapErrExt<T,E>: Sized
|
||||
{
|
||||
#[inline] fn wrap_err<F,D>(self, val: D) -> Result<T, Wrap<E, D>>
|
||||
where D: fmt::Display
|
||||
{
|
||||
self.wrap_err_with(|| val)
|
||||
}
|
||||
fn wrap_err_with<F,D>(self, func: F) -> Result<T, Wrap<E, D>>
|
||||
where D: fmt::Display,
|
||||
F: FnOnce() -> D;
|
||||
}
|
||||
|
||||
impl<T,E> WrapErrExt<T,E> for Result<T,E>
|
||||
{
|
||||
fn wrap_err_with<F,D>(self, func: F) -> Result<T, Wrap<E, D>>
|
||||
where D: fmt::Display,
|
||||
F: FnOnce() -> D
|
||||
{
|
||||
self.map_err(|e| Wrap(e, func()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// We can now define the reporter trait, like last time, and then out own reporter type for `Wrap<E,D>` that prints its long message properly
|
||||
|
||||
pub trait Reporter
|
||||
{
|
||||
type Error: error::Error + ?Sized;
|
||||
|
||||
#[inline] fn long_message(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
|
||||
|
||||
fn source(&self) -> &Self::Error;
|
||||
}
|
||||
|
||||
impl<E> Reporter for Box<E>
|
||||
where E: error::Error + ?Sized
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", self)
|
||||
}
|
||||
|
||||
fn source(&self) -> &Self::Error
|
||||
{
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WrapReporter<E,D>(Wrap<E,D>);
|
||||
|
||||
impl<E,D> Reporter for WrapReporter<E,D>
|
||||
where E: error::Error + 'static,
|
||||
D: fmt::Display
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
|
||||
fn long_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", self.0.long_message())
|
||||
}
|
||||
|
||||
fn source(&self) -> &Self::Error
|
||||
{
|
||||
&self.0.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<E,D> From<Wrap<E,D>> for WrapReporter<E,D>
|
||||
{
|
||||
fn from(from: Wrap<E,D>) -> Self
|
||||
{
|
||||
Self(from)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Report<E>(Box<E>) where E: ?Sized;
|
||||
|
||||
impl<E> From<E> for Report<E>
|
||||
where E: error::Error
|
||||
{
|
||||
fn from(from: E) -> Self
|
||||
{
|
||||
Self(Box::new(from))
|
||||
}
|
||||
}
|
||||
impl<E> From<Box<E>> for Report<E>
|
||||
where E: error::Error + 'static + ?Sized
|
||||
{
|
||||
fn from(from: Box<E>) -> Self
|
||||
{
|
||||
Self(from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Reporter for Report<E>
|
||||
where E: error::Error + ?Sized
|
||||
{
|
||||
type Error = E;
|
||||
|
||||
fn short_message(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
|
||||
fn source(&self) -> &Self::Error
|
||||
{
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test
|
||||
{
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test()
|
||||
{
|
||||
let err: Result<(), _> = Err(error!("hello world"))
|
||||
.wrap_err_with(|| error!("thing failed"))
|
||||
.wrap_err_with(|| error!("top level failed again"));
|
||||
|
||||
if let Err(err) = err
|
||||
{
|
||||
panic!("{}\n\n{}", err, err.long_message());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report()
|
||||
{
|
||||
fn one() -> Result<(), Report<dyn error::Error>>
|
||||
{
|
||||
let err: Result<(), _> = Err(error!("hello world"))
|
||||
.wrap_err_with(|| error!("thing failed"))
|
||||
.wrap_err_with(|| error!("top level failed again"));
|
||||
Ok(err?) //failed again. just fucking use eyre
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
//! Aggregate errors
|
||||
use super::*;
|
||||
use std::result::Result;
|
||||
|
||||
/// An aggregate of 2 operations that *might* have failed.
|
||||
///
|
||||
/// In order for all aggregate operations to propagate a value, they must all succeed.
|
||||
/// However, will return the errors of failed ones and continue propagating an `Err()` variant, so you get any of the error(s) from the previous aggregate calls.
|
||||
///
|
||||
/// This produces a potential tree of errors, which the `Error` trait cannot represent, so the reporting for `Aggregate` is a bit shit so far, but its general rule is:
|
||||
/// * Debug formatter prints all of the first and second errors including their sub-errors. So, the entire tree. But in an ugly and hard to read way
|
||||
/// * The default formatter prints only the *first* error, but also only if the second error exists, because:
|
||||
/// * The `source()` for the aggregate is first the *second* error, if exists, then the *first* if not.
|
||||
///
|
||||
/// This means, `Aggregate`'s message itself when it does form a tree will be the first branch of the tree, and its source will be the second branch. The second branch is signigicanly better formatted by `eyre`, so the results from the first branch might be a bit hard to decipher, but it's fit for purpose in smaller chains.
|
||||
///
|
||||
/// We prioratise the second branch as the source because that's the one least likely to be an `Aggregate` itself, and will have a more meaningful message trace when reported by `eyre`.
|
||||
pub struct Aggregate<T,U>(Option<T>, Option<U>);
|
||||
|
||||
pub trait AggregateExt<T,E>: Sized
|
||||
{
|
||||
/// Aggregate this result with a function that can take the value if suceeded and return its own value, or a reference to the error and return either its own error or *both*
|
||||
///
|
||||
/// # How it works
|
||||
/// `Operation 2` is always ran, and gets the value from `operation 1` if it suceeded.
|
||||
/// There is never a time where the closure will not be called except when there is a panic
|
||||
///
|
||||
/// * If `operation 1` and `2` fail: The aggregate containing both errors returns
|
||||
/// * If `operation 1` succeeds but operation 2 does not: the error from `operation 2` is returned
|
||||
/// * If `operation 2` succeeds but `operation 1` does not: the error from `operation 1` is returned
|
||||
/// * If both succeed: the value from `operation 2` is returned.
|
||||
fn aggregate_with<F,U,V>(self, next: F) -> Result<V, Aggregate<E, U>>
|
||||
where F: FnOnce(Result<T,&E>) -> Result<V, U>;
|
||||
/// Same as `aggregate_with` except it takes a result instead of taking a closure, so it is evaluated before the aggregation is called. In practive this has little difference, because all closures are called regardless, however it can help in certain situation where we don't need to know the value of pervious aggregate calls, or if we want to calls to happen even if there is a panic in one of the aggregations.
|
||||
fn aggregate<U,V>(self, next: Result<V,U>) -> Result<(T,V), Aggregate<E,U>>;
|
||||
}
|
||||
|
||||
impl<T,E> AggregateExt<T,E> for Result<T,E>
|
||||
{
|
||||
fn aggregate<U,V>(self, next: Result<V,U>) -> Result<(T,V), Aggregate<E,U>>
|
||||
{
|
||||
match self {
|
||||
Ok(v) => match next {
|
||||
Ok(v1) => Ok((v,v1)),
|
||||
Err(err) => Err(Aggregate(None, Some(err))),
|
||||
},
|
||||
Err(er) => match next {
|
||||
Ok(_) => Err(Aggregate(Some(er), None)),
|
||||
Err(err) => Err(Aggregate(Some(er), Some(err))),
|
||||
},
|
||||
}
|
||||
}
|
||||
fn aggregate_with<F,U,V>(self, next: F) -> Result<V, Aggregate<E, U>>
|
||||
where F: FnOnce(Result<T,&E>) -> Result<V, U>
|
||||
{
|
||||
match self {
|
||||
Ok(v) => {
|
||||
match next(Ok(v)) {
|
||||
Err(err) => Err(Aggregate(None, Some(err))),
|
||||
Ok(ok) => Ok(ok),
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
match next(Err(&e)) {
|
||||
Err(err) => Err(Aggregate(Some(e), Some(err))),
|
||||
Ok(_) => Err(Aggregate(Some(e), None)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T,U> fmt::Debug for Aggregate<T,U>
|
||||
where T: fmt::Debug,
|
||||
U: fmt::Debug
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "Aggregate Error ({:?}, {:?})", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T,U> fmt::Display for Aggregate<T,U>
|
||||
where T: error::Error,
|
||||
U: error::Error,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "Aggregate Error ({})", self.0.as_ref().map(|_| 1).unwrap_or(0) + self.1.as_ref().map(|_| 1).unwrap_or(0))?;
|
||||
if self.1.is_some() {
|
||||
if let Some(one) = &self.0 {
|
||||
writeln!(f, "\nBranch: {}", one)?;
|
||||
for next in std::iter::successors(one.source(), |e| e.source())
|
||||
{
|
||||
writeln!(f, " -> {}", next)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T,U> error::Error for Aggregate<T,U>
|
||||
where T: error::Error + 'static,
|
||||
U: error::Error + 'static,
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
||||
{
|
||||
if let Some(z) = &self.1 {
|
||||
return Some(z);
|
||||
}
|
||||
|
||||
if let Some(o) = &self.0 {
|
||||
return Some(o);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
//! Error contexts
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Default)]
|
||||
pub struct Context
|
||||
{
|
||||
file: Option<&'static str>,
|
||||
line: Option<u32>,
|
||||
column: Option<u32>,
|
||||
}
|
||||
|
||||
impl Context
|
||||
{
|
||||
/// Create a new populated context
|
||||
pub const fn with_all(file: &'static str, line: u32, column: u32) -> Self
|
||||
{
|
||||
Self {
|
||||
file: Some(file),
|
||||
line: Some(line),
|
||||
column: Some(column)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool
|
||||
{
|
||||
self.column.is_none() &&
|
||||
self.line.is_none() &&
|
||||
self.file.is_none()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
impl fmt::Display for Context
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
if self.is_empty() {
|
||||
write!(f, "<unbound>")
|
||||
} else {
|
||||
if let Some(file) = self.file {
|
||||
write!(f, "{} ", file)
|
||||
} else {
|
||||
write!(f, "? ")
|
||||
}?;
|
||||
if let Some(line) = self.line {
|
||||
write!(f, "{}:", line)
|
||||
} else {
|
||||
write!(f, "?:")
|
||||
}?;
|
||||
if let Some(column) = self.column {
|
||||
write!(f, "{}", column)
|
||||
} else {
|
||||
write!(f, "?")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContextMessage<D>(pub(super) D, pub(super) Context)
|
||||
where D: fmt::Display;
|
||||
|
||||
impl<D> ContextMessage<D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
pub const fn new(ctx: Context, msg: D) -> Self
|
||||
{
|
||||
Self(msg, ctx)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,113 @@
|
||||
//! Message string
|
||||
use super::*;
|
||||
use std::result::Result;
|
||||
|
||||
pub enum Message
|
||||
{
|
||||
Str(&'static str),
|
||||
String(String),
|
||||
Other(Box<dyn fmt::Display + Send + Sync + 'static>),
|
||||
None,
|
||||
}
|
||||
impl cmp::PartialEq for Message
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool
|
||||
{
|
||||
self.as_str() == other.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl Message
|
||||
{
|
||||
/// Into the internal string
|
||||
pub fn into_string(self) -> Option<Cow<'static, str>>
|
||||
{
|
||||
Some(match self {
|
||||
Self::Str(string) => Cow::Borrowed(string),
|
||||
Self::String(string) => Cow::Owned(string),
|
||||
Self::Other(other) => Cow::Owned(other.to_string()),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
/// Returns message as a string, either created from `format!`, or referenced from inner
|
||||
pub fn as_str(&self) -> Option<Cow<'_, str>>
|
||||
{
|
||||
Some(match self {
|
||||
Self::Str(string) => Cow::Borrowed(string),
|
||||
Self::String(string) => Cow::Borrowed(&string),
|
||||
Self::Other(other) => Cow::Owned(other.to_string()),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
pub const fn from_str(string: &'static str) -> Self
|
||||
{
|
||||
Self::Str(string)
|
||||
}
|
||||
pub const fn from_string(string: String) -> Self
|
||||
{
|
||||
Self::String(string)
|
||||
}
|
||||
pub fn from_other(other: impl fmt::Display + Send + Sync + 'static) -> Self
|
||||
{
|
||||
Self::Other(Box::new(other))
|
||||
}
|
||||
|
||||
pub const fn none() -> Self
|
||||
{
|
||||
Self::None
|
||||
}
|
||||
|
||||
pub fn fmt(&self) -> impl fmt::Display + '_
|
||||
{
|
||||
struct Format<'a>(&'a Message);
|
||||
impl<'a> fmt::Display for Format<'a>
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
match self.0 {
|
||||
Message::Str(string) => write!(f, "{}", string),
|
||||
Message::String(string) => write!(f, "{}", string),
|
||||
Message::Other(other) => write!(f, "{}", other),
|
||||
_ => Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Format(&self)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Message
|
||||
{
|
||||
type Err = !;
|
||||
|
||||
fn from_str(string: &str) -> Result<Self, Self::Err>
|
||||
{
|
||||
Ok(Self::String(string.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Message
|
||||
where T: fmt::Display + 'static + Send + Sync
|
||||
{
|
||||
fn from(from: T) -> Self
|
||||
{
|
||||
Self::from_other(from)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Message
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "error::Message (")?;
|
||||
match self {
|
||||
Self::Str(string) => write!(f, "{:?}", string),
|
||||
Self::String(string) => write!(f, "{:?}", string),
|
||||
Self::Other(_) => write!(f, "<opaque type>"),
|
||||
_=> write!(f, "<unbound>"),
|
||||
}?;
|
||||
write!(f,")")
|
||||
}
|
||||
}
|
@ -0,0 +1,282 @@
|
||||
//! Error handling stuffs (I think we'll go with this one in the end)
|
||||
use std::{
|
||||
error,
|
||||
io,
|
||||
cmp,
|
||||
fmt,
|
||||
str::{
|
||||
FromStr,
|
||||
},
|
||||
borrow::Cow,
|
||||
marker::{
|
||||
Send, Sync
|
||||
},
|
||||
};
|
||||
|
||||
/// Result for all our operations, see `ErrorKind` below
|
||||
///
|
||||
/// This type can be used for when we want to propagate an error, but not pass it off to `eyre` reporter just yet, because we might still want to act on it and respond.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
mod context;
|
||||
pub use context::*;
|
||||
mod message;
|
||||
use message::*;
|
||||
mod wrap;
|
||||
pub use wrap::*;
|
||||
mod aggregate;
|
||||
pub use aggregate::*;
|
||||
|
||||
impl Default for ErrorKind
|
||||
{
|
||||
#[inline]
|
||||
fn default() -> Self
|
||||
{
|
||||
Self::None
|
||||
}
|
||||
}
|
||||
|
||||
impl Error
|
||||
{
|
||||
pub fn into_inner(self) -> ErrorKind
|
||||
{
|
||||
*self.internal
|
||||
}
|
||||
|
||||
/// The kind of the error
|
||||
pub fn kind(&self) -> &ErrorKind
|
||||
{
|
||||
&self.internal
|
||||
}
|
||||
|
||||
/// The message for this error
|
||||
pub fn message(&self) -> Option<Cow<'_, str>>
|
||||
{
|
||||
self.message.as_str()
|
||||
}
|
||||
|
||||
/// The context from this error
|
||||
pub fn context(&self) -> &Context
|
||||
{
|
||||
&self.context
|
||||
}
|
||||
|
||||
|
||||
/// Create a new error with a constant string
|
||||
pub fn new_static(err: ErrorKind, ctx: Context, message: &'static str) -> Self
|
||||
{
|
||||
Self {
|
||||
internal: box err,
|
||||
context: ctx,
|
||||
message: Message::from_str(message),
|
||||
}
|
||||
}
|
||||
/// Create a new error with a string
|
||||
pub fn new_string(err: ErrorKind, ctx: Context, message: String) -> Self
|
||||
{
|
||||
Self {
|
||||
internal: box err,
|
||||
context: ctx,
|
||||
message: Message::from_string(message),
|
||||
}
|
||||
}
|
||||
/// Create a new error with a message
|
||||
pub fn new(err: ErrorKind, ctx: Context, message: impl fmt::Display + 'static + Send + Sync) -> Self
|
||||
{
|
||||
Self {
|
||||
internal: box err,
|
||||
context: ctx,
|
||||
message: Message::from_other(message),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
if !self.context().is_empty() {
|
||||
write!(f, "{}: ", self.context)?;
|
||||
}
|
||||
match &self.message {
|
||||
Message::None => write!(f, "{}", self.internal),
|
||||
message => write!(f, "{}", message.fmt())
|
||||
}
|
||||
}
|
||||
}
|
||||
impl error::Error for Error
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
||||
{
|
||||
match &self.message {
|
||||
Message::None => std::iter::successors(self.internal.source(), |e| e.source()).take(1).next(),
|
||||
_ => Some(&self.internal),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! context {
|
||||
() => ($crate::error::Context::with_all(file!(), line!(), column!()));
|
||||
($msg:expr) => ($crate::error::ContextMessage::new($crate::context!(), $msg));
|
||||
(yield $fmt:literal $($tt:tt)*) => ($crate::context!(lazy_format::lazy_format!($fmt $($tt)*)));
|
||||
($fmt:literal $($tt:tt)*) => ($crate::context!(format!($fmt $($tt)*)));
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! error {
|
||||
($kind:expr, $lit:literal) => ($crate::error::Error::new_static($kind, $crate::context!(), $lit));
|
||||
($lit:literal) => ($crate::error!(Default::default(), $lit));
|
||||
($kind:expr, $msg:expr) => {
|
||||
{
|
||||
pub struct DisplayError<D>(D) where D: fmt::Display;
|
||||
impl<D> fmt::Debug for DisplayError<D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
write!(f, "{}", std::any::type_name::<Self>())
|
||||
}
|
||||
}
|
||||
impl<D> fmt::Display for DisplayError<D>
|
||||
where D: fmt::Display
|
||||
{
|
||||
#[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
impl<D> error::Error for DisplayError<D>
|
||||
where D: fmt::Display{}
|
||||
|
||||
$crate::error::Error::new($kind, $crate::context!(), DisplayError($msg))
|
||||
}
|
||||
};
|
||||
($msg:expr) => ($crate::error!(Default::default(), $msg));
|
||||
(yield $kind:expr, $lit:literal $($tt:tt)*) => ($crate::error!($kind, lazy_format::lazy_format!($lit $($tt)*)));
|
||||
($kind:expr, $lit:literal $($tt:tt)*) => ($crate::error!($kind, format!($lit $($tt)*)));
|
||||
(yield $lit:literal $($tt:tt)*) => ($crate::error!(yield Default::default(), $lit $($tt)*));
|
||||
($lit:literal $($tt:tt)*) => ($crate::error!(Default::default(), $lit $($tt)*));
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error
|
||||
{
|
||||
internal: Box<ErrorKind>,
|
||||
context: Context,
|
||||
message: Message,
|
||||
}
|
||||
|
||||
// Actual stuffs
|
||||
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug)]
|
||||
pub enum ErrorKind
|
||||
{
|
||||
IO(io::Error),
|
||||
Wrap(Error),
|
||||
Any(Box<dyn error::Error +Send + Sync + 'static>),
|
||||
None,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorKind
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||||
{
|
||||
match self {
|
||||
Self::IO(io) => write!(f, "i/o error: {}", io),
|
||||
Self::Wrap(wrap) => write!(f, "{}", wrap),
|
||||
Self::None => Ok(()),
|
||||
_ => write!(f, "unknown error"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for ErrorKind
|
||||
{
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)>
|
||||
{
|
||||
Some(match &self {
|
||||
Self::IO(io) => io,
|
||||
Self::Wrap(wrap) => return wrap.source(),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for ErrorKind
|
||||
{
|
||||
fn from(from: io::Error) -> Self
|
||||
{
|
||||
Self::IO(from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for Error
|
||||
where T: Into<ErrorKind>
|
||||
{
|
||||
fn from(from: T) -> Self
|
||||
{
|
||||
Self {
|
||||
internal: box from.into(),
|
||||
context: Default::default(),
|
||||
message: Message::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(unused_imports)]
|
||||
mod test
|
||||
{
|
||||
use super::*;
|
||||
use color_eyre::{
|
||||
eyre::{
|
||||
Result as ReportedResult,
|
||||
},
|
||||
Help,
|
||||
SectionExt,
|
||||
};
|
||||
#[test]
|
||||
fn test() -> ReportedResult<()>
|
||||
{
|
||||
color_eyre::install()?;
|
||||
fn testt() -> Result<()> {
|
||||
let err= error!(yield one().unwrap_err().into_inner(), "hello world {} {}", 1, 2);
|
||||
Err(err)
|
||||
.wrap_err_with(|| context!("with string"))
|
||||
.wrap_err_with(|| context!(yield "with another string and some stuffs {}", 1))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn works() -> Result<()>
|
||||
{
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn one() -> Result<()>
|
||||
{
|
||||
Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "end of records"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn two() -> Result<()>
|
||||
{
|
||||
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "nonexistent"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn three() -> Result<()>
|
||||
{
|
||||
Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "piped"))
|
||||
.wrap_err_with(|| context!("hello hello"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
||||
Ok(one()
|
||||
.aggregate_with(|_| two())
|
||||
.aggregate_with(|_| works())
|
||||
.aggregate_with(|_| three())?)
|
||||
}
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
//! Wrapping errors
|
||||
use super::*;
|
||||
use std::result::Result;
|
||||
|
||||
/// Convenienve constant for `map_msg`, used so you don't need to keep going `Option::<!>::None`.
|
||||
pub const NO_MESSAGE: Option<!> = None;
|
||||
|
||||
/// Traits for assigning messages and context to any error that can be transformed into `ErrorKind`.
|
||||
pub trait Assign<T>: Sized
|
||||
{
|
||||
/// Wrap an error with a context message deferred
|
||||
fn wrap_err_with<F,D>(self, with: F) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static,
|
||||
F: FnOnce() -> ContextMessage<D>;
|
||||
/// Wrap an error with a context message
|
||||
#[inline] fn wrap_err<D>(self, with: ContextMessage<D>) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static
|
||||
{
|
||||
self.wrap_err_with(|| with)
|
||||
}
|
||||
/// Wrap an error with a message and no context
|
||||
#[inline] fn wrap_err_no_context<D>(self, with: D) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static
|
||||
{
|
||||
self.wrap_err(ContextMessage::new(Default::default(), with))
|
||||
}
|
||||
/// Wrap an error with a message and no context deferred
|
||||
#[inline] fn wrap_err_with_no_context<F, D>(self, with: F) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static,
|
||||
F: FnOnce() -> D
|
||||
{
|
||||
self.wrap_err_with(|| ContextMessage::new(Default::default(), with()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Assign<T> for Result<T,Error>
|
||||
{
|
||||
fn wrap_err_with<F,D>(self, with: F) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static,
|
||||
F: FnOnce() -> ContextMessage<D>
|
||||
{
|
||||
self.map_err(|e| {
|
||||
let val = with();
|
||||
Error::new(ErrorKind::Wrap(e), val.1, val.0)
|
||||
})
|
||||
}
|
||||
}
|
||||
impl<T,E> Assign<T> for Result<T,E>
|
||||
where E: Into<ErrorKind>
|
||||
{
|
||||
fn wrap_err_with<F,D>(self, with: F) -> Result<T, Error>
|
||||
where D: fmt::Display + Send + Sync + 'static,
|
||||
F: FnOnce() -> ContextMessage<D>
|
||||
{
|
||||
self.map_err(|e| {
|
||||
let val = with();
|
||||
Error::new(e.into(), val.1, val.0)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Error mapping extensions
|
||||
pub trait WrapErr<T>: Sized
|
||||
{
|
||||
|
||||
/// Map the error kind
|
||||
fn map_kind_with<F: FnOnce(ErrorKind) -> ErrorKind>(self, kind: F) -> Result<T,Error>;
|
||||
/// Map the error kind impeatively
|
||||
#[inline] fn map_kind(self, kind: ErrorKind) -> Result<T, Error>
|
||||
{
|
||||
self.map_kind_with(|_| kind)
|
||||
}
|
||||
|
||||
/// Map the error message
|
||||
fn map_msg_with<M, F: FnOnce(Option<Cow<'static, str>>) -> Option<M>>(self, msg: F) -> Result<T, Error>
|
||||
where M: fmt::Display + Send + Sync + 'static;
|
||||
/// Map the error message
|
||||
#[inline] fn map_msg<M>(self, msg: M) -> Result<T, Error>
|
||||
where M: fmt::Display + Send + Sync + 'static
|
||||
{
|
||||
self.map_msg_with(|_| Some(msg))
|
||||
}
|
||||
|
||||
#[inline] fn map_msg_remove(self) -> Result<T, Error>
|
||||
{
|
||||
self.map_msg_with(|_| NO_MESSAGE)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> WrapErr<T> for Result<T,Error>
|
||||
{
|
||||
fn map_kind_with<F: FnOnce(ErrorKind) -> ErrorKind>(self, kind: F) -> Result<T,Error>
|
||||
{
|
||||
self.map_err(|err| {
|
||||
Error {
|
||||
internal: box kind(*err.internal),
|
||||
..err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn map_msg_with<M, F: FnOnce(Option<Cow<'static, str>>) -> Option<M>>(self, msg: F) -> Result<T, Error>
|
||||
where M: fmt::Display + Send + Sync + 'static
|
||||
{
|
||||
self.map_err(|err| {
|
||||
let msg = msg(err.message.into_string());
|
||||
|
||||
Error {
|
||||
message: match msg {
|
||||
Some(msg) => Message::from_other(msg),
|
||||
None => Message::None,
|
||||
},
|
||||
..err
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue