mirror of
https://github.com/Start9Labs/rpc-toolkit.git
synced 2026-03-30 04:11:58 +00:00
add default
This commit is contained in:
@@ -5,6 +5,8 @@ name = "rpc-toolkit-macro-internals"
|
||||
version = "0.1.0"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
cli-cookies = []
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.26"
|
||||
|
||||
@@ -225,7 +225,11 @@ fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> Token
|
||||
modifications.extend(quote_spanned! { ty_span =>
|
||||
arg = arg.takes_value(true);
|
||||
});
|
||||
if p.path.segments.last().unwrap().ident == "Option" {
|
||||
if let Some(default) = &arg.default {
|
||||
modifications.extend(quote_spanned! { ty_span =>
|
||||
arg = arg.default_value(#default);
|
||||
});
|
||||
} else if p.path.segments.last().unwrap().ident == "Option" {
|
||||
arg.optional = true;
|
||||
modifications.extend(quote_spanned! { ty_span =>
|
||||
arg = arg.required(false);
|
||||
|
||||
@@ -81,6 +81,7 @@ pub struct ArgOptions {
|
||||
short: Option<LitStr>,
|
||||
long: Option<LitStr>,
|
||||
parse: Option<Path>,
|
||||
default: Option<LitStr>,
|
||||
count: Option<Path>,
|
||||
multiple: Option<Path>,
|
||||
stdin: Option<Path>,
|
||||
|
||||
@@ -394,6 +394,7 @@ pub fn parse_arg_attr(attr: Attribute, arg: PatType) -> Result<ArgOptions> {
|
||||
short: None,
|
||||
long: None,
|
||||
parse: None,
|
||||
default: None,
|
||||
count: None,
|
||||
multiple: None,
|
||||
stdin: None,
|
||||
@@ -627,6 +628,28 @@ pub fn parse_arg_attr(attr: Attribute, arg: PatType) -> Result<ArgOptions> {
|
||||
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("long") => {
|
||||
return Err(Error::new(p.span(), "`long` must be assigned to"));
|
||||
}
|
||||
NestedMeta::Meta(Meta::NameValue(nv)) if nv.path.is_ident("default") => {
|
||||
if let Lit::Str(default) = nv.lit {
|
||||
if opt.default.is_some() {
|
||||
return Err(Error::new(
|
||||
default.span(),
|
||||
"duplicate argument `default`",
|
||||
));
|
||||
}
|
||||
opt.default = Some(default);
|
||||
} else {
|
||||
return Err(Error::new(nv.lit.span(), "`default` must be a string"));
|
||||
}
|
||||
}
|
||||
NestedMeta::Meta(Meta::List(list)) if list.path.is_ident("default") => {
|
||||
return Err(Error::new(
|
||||
list.path.span(),
|
||||
"`default` does not take any arguments",
|
||||
));
|
||||
}
|
||||
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("default") => {
|
||||
return Err(Error::new(p.span(), "`default` must be assigned to"));
|
||||
}
|
||||
_ => {
|
||||
return Err(Error::new(arg.span(), "unknown argument"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user