Skip to content

Commit

Permalink
Remove source from default Display implementation
Browse files Browse the repository at this point in the history
Closes #356
  • Loading branch information
shepmaster committed Nov 28, 2023
1 parent e4c84fb commit 5987482
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use std::{fs, io, path::PathBuf};

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Unable to read configuration from {}: {}", path.display(), source))]
#[snafu(display("Unable to read configuration from {}", path.display()))]
ReadConfiguration { source: io::Error, path: PathBuf },

#[snafu(display("Unable to write result to {}: {}", path.display(), source))]
#[snafu(display("Unable to write result to {}", path.display()))]
WriteResult { source: io::Error, path: PathBuf },
}

Expand Down
12 changes: 4 additions & 8 deletions snafu-derive/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,23 +545,19 @@ pub mod display {
let mut shorthand_names = &BTreeSet::new();
let mut assigned_names = &BTreeSet::new();

let format = match (display_format, doc_comment, source_field) {
(Some(v), _, _) => {
let format = match (display_format, doc_comment) {
(Some(v), _) => {
let exprs = &v.exprs;
shorthand_names = &v.shorthand_names;
assigned_names = &v.assigned_names;
quote! { #(#exprs),* }
}
(_, Some(d), _) => {
(_, Some(d)) => {
let content = &d.content;
shorthand_names = &d.shorthand_names;
quote! { #content }
}
(_, _, Some(f)) => {
let field_name = &f.name;
quote! { concat!(stringify!(#default_name), ": {}"), #field_name }
}
_ => quote! { stringify!(#default_name)},
_ => quote! { stringify!(#default_name) },
};

let field_names = super::AllFieldNames(field_container).field_names();
Expand Down
22 changes: 11 additions & 11 deletions src/Snafu.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ it is valid. Detailed information on each attribute is below.

### Enum variant or struct

| Option (inside `#[snafu(...)]`) | Description |
|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `display("{field:?}: {}", foo)` | Sets the display implementation for this error variant using `format_args!` syntax. If this is omitted, the default is `"VariantName: {source}"` if there is a source or `"VariantName"` if not |
| `context(false)` | Skips creation of the context selector, implements `From` for the mandatory source error |
| `context(suffix(N))` | Changes the suffix of the generated context selector to `N` |
| `context(suffix(false))` | No suffix for the generated context selector |
| `transparent` | Delegates `Display` and `Error::source` to this error's source, implies `context(false)` |
| `visibility(v)` | Sets the visibility of the generated context selector to `v` (e.g. `pub`) |
| `visibility` | Resets visibility back to private |
| `provide(flags, type => expr)` | Provides the type using the `expr` with the optional flags |
| `whatever` | Stringly-typed error. Message field must be called `message`. Source optional, but if present must be of a specific [format](#controlling-stringly-typed-errors) |
| Option (inside `#[snafu(...)]`) | Description |
|---------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `display("{field:?}: {}", foo)` | Sets the display implementation for this error variant using `format_args!` syntax. If this is omitted, the default is `"VariantName" |
| `context(false)` | Skips creation of the context selector, implements `From` for the mandatory source error |
| `context(suffix(N))` | Changes the suffix of the generated context selector to `N` |
| `context(suffix(false))` | No suffix for the generated context selector |
| `transparent` | Delegates `Display` and `Error::source` to this error's source, implies `context(false)` |
| `visibility(v)` | Sets the visibility of the generated context selector to `v` (e.g. `pub`) |
| `visibility` | Resets visibility back to private |
| `provide(flags, type => expr)` | Provides the type using the `expr` with the optional flags |
| `whatever` | Stringly-typed error. Message field must be called `message`. Source optional, but if present must be of a specific [format](#controlling-stringly-typed-errors) |

### Context fields

Expand Down
4 changes: 2 additions & 2 deletions src/guide/comparison/failure.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ use std::num::ParseIntError;

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display(r#"Could not parse the area code from "{value}": {source}"#))]
#[snafu(display(r#"Could not parse the area code from "{value}""#))]
AreaCodeInvalid {
value: String,
source: ParseIntError,
},

#[snafu(display(r#"Could not parse the phone exchange from "{value}": {source}"#))]
#[snafu(display(r#"Could not parse the phone exchange from "{value}""#))]
PhoneExchangeInvalid {
value: String,
source: ParseIntError,
Expand Down
2 changes: 1 addition & 1 deletion src/guide/troubleshooting/missing_field_source.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
pub enum ProjectError {
#[snafu(display("Unable to read configuration from {path}: {source}"))]
#[snafu(display("Unable to read configuration from {path}"))]
IOConfigError {
path: &'static str,
source: io::Error,
Expand Down
3 changes: 1 addition & 2 deletions src/guide/what_code_is_generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ impl std::fmt::Display for Error {
```

If no display format is specified, the variant's name will be used
by default. If the field is an underlying error, that error's
`Display` implementation will also be included.
by default.

#### `ErrorCompat`

Expand Down
2 changes: 1 addition & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Could not open config file at {}: {}", filename.display(), source))]
#[snafu(display("Could not open config file at {}", filename.display()))]
OpenConfig {
filename: PathBuf,
source: io::Error,
Expand Down
2 changes: 1 addition & 1 deletion tests/default_error_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ fn default_error_display() {
.fail::<()>()
.context(NoDisplaySnafu)
.unwrap_err();
assert_eq!(format!("{}", err), "NoDisplay: inner error",);
assert_eq!(err.to_string(), "NoDisplay");
}
2 changes: 1 addition & 1 deletion tests/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn implements_error() {
fn implements_display() {
let error = example().unwrap_err();
// no `Beta: ` prefix
assert_eq!(error.to_string(), "BetaError: AlphaError");
assert_eq!(error.to_string(), "BetaError");
}

mod with_backtraces {
Expand Down

0 comments on commit 5987482

Please sign in to comment.