GeezerVison is showing github.com — feat(derive): bind value enums directly · jdx/usage@f236c01
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: PLAN.md +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -518,6 +518,9 @@ Groups are the opposite case: `Command::get_groups`, `ArgGroup::get_args` and | 518 | 518 | a bare flag, and an explicit value. The derive infers zero-or-one value arity, | 519 | 519 | usage-argv binds all three states, and emitted KDL/help use an optional | 520 | 520 | placeholder without requiring a synthetic `default_missing`. | | 521 | +- [x] **Direct `ValueEnum` binding.** The derive converts canonical words and | | 522 | + aliases directly to variants, including case-insensitive declarations, so a | | 523 | + clap migration does not need to add a redundant `FromStr` implementation. | 521 | 524 |
| 522 | 525 | **Help output** | 523 | 526 |
| |
Collapse file Copy file name to clipboardExpand all lines: argv/src/spec.rs +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2383,6 +2383,8 @@ pub trait ValueEnum: Sized { | 2383 | 2383 | /// Presentation metadata for canonical values and aliases. | 2384 | 2384 | const DETAILS: &'static [ChoiceMeta<'static>] = &[]; | 2385 | 2385 | const IGNORE_CASE: bool = false; | | 2386 | + /// Convert one canonical word or alias into its enum variant. | | 2387 | + fn from_choice(value: &str) -> Option<Self>; | 2386 | 2388 | } | 2387 | 2389 |
| 2388 | 2390 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
Collapse file Copy file name to clipboardExpand all lines: derive/src/codegen.rs +42-3Lines changed: 42 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2987,6 +2987,27 @@ fn field_final(field: &Field) -> TokenStream { | 2987 | 2987 | } | 2988 | 2988 | } | 2989 | 2989 | }; | | 2990 | + if field.value_enum { | | 2991 | + return quote! {{ | | 2992 | + let __usage_text = #text; | | 2993 | + match <#ty as usage_argv::spec::ValueEnum>::from_choice(&__usage_text) { | | 2994 | + ::std::option::Option::Some(parsed) => parsed, | | 2995 | + ::std::option::Option::None => { | | 2996 | + return ::std::result::Result::Err( | | 2997 | + usage_argv::Error::InvalidValue(::std::boxed::Box::new( | | 2998 | + usage_argv::InvalidValue { | | 2999 | + name: #name, | | 3000 | + value: __usage_text, | | 3001 | + reason: ::std::string::String::from( | | 3002 | + "not one of the declared values", | | 3003 | + ), | | 3004 | + }, | | 3005 | + )), | | 3006 | + ); | | 3007 | + } | | 3008 | + } | | 3009 | + }}; | | 3010 | + } | 2990 | 3011 | if is_std_string { | 2991 | 3012 | return text; | 2992 | 3013 | } | @@ -5632,9 +5653,9 @@ fn post_binding(cli: &Cli) -> TokenStream { | 5632 | 5653 |
| 5633 | 5654 | /// The word list for a value enum. | 5634 | 5655 | /// | 5635 | | -/// Conversion remains the type's own `FromStr` implementation, like every other typed | 5636 | | -/// field. This derive owns only CLI metadata, so it can coexist with a domain parser whose | 5637 | | -/// accepted syntax or error type is broader than the choices presented in help. | | 5656 | +/// Conversion is generated from the same canonical words and aliases as the metadata, so | | 5657 | +/// parsing, help, and completion cannot drift. A separate domain `FromStr` implementation | | 5658 | +/// may still coexist; value-enum fields do not call it. | 5638 | 5659 | pub fn emit_value_enum(value_enum: &ValueEnum) -> TokenStream { | 5639 | 5660 | let ident = &value_enum.ident; | 5640 | 5661 | let runtime = runtime_path(); | @@ -5684,6 +5705,19 @@ pub fn emit_value_enum(value_enum: &ValueEnum) -> TokenStream { | 5684 | 5705 | ) | 5685 | 5706 | }); | 5686 | 5707 | let ignore_case = value_enum.ignore_case; | | 5708 | + let parse_arms = value_enum.variants.iter().map(|value| { | | 5709 | + let ident = &value.ident; | | 5710 | + let cfg = &value.cfg_attrs; | | 5711 | + let names = ::std::iter::once(&value.name).chain(value.aliases.iter().map(|a| &a.name)); | | 5712 | + quote! { | | 5713 | + #(#cfg)* | | 5714 | + if [#(#names),*].iter().any(|candidate| { | | 5715 | + *candidate == value || #ignore_case && candidate.eq_ignore_ascii_case(value) | | 5716 | + }) { | | 5717 | + return ::std::option::Option::Some(Self::#ident); | | 5718 | + } | | 5719 | + } | | 5720 | + }); | 5687 | 5721 |
| 5688 | 5722 | quote! { | 5689 | 5723 | #[doc(hidden)] | @@ -5696,6 +5730,11 @@ pub fn emit_value_enum(value_enum: &ValueEnum) -> TokenStream { | 5696 | 5730 | const ALIASES: &'static [(&'static str, &'static str)] = &[#(#aliases),*]; | 5697 | 5731 | const DETAILS: &'static [usage_argv::spec::ChoiceMeta<'static>] = &[#(#details),*]; | 5698 | 5732 | const IGNORE_CASE: bool = #ignore_case; | | 5733 | + | | 5734 | + fn from_choice(value: &str) -> ::std::option::Option<Self> { | | 5735 | + #(#parse_arms)* | | 5736 | + ::std::option::Option::None | | 5737 | + } | 5699 | 5738 | } | 5700 | 5739 | }; | 5701 | 5740 | } | |
Collapse file Copy file name to clipboardExpand all lines: derive/src/lib.rs +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -416,9 +416,9 @@ pub fn derive_subcommands(input: TokenStream) -> TokenStream { | 416 | 416 | /// it, `hide` keeps the value accepted while omitting it from help and completion, | 417 | 417 | /// `alias` is hidden, and `visible_alias` is advertised alongside the canonical word. | 418 | 418 | /// | 419 | | -/// The enum must implement [`FromStr`](std::str::FromStr); this derive owns only its CLI | 420 | | -/// word metadata and does not replace domain parsing. Variant `cfg` and `cfg_attr` | 421 | | -/// attributes are copied to their entries in the static word tables. | | 419 | +/// The derive binds canonical words and aliases directly to their variants; a separate | | 420 | +/// [`FromStr`](std::str::FromStr) implementation is not required. Variant `cfg` and | | 421 | +/// `cfg_attr` attributes are copied to their entries in the static word tables. | 422 | 422 | /// | 423 | 423 | /// A field holding one says `value_enum`, which is what puts the words in the spec — so | 424 | 424 | /// help, completions and the check that rejects a wrong word all read the same list, and | |
Collapse file Copy file name to clipboardExpand all lines: docs/rust/clap-compatibility.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,7 @@ the Rust declaration, not only from generated KDL, wherever the bridge column sa | 58 | 58 | | explicit `id` | yes | yes | yes | yes | yes | yes | `#[arg(id = "…")]` supplies the stable field identity used by relationships and generated specs. | | 59 | 59 | | positional arguments | yes | yes | yes | yes | yes | yes | Required, optional, and variadic positionals are supported. | | 60 | 60 | | `Option<T>`, `Vec<T>`, `Option<Vec<T>>`, `Option<Option<T>>` | yes | yes | yes | yes | yes | n/a | Nested `Option` preserves absent, bare, and explicitly valued flags; values use `FromStr`. | | | 61 | +| derived `ValueEnum` | yes | yes | yes | yes | yes | n/a | Canonical values, aliases, case policy, help, and direct enum binding come from one derive; no separate `FromStr` is required. | | 61 | 62 | | `ArgAction::Set`, `SetTrue`, `SetFalse`, `Append`, `Count` | yes | yes | yes | yes | yes | lossy | Common typed shapes are covered; arbitrary action/type combinations are not. | | 62 | 63 | | `default_value` | yes | yes | yes | yes | yes | yes | Defaults apply after argv and environment values and clear token-required metadata. | | 63 | 64 | | `default_missing_value` | yes | yes | yes | yes | yes | usage-only | `#[usage(default_missing = "…")]`; clap has no getter. | | |
Collapse file Copy file name to clipboardExpand all lines: docs/rust/subcommands.md +3-2Lines changed: 3 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -175,8 +175,9 @@ hidden spelling, and `visible_alias` advertises the alternate spelling. Plural a | 175 | 175 | accept lists. `#[usage(ignore_case)]` applies to the whole enum, and `cfg`-gated variants remain | 176 | 176 | gated in every generated metadata table. | 177 | 177 |
| 178 | | -The type still implements its own `FromStr`: `ValueEnum` owns the portable CLI metadata and | 179 | | -does not replace domain parsing. | | 178 | +`ValueEnum` also binds those words directly to their variants, so the type needs no separate | | 179 | +`FromStr`. An existing domain `FromStr` implementation may still coexist; value-enum fields | | 180 | +use the derive's canonical values and aliases. | 180 | 181 |
| 181 | 182 | ## Mounts and restart tokens | 182 | 183 |
| |
Collapse file Copy file name to clipboardExpand all lines: usage-rs/tests/facade.rs +3-18Lines changed: 3 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -317,24 +317,6 @@ enum Shell { | 317 | 317 | PowerShell, | 318 | 318 | } | 319 | 319 |
| 320 | | -impl std::str::FromStr for Shell { | 321 | | - type Err = String; | 322 | | - | 323 | | - fn from_str(value: &str) -> Result<Self, Self::Err> { | 324 | | - #[cfg(windows)] | 325 | | - if value.eq_ignore_ascii_case("power-shell") { | 326 | | - return Ok(Self::PowerShell); | 327 | | - } | 328 | | - if value.eq_ignore_ascii_case("bash") { | 329 | | - Ok(Self::Bash) | 330 | | - } else if value.eq_ignore_ascii_case("zsh") || value.eq_ignore_ascii_case("shell-z") { | 331 | | - Ok(Self::Zsh) | 332 | | - } else { | 333 | | - Err(format!("unsupported shell: {value}")) | 334 | | - } | 335 | | - } | 336 | | -} | 337 | | - | 338 | 320 | #[derive(Cli)] | 339 | 321 | #[usage(bin = "choice-ex")] | 340 | 322 | struct ChoiceEx { | @@ -696,6 +678,9 @@ fn emitted_specs_preserve_value_enum_aliases_and_case_policy() { | 696 | 678 | let cli = ChoiceEx::parse_from(&[OsStr::new("--shell"), OsStr::new("SHELL-Z")]) | 697 | 679 | .expect("aliases and ASCII case folding should parse"); | 698 | 680 | assert!(matches!(cli.shell, Shell::Zsh)); | | 681 | + let cli = ChoiceEx::parse_from(&[OsStr::new("--shell"), OsStr::new("Z-SHELL")]) | | 682 | + .expect("every declared alias should bind without a separate FromStr"); | | 683 | + assert!(matches!(cli.shell, Shell::Zsh)); | 699 | 684 |
| 700 | 685 | let kdl = ChoiceEx::to_kdl(); | 701 | 686 | assert!(kdl.contains("choices ignore_case=#true"), "{kdl}"); | |
|