mirror of https://github.com/zkat/miette.git
feat(derive): improved derive support, including partial help format string support!
This commit is contained in:
parent
9a78a94395
commit
9ef0dd261f
|
|
@ -21,11 +21,37 @@ impl Parse for Help {
|
|||
if la.peek(syn::token::Paren) {
|
||||
let content;
|
||||
parenthesized!(content in input);
|
||||
let str = content.parse::<syn::LitStr>()?;
|
||||
Ok(Help {
|
||||
fmt: str.value(),
|
||||
args: Vec::new(),
|
||||
})
|
||||
let mut fmt = None;
|
||||
let mut args = Vec::new();
|
||||
let punc = syn::punctuated::Punctuated::<syn::Expr, Token![,]>::parse_terminated(
|
||||
&content,
|
||||
)?;
|
||||
for (i, arg) in punc.into_iter().enumerate() {
|
||||
if i == 0 {
|
||||
if let syn::Expr::Lit(syn::ExprLit {
|
||||
lit: syn::Lit::Str(str),
|
||||
..
|
||||
}) = arg
|
||||
{
|
||||
fmt = Some(str.value());
|
||||
}
|
||||
} else {
|
||||
args.push(arg);
|
||||
}
|
||||
}
|
||||
if let Some(fmt) = fmt {
|
||||
Ok(Help { fmt, args })
|
||||
} else if !args.is_empty() {
|
||||
Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"The first arg to help() must be a literal format string.",
|
||||
))
|
||||
} else {
|
||||
Err(syn::Error::new(
|
||||
ident.span(),
|
||||
"help() format string is required",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
input.parse::<Token![=]>()?;
|
||||
Ok(Help {
|
||||
|
|
@ -53,9 +79,9 @@ impl Help {
|
|||
ref fields,
|
||||
..
|
||||
}| {
|
||||
let help = &help.as_ref().unwrap();
|
||||
let fmt = &help.fmt;
|
||||
let args = help.args.iter().map(|arg| quote! { #arg, });
|
||||
let help = &help.as_ref().unwrap();
|
||||
let fmt = &help.fmt;
|
||||
let args = &help.args;
|
||||
match fields {
|
||||
syn::Fields::Named(_) => {
|
||||
quote! { Self::#ident{..} => std::option::Option::Some(std::boxed::Box::new(format!(#fmt, #(#args),*))), }
|
||||
|
|
|
|||
|
|
@ -132,7 +132,6 @@ fn list_help() {
|
|||
);
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn fmt_help() {
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
|
|
@ -144,8 +143,8 @@ fn fmt_help() {
|
|||
struct FooStruct(String);
|
||||
|
||||
assert_eq!(
|
||||
"1 bar".to_string(),
|
||||
FooStruct.help().unwrap().to_string()
|
||||
"1 hello".to_string(),
|
||||
FooStruct("hello".into()).help().unwrap().to_string()
|
||||
);
|
||||
|
||||
#[derive(Debug, Diagnostic, Error)]
|
||||
|
|
@ -163,4 +162,3 @@ fn fmt_help() {
|
|||
FooEnum::X.help().unwrap().to_string()
|
||||
);
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue