add default

This commit is contained in:
Aiden McClelland
2021-07-29 13:52:51 -06:00
parent 1e9ded9a31
commit d1594997fd
8 changed files with 52 additions and 8 deletions

View File

@@ -5,6 +5,8 @@ name = "rpc-toolkit-macro-internals"
version = "0.1.0" version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
cli-cookies = []
[dependencies] [dependencies]
proc-macro2 = "1.0.26" proc-macro2 = "1.0.26"

View File

@@ -225,7 +225,11 @@ fn build_app(name: LitStr, opt: &mut Options, params: &mut [ParamType]) -> Token
modifications.extend(quote_spanned! { ty_span => modifications.extend(quote_spanned! { ty_span =>
arg = arg.takes_value(true); 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; arg.optional = true;
modifications.extend(quote_spanned! { ty_span => modifications.extend(quote_spanned! { ty_span =>
arg = arg.required(false); arg = arg.required(false);

View File

@@ -81,6 +81,7 @@ pub struct ArgOptions {
short: Option<LitStr>, short: Option<LitStr>,
long: Option<LitStr>, long: Option<LitStr>,
parse: Option<Path>, parse: Option<Path>,
default: Option<LitStr>,
count: Option<Path>, count: Option<Path>,
multiple: Option<Path>, multiple: Option<Path>,
stdin: Option<Path>, stdin: Option<Path>,

View File

@@ -394,6 +394,7 @@ pub fn parse_arg_attr(attr: Attribute, arg: PatType) -> Result<ArgOptions> {
short: None, short: None,
long: None, long: None,
parse: None, parse: None,
default: None,
count: None, count: None,
multiple: None, multiple: None,
stdin: 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") => { NestedMeta::Meta(Meta::Path(p)) if p.is_ident("long") => {
return Err(Error::new(p.span(), "`long` must be assigned to")); 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")); return Err(Error::new(arg.span(), "unknown argument"));
} }

View File

@@ -9,6 +9,9 @@ version = "0.1.0"
[lib] [lib]
proc-macro = true proc-macro = true
[features]
cli-cookies = ["rpc-toolkit-macro-internals/cli-cookies"]
[dependencies] [dependencies]
proc-macro2 = "1.0.1" proc-macro2 = "1.0.1"
rpc-toolkit-macro-internals = { path = "../rpc-toolkit-macro-internals" } rpc-toolkit-macro-internals = { path = "../rpc-toolkit-macro-internals" }

View File

@@ -8,18 +8,26 @@ version = "0.1.0"
[features] [features]
cbor = ["serde_cbor"] cbor = ["serde_cbor"]
default = ["cbor"] default = ["cbor"]
cli-cookies = ["rpc-toolkit-macro/cli-cookies"]
[dependencies] [dependencies]
clap = "2.33.3" clap = "2.33.3"
futures = "0.3.15" futures = "0.3.15"
hyper = { version="0.14.5", features=["server", "http1", "http2", "tcp", "stream", "client"] } hyper = { version = "0.14.5", features = [
"server",
"http1",
"http2",
"tcp",
"stream",
"client",
] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
reqwest = { version="0.11.2" } reqwest = { version = "0.11.2" }
rpc-toolkit-macro = { path="../rpc-toolkit-macro" } rpc-toolkit-macro = { path = "../rpc-toolkit-macro" }
serde = { version="1.0.125", features=["derive"] } serde = { version = "1.0.125", features = ["derive"] }
serde_cbor = { version="0.11.1", optional=true } serde_cbor = { version = "0.11.1", optional = true }
serde_json = "1.0.64" serde_json = "1.0.64"
thiserror = "1.0.24" thiserror = "1.0.24"
tokio = { version="1.4.0", features=["full"] } tokio = { version = "1.4.0", features = ["full"] }
url = "2.2.1" url = "2.2.1"
yajrc = { version="*", path="../../yajrc" } yajrc = { version = "*", path = "../../yajrc" }

View File

@@ -0,0 +1 @@

View File

@@ -44,6 +44,8 @@ pub use {clap, hyper, reqwest, serde, serde_json, tokio, url, yajrc};
pub use crate::context::Context; pub use crate::context::Context;
pub use crate::metadata::Metadata; pub use crate::metadata::Metadata;
#[cfg(feature = "cli-cookies")]
pub mod cli_helpers;
pub mod command_helpers; pub mod command_helpers;
mod context; mod context;
mod metadata; mod metadata;