mirror of https://github.com/zkat/miette.git
fix(derive/help): support field-level fmt interpolation
This commit is contained in:
parent
df7bcfa17d
commit
0a5877630c
|
|
@ -70,6 +70,23 @@ impl Help {
|
|||
for (i, field) in fields.iter().enumerate() {
|
||||
for attr in &field.attrs {
|
||||
if attr.path().is_ident("help") {
|
||||
if let syn::Meta::List(_) = &attr.meta {
|
||||
let display = attr.parse_args_with(|input: ParseStream| {
|
||||
let fmt = input.parse()?;
|
||||
let args = if input.is_empty() {
|
||||
TokenStream::new()
|
||||
} else {
|
||||
fmt::parse_token_expr(input, false)?
|
||||
};
|
||||
Ok(Display {
|
||||
fmt,
|
||||
args,
|
||||
has_bonus_display: false,
|
||||
})
|
||||
})?;
|
||||
return Ok(Some(Help::Display(display)));
|
||||
}
|
||||
|
||||
let help = if let Some(ident) = field.ident.clone() {
|
||||
syn::Member::Named(ident)
|
||||
} else {
|
||||
|
|
@ -84,6 +101,7 @@ impl Help {
|
|||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn gen_enum(variants: &[DiagnosticDef]) -> Option<TokenStream> {
|
||||
gen_all_variants_with(
|
||||
variants,
|
||||
|
|
|
|||
|
|
@ -632,3 +632,45 @@ fn test_optional_source_code() {
|
|||
.source_code()
|
||||
.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn field_fmt_help() {
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
#[error("welp")]
|
||||
struct Foo {
|
||||
#[help("do {suggestion} instead")]
|
||||
suggestion: String,
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
"do that instead".to_string(),
|
||||
Foo { suggestion: "that".into() }.help().unwrap().to_string()
|
||||
);
|
||||
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
#[error("welp")]
|
||||
enum Bar {
|
||||
#[error("variant")]
|
||||
Variant {
|
||||
#[help("do {suggestion} instead")]
|
||||
suggestion: String,
|
||||
},
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
"do that instead".to_string(),
|
||||
Bar::Variant { suggestion: "that".into() }.help().unwrap().to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn field_fmt_help_positional() {
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
#[error("welp")]
|
||||
struct Foo(#[help("do {0} instead")] String);
|
||||
|
||||
assert_eq!(
|
||||
"do that instead".to_string(),
|
||||
Foo("that".into()).help().unwrap().to_string()
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue