Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lexopt = "0.3.0"

[features]
parse-is-complete = ["complete"]
complete = ["uutils-args-complete"]
complete = ["uutils-args-complete", "uutils-args-derive/complete"]

[workspace]
members = ["derive", "complete"]
3 changes: 3 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ proc-macro = true
proc-macro2 = "1.0.81"
quote = "1.0.36"
syn = { version = "2.0.60", features = ["full"] }

[features]
complete = []
48 changes: 33 additions & 15 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ pub fn arguments(input: TokenStream) -> TokenStream {
quote!(Ok(Some(::uutils_args::Argument::Positional(value))))
};

let complete_fn = if cfg!(feature = "complete") {
quote!(
fn complete() -> ::uutils_args_complete::Command<'static> {
use ::uutils_args::Value;
#complete_command
}
)
} else {
quote!()
};

let expanded = quote!(
impl #impl_generics Arguments for #name #ty_generics #where_clause {
const EXIT_CODE: i32 = #exit_code;
Expand Down Expand Up @@ -117,11 +128,10 @@ pub fn arguments(input: TokenStream) -> TokenStream {
#version_string
}

#[cfg(feature = "complete")]
fn complete() -> ::uutils_args_complete::Command<'static> {
use ::uutils_args::Value;
#complete_command
}
// "#[cfg(feature = "complete")] fn complete()"
// However, this would attempt to evaluate the feature "complete" in the context of the caller.
// Therefore, we must evaluate whether the feature is true or not while this macro is running.
#complete_fn
}
);

Expand Down Expand Up @@ -176,6 +186,22 @@ pub fn value(input: TokenStream) -> TokenStream {

let keys_len = all_keys.len();

let value_hint_fn = if cfg!(feature = "complete") {
quote!(
fn value_hint() -> ::uutils_args_complete::ValueHint {
let keys: [&str; #keys_len] = [#(#all_keys),*];
::uutils_args_complete::ValueHint::Strings(
keys
.into_iter()
.map(ToString::to_string)
.collect()
)
}
)
} else {
quote!()
};

let expanded = quote!(
impl #impl_generics Value for #name #ty_generics #where_clause {
fn from_value(value: &::std::ffi::OsStr) -> ::uutils_args::ValueResult<Self> {
Expand Down Expand Up @@ -212,16 +238,8 @@ pub fn value(input: TokenStream) -> TokenStream {
})
}

#[cfg(feature = "complete")]
fn value_hint() -> ::uutils_args_complete::ValueHint {
let keys: [&str; #keys_len] = [#(#all_keys),*];
::uutils_args_complete::ValueHint::Strings(
keys
.into_iter()
.map(ToString::to_string)
.collect()
)
}
// See impl Arguments::complete (fn value_hint())
#value_hint_fn
}
);

Expand Down