This commit is contained in:
Luiz Felipe Frazão 2026-06-03 13:58:11 +09:00 committed by GitHub
commit 4b3f009cd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

View File

@ -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,

View File

@ -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()
);
}