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) {
|
if la.peek(syn::token::Paren) {
|
||||||
let content;
|
let content;
|
||||||
parenthesized!(content in input);
|
parenthesized!(content in input);
|
||||||
let str = content.parse::<syn::LitStr>()?;
|
let mut fmt = None;
|
||||||
Ok(Help {
|
let mut args = Vec::new();
|
||||||
fmt: str.value(),
|
let punc = syn::punctuated::Punctuated::<syn::Expr, Token![,]>::parse_terminated(
|
||||||
args: Vec::new(),
|
&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 {
|
} else {
|
||||||
input.parse::<Token![=]>()?;
|
input.parse::<Token![=]>()?;
|
||||||
Ok(Help {
|
Ok(Help {
|
||||||
|
|
@ -53,9 +79,9 @@ impl Help {
|
||||||
ref fields,
|
ref fields,
|
||||||
..
|
..
|
||||||
}| {
|
}| {
|
||||||
let help = &help.as_ref().unwrap();
|
let help = &help.as_ref().unwrap();
|
||||||
let fmt = &help.fmt;
|
let fmt = &help.fmt;
|
||||||
let args = help.args.iter().map(|arg| quote! { #arg, });
|
let args = &help.args;
|
||||||
match fields {
|
match fields {
|
||||||
syn::Fields::Named(_) => {
|
syn::Fields::Named(_) => {
|
||||||
quote! { Self::#ident{..} => std::option::Option::Some(std::boxed::Box::new(format!(#fmt, #(#args),*))), }
|
quote! { Self::#ident{..} => std::option::Option::Some(std::boxed::Box::new(format!(#fmt, #(#args),*))), }
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,6 @@ fn list_help() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fmt_help() {
|
fn fmt_help() {
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
|
|
@ -144,8 +143,8 @@ fn fmt_help() {
|
||||||
struct FooStruct(String);
|
struct FooStruct(String);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
"1 bar".to_string(),
|
"1 hello".to_string(),
|
||||||
FooStruct.help().unwrap().to_string()
|
FooStruct("hello".into()).help().unwrap().to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
|
|
@ -163,4 +162,3 @@ fn fmt_help() {
|
||||||
FooEnum::X.help().unwrap().to_string()
|
FooEnum::X.help().unwrap().to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue