From 5987482b760a58d6a6c234a94d4fca80a5951ec8 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Tue, 28 Nov 2023 15:42:22 -0500 Subject: [PATCH] Remove source from default `Display` implementation Closes #356 --- README.md | 4 ++-- snafu-derive/src/shared.rs | 12 ++++------ src/Snafu.md | 22 +++++++++---------- src/guide/comparison/failure.md | 4 ++-- .../troubleshooting/missing_field_source.md | 2 +- src/guide/what_code_is_generated.md | 3 +-- tests/basic.rs | 2 +- tests/default_error_display.rs | 2 +- tests/transparent.rs | 2 +- 9 files changed, 24 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 040698cf..d9af1413 100644 --- a/README.md +++ b/README.md @@ -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 }, } diff --git a/snafu-derive/src/shared.rs b/snafu-derive/src/shared.rs index 2774487b..b016671b 100644 --- a/snafu-derive/src/shared.rs +++ b/snafu-derive/src/shared.rs @@ -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(); diff --git a/src/Snafu.md b/src/Snafu.md index 3ad6b027..b6eecdc0 100644 --- a/src/Snafu.md +++ b/src/Snafu.md @@ -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 diff --git a/src/guide/comparison/failure.md b/src/guide/comparison/failure.md index f909bea7..321b0694 100644 --- a/src/guide/comparison/failure.md +++ b/src/guide/comparison/failure.md @@ -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, diff --git a/src/guide/troubleshooting/missing_field_source.md b/src/guide/troubleshooting/missing_field_source.md index 10fe2384..c3546884 100644 --- a/src/guide/troubleshooting/missing_field_source.md +++ b/src/guide/troubleshooting/missing_field_source.md @@ -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, diff --git a/src/guide/what_code_is_generated.md b/src/guide/what_code_is_generated.md index 04519f13..5bcb343a 100644 --- a/src/guide/what_code_is_generated.md +++ b/src/guide/what_code_is_generated.md @@ -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` diff --git a/tests/basic.rs b/tests/basic.rs index 9970823b..591671fb 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -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, diff --git a/tests/default_error_display.rs b/tests/default_error_display.rs index b9e4758b..6e9381c6 100644 --- a/tests/default_error_display.rs +++ b/tests/default_error_display.rs @@ -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"); } diff --git a/tests/transparent.rs b/tests/transparent.rs index 8a9c49ff..72235f4a 100644 --- a/tests/transparent.rs +++ b/tests/transparent.rs @@ -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 {