Skip to content

Commit

Permalink
Fix display impl for empty enum and non provided enum (#11)
Browse files Browse the repository at this point in the history
* Add no_display attribute
* Add tests with trybuild
* Fix doc
  • Loading branch information
Mifom authored Apr 24, 2023
1 parent a7edf5b commit 937a14f
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 29 deletions.
159 changes: 159 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ std = []
[lib]
proc-macro = true

[[test]]
name = "compile_and_fail"
path = "compile_tests/compiler.rs"

[dependencies]
myn = "0.1"

[dev-dependencies]
error-iter = "0.4"
rustversion = "1.0.12"
trybuild = "1.0.80"
15 changes: 15 additions & 0 deletions compile_tests/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[test]
fn compile_tests() {
let t = trybuild::TestCases::new();
t.pass("compile_tests/empty.rs");
t.pass("compile_tests/one_comment.rs");
t.pass("compile_tests/one_param.rs");
t.compile_fail("compile_tests/one_non_signed.rs");
t.pass("compile_tests/multiple_variant.rs");
t.compile_fail("compile_tests/multiple_non_signed.rs");
t.compile_fail("compile_tests/multiple_one_non_signed.rs");
t.pass("compile_tests/no_display.rs");
if rustversion::cfg!(since(1.68.0)) {
t.compile_fail("compile_tests/no_display_no_impl.rs");
}
}
4 changes: 4 additions & 0 deletions compile_tests/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(Debug, onlyerror::Error)]
enum Error {}

fn main() {}
8 changes: 8 additions & 0 deletions compile_tests/multiple_non_signed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[derive(Debug, onlyerror::Error)]
enum Error {
First,
Second(usize),
Third { key: String, value: Vec<usize> },
}

fn main() {}
5 changes: 5 additions & 0 deletions compile_tests/multiple_non_signed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Required error message is missing
--> compile_tests/multiple_non_signed.rs:3:5
|
3 | First,
| ^^^^^
13 changes: 13 additions & 0 deletions compile_tests/multiple_one_non_signed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[derive(Debug, onlyerror::Error)]
enum Error {
/// First
First,
#[error("Second with {0}")]
Second(usize),
Third {
key: String,
value: Vec<usize>,
},
}

fn main() {}
5 changes: 5 additions & 0 deletions compile_tests/multiple_one_non_signed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Required error message is missing
--> compile_tests/multiple_one_non_signed.rs:7:5
|
7 | Third {
| ^^^^^
13 changes: 13 additions & 0 deletions compile_tests/multiple_variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
enum Error {
/// First
First,
#[error("Second with {0}")]
Second(usize),
#[error("Third with {key} and {value:?}")]
Third { key: String, value: Vec<usize> },
}

fn main() {}
17 changes: 17 additions & 0 deletions compile_tests/no_display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
#[no_display]
enum Error {
First,
Second(usize),
Third { key: String, value: Vec<usize> },
}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!(f, "Should work")
}
}

fn main() {}
14 changes: 14 additions & 0 deletions compile_tests/no_display_no_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
#[no_display]
enum Error {
/// First
First,
#[error("Second with {0}")]
Second(usize),
#[error("Third with {key} and {value:?}")]
Third { key: String, value: Vec<usize> },
}

fn main() {}
11 changes: 11 additions & 0 deletions compile_tests/no_display_no_impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0277]: `Error` doesn't implement `std::fmt::Display`
--> compile_tests/no_display_no_impl.rs:3:17
|
3 | #[derive(Debug, onlyerror::Error)]
| ^^^^^^^^^^^^^^^^ `Error` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Error`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `std::error::Error`
--> $RUST/core/src/error.rs
= note: this error originates in the derive macro `onlyerror::Error` (in Nightly builds, run with -Z macro-backtrace for more info)
9 changes: 9 additions & 0 deletions compile_tests/one_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
enum Error {
/// One
One,
}

fn main() {}
6 changes: 6 additions & 0 deletions compile_tests/one_non_signed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Debug, onlyerror::Error)]
enum Error {
One,
}

fn main() {}
5 changes: 5 additions & 0 deletions compile_tests/one_non_signed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: Required error message is missing
--> compile_tests/one_non_signed.rs:3:5
|
3 | One,
| ^^^
9 changes: 9 additions & 0 deletions compile_tests/one_param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(dead_code)]

#[derive(Debug, onlyerror::Error)]
enum Error {
#[error("One")]
One,
}

fn main() {}
Loading

0 comments on commit 937a14f

Please sign in to comment.