Skip to content

Display with self #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 65 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@
//! }
//! ```
//!
//! If you need a reference to the error when `Display`ing, you can instead use
//! `display(x) -> (pattern, ..args)`, where `x` sets the name of the reference.
//!
//! ```rust
//! # #[macro_use] extern crate quick_error;
//! # fn main() {}
//! #
//! use std::error::Error; // put methods like `description()` of this trait into scope
//!
//! quick_error! {
//! #[derive(Debug)]
//! pub enum SomeError {
//! Io(err: std::io::Error) {
//! display(x) -> ("{}: {}", x.description(), err)
//! }
//! Utf8(err: std::str::Utf8Error) {
//! display(self_) -> ("{}, valid up to {}", self_.description(), err.valid_up_to())
//! }
//! }
//! }
//! ```
//!
//! To convert to the type from any other, use one of the three forms of
//! `from` clause.
//!
Expand Down Expand Up @@ -181,7 +203,6 @@
//! Empty braces can be omitted as of quick_error 0.1.3.
//!


/// Main macro that does all the work
#[macro_export]
macro_rules! quick_error {
Expand Down Expand Up @@ -376,9 +397,11 @@ macro_rules! quick_error {
match self {
$(
&$name::$item $( ( $(ref $var),* ) )* => {
quick_error!(FIND_DISPLAY_IMPL
$item self fmt [ $( ( $($var)* ) )* ]
{ $($funcs)* })
let display_fn = quick_error!(FIND_DISPLAY_IMPL
$name $item
{ $($funcs)* });

display_fn(self, fmt)
}
)*
}
Expand Down Expand Up @@ -415,25 +438,34 @@ macro_rules! quick_error {
{ $($funcs)* });
)*
};
(FIND_DISPLAY_IMPL $item:ident $me:ident $fmt:ident
[ $( ( $($var:ident)* ) )* ]
{ display($($exprs:tt)*) $($tail:tt)* }
(FIND_DISPLAY_IMPL $name:ident $item:ident
{ display($self_:tt) -> ($($exprs:tt)*) $($tail:tt)* }
) => {
write!($fmt, $($exprs)*)
|quick_error!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| { write!(f, $($exprs)*) }
};
(FIND_DISPLAY_IMPL $item:ident $me:ident $fmt:ident
[ $( ( $($var:ident)* ) )* ]
(FIND_DISPLAY_IMPL $name:ident $item:ident
{ display($pattern:expr) $($tail:tt)* }
) => {
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) }
};
(FIND_DISPLAY_IMPL $name:ident $item:ident
{ display($pattern:expr, $($exprs:tt)*) $($tail:tt)* }
) => {
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $($exprs)*) }
};
(FIND_DISPLAY_IMPL $name:ident $item:ident
{ $t:tt $($tail:tt)* }
) => {
quick_error!(FIND_DISPLAY_IMPL
$item $me $fmt [ $( ( $($var)* ) )* ]
$name $item
{ $($tail)* })
};
(FIND_DISPLAY_IMPL $item:ident $me:ident $fmt:ident
[ $( ( $($var:ident)* ) )* ]
(FIND_DISPLAY_IMPL $name:ident $item:ident
{ }
) => {
write!($fmt, "{}", ::std::error::Error::description($me))
|self_: &$name, f: &mut ::std::fmt::Formatter| {
write!(f, "{}", ::std::error::Error::description(self_))
}
};
(FIND_DESCRIPTION_IMPL $item:ident $me:ident $fmt:ident
[ $( ( $($var:ident)* ) )* ]
Expand Down Expand Up @@ -533,7 +565,11 @@ macro_rules! quick_error {
// anything else.
// This is to contrast FIND_* clauses which just find stuff they need and
// skip everything else completely
(ERROR_CHECK display($($exprs:tt)*) $($tail:tt)*)
(ERROR_CHECK display($self_:tt) -> ($($exprs:tt)*) $($tail:tt)*)
=> { quick_error!(ERROR_CHECK $($tail)*); };
(ERROR_CHECK display($pattern: expr) $($tail:tt)*)
=> { quick_error!(ERROR_CHECK $($tail)*); };
(ERROR_CHECK display($pattern: expr, $($exprs:tt)*) $($tail:tt)*)
=> { quick_error!(ERROR_CHECK $($tail)*); };
(ERROR_CHECK description($expr:expr) $($tail:tt)*)
=> { quick_error!(ERROR_CHECK $($tail)*); };
Expand All @@ -546,6 +582,8 @@ macro_rules! quick_error {
(ERROR_CHECK from($fvar:ident : $ftyp:ty) -> ($($e:expr),*) $($tail:tt)*)
=> { quick_error!(ERROR_CHECK $($tail)*); };
(ERROR_CHECK) => {};
// Utility functions
(IDENT $ident: ident) => { $ident }
}

#[cfg(test)]
Expand Down Expand Up @@ -594,14 +632,17 @@ mod test {
/// I/O error with some context
IoAt(place: &'static str, err: io::Error) {
cause(err)
display("Error at {}: {}", place, err)
display(self_) -> ("{} {}: {}", self_.description(), place, err)
description("io error at")
from(s: String) -> ("idea",
io::Error::new(io::ErrorKind::Other, s))
}
Discard {
from(&'static str)
}
Singleton {
display("Just a string")
}
}
}

Expand Down Expand Up @@ -633,7 +674,7 @@ mod test {
let err: &Error = &IoWrapper::IoAt("file",
io::Error::new(io::ErrorKind::NotFound, io1));
assert_eq!(format!("{}", err),
"Error at file: I/O error: some error".to_string());
"io error at file: I/O error: some error".to_string());
assert_eq!(format!("{:?}", err), "IoAt(\"file\", Error { \
repr: Custom(Custom { kind: NotFound, \
error: Io(Error { repr: Custom(Custom { \
Expand All @@ -654,7 +695,7 @@ mod test {
#[test]
fn io_wrapper_custom_from() {
let io1: IoWrapper = From::from("Stringy".to_string());
assert_eq!(format!("{}", io1), "Error at idea: Stringy".to_string());
assert_eq!(format!("{}", io1), "io error at idea: Stringy".to_string());
assert_eq!(io1.cause().unwrap().description(), "Stringy");
}

Expand All @@ -665,4 +706,10 @@ mod test {
assert!(io1.cause().is_none());
}

#[test]
fn io_wrapper_signleton() {
let io1: IoWrapper = IoWrapper::Singleton;
assert_eq!(format!("{}", io1), "Just a string".to_string());
}

}