Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Allow variant with no #fail attribute #245

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ rust:
- nightly
cache: cargo
script:
- cargo test
- cargo test --all
- cargo test --features backtrace
- cargo check --no-default-features
13 changes: 6 additions & 7 deletions failure_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ fn fail_derive(s: synstructure::Structure) -> TokenStream {
}

fn display_body(s: &synstructure::Structure) -> Option<quote::__rt::TokenStream> {
let mut msgs = s.variants().iter().map(|v| find_error_msg(&v.ast().attrs));
if msgs.all(|msg| msg.is_none()) {
return None;
}

Some(s.each_variant(|v| {
let msg =
find_error_msg(&v.ast().attrs).expect("All variants must have display attribute.");
let msg = match find_error_msg(&v.ast().attrs) {
Some(msg) => msg,
None => {
return quote!(return write!(f, "{:?}", self));
}
};
if msg.nested.is_empty() {
panic!("Expected at least one argument to fail attribute");
}
Expand Down
24 changes: 24 additions & 0 deletions failure_derive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ fn enum_error() {
let s = format!("{}", EnumError::UnitVariant);
assert_eq!(&s[..], "An error has occurred.");
}

#[derive(Debug, Fail)]
enum EnumWithNoAttr {
#[fail(display = "Error: {}", _0)]
TupleVariant(usize),
UnitVariant,
}

#[test]
fn enum_with_no_attr() {
let s = format!("{}", EnumWithNoAttr::TupleVariant(4));
assert_eq!(&s[..], "Error: 4");
let s = format!("{}", EnumWithNoAttr::UnitVariant);
assert_eq!(&s[..], "UnitVariant");
}

#[derive(Debug, Fail)]
struct StructWithNoAttr;

#[test]
fn struct_with_no_attr() {
let s = format!("{}", StructWithNoAttr {});
assert_eq!(&s[..], "StructWithNoAttr");
}