Skip to content

Commit 8f8e861

Browse files
committed
docs(cookbook): Add local enum to typed-derive
1 parent 926bafe commit 8f8e861

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

examples/typed-derive/implicit.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,35 @@ $ typed-derive implicit --help
44
Usage: typed-derive implicit [OPTIONS]
55

66
Options:
7-
-O <OPTIMIZATION> Implicitly using `std::str::FromStr`
8-
-I <DIR> Allow invalid UTF-8 paths
9-
--bind <BIND> Handle IP addresses
10-
--sleep <SLEEP> Allow human-readable durations
11-
-h, --help Print help
7+
-O <OPTIMIZATION>
8+
Implicitly using `std::str::FromStr`
9+
10+
-I <DIR>
11+
Allow invalid UTF-8 paths
12+
13+
--bind <BIND>
14+
Handle IP addresses
15+
16+
--sleep <SLEEP>
17+
Allow human-readable durations
18+
19+
--bump-level <BUMP_LEVEL>
20+
Custom enums
21+
22+
Possible values:
23+
- major: Increase the major version (x.0.0)
24+
- minor: Increase the minor version (x.y.0)
25+
- patch: Increase the patch version (x.y.z)
26+
27+
-h, --help
28+
Print help (see a summary with '-h')
1229

1330
```
1431

1532
Optimization-level (number)
1633
```console
1734
$ typed-derive implicit -O 1
18-
Implicit(ImplicitParsers { optimization: Some(1), include: None, bind: None, sleep: None })
35+
Implicit(ImplicitParsers { optimization: Some(1), include: None, bind: None, sleep: None, bump_level: None })
1936

2037
$ typed-derive implicit -O plaid
2138
? failed
@@ -28,14 +45,14 @@ For more information, try '--help'.
2845
Include (path)
2946
```console
3047
$ typed-derive implicit -I../hello
31-
Implicit(ImplicitParsers { optimization: None, include: Some("../hello"), bind: None, sleep: None })
48+
Implicit(ImplicitParsers { optimization: None, include: Some("../hello"), bind: None, sleep: None, bump_level: None })
3249

3350
```
3451

3552
IP Address
3653
```console
3754
$ typed-derive implicit --bind 192.0.0.1
38-
Implicit(ImplicitParsers { optimization: None, include: None, bind: Some(192.0.0.1), sleep: None })
55+
Implicit(ImplicitParsers { optimization: None, include: None, bind: Some(192.0.0.1), sleep: None, bump_level: None })
3956

4057
$ typed-derive implicit --bind localhost
4158
? failed
@@ -48,7 +65,7 @@ For more information, try '--help'.
4865
Time
4966
```console
5067
$ typed-derive implicit --sleep 10s
51-
Implicit(ImplicitParsers { optimization: None, include: None, bind: None, sleep: Some(10s) })
68+
Implicit(ImplicitParsers { optimization: None, include: None, bind: None, sleep: Some(10s), bump_level: None })
5269

5370
$ typed-derive implicit --sleep forever
5471
? failed
@@ -57,3 +74,24 @@ error: invalid value 'forever' for '--sleep <SLEEP>': failed to parse "forever"
5774
For more information, try '--help'.
5875

5976
```
77+
78+
Version field
79+
```console
80+
$ typed-derive implicit --bump-level minor
81+
Implicit(ImplicitParsers { optimization: None, include: None, bind: None, sleep: None, bump_level: Some(Minor) })
82+
83+
$ typed-derive implicit --bump-level 10.0.0
84+
? failed
85+
error: invalid value '10.0.0' for '--bump-level <BUMP_LEVEL>'
86+
[possible values: major, minor, patch]
87+
88+
For more information, try '--help'.
89+
90+
$ typed-derive implicit --bump-level blue
91+
? failed
92+
error: invalid value 'blue' for '--bump-level <BUMP_LEVEL>'
93+
[possible values: major, minor, patch]
94+
95+
For more information, try '--help'.
96+
97+
```

examples/typed-derive/implicit.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clap::Args;
2+
use clap::ValueEnum;
23

34
#[derive(Args, Debug)]
45
pub(crate) struct ImplicitParsers {
@@ -17,4 +18,45 @@ pub(crate) struct ImplicitParsers {
1718
/// Allow human-readable durations
1819
#[arg(long)]
1920
sleep: Option<jiff::SignedDuration>,
21+
22+
/// Custom enums
23+
#[arg(long)]
24+
bump_level: Option<BumpLevel>,
25+
}
26+
27+
#[derive(Debug, Clone, Copy, ValueEnum)]
28+
#[value(rename_all = "kebab-case")]
29+
pub(crate) enum BumpLevel {
30+
/// Increase the major version (x.0.0)
31+
Major,
32+
/// Increase the minor version (x.y.0)
33+
Minor,
34+
/// Increase the patch version (x.y.z)
35+
Patch,
36+
}
37+
38+
impl std::fmt::Display for BumpLevel {
39+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40+
use clap::ValueEnum;
41+
42+
self.to_possible_value()
43+
.expect("no values are skipped")
44+
.get_name()
45+
.fmt(f)
46+
}
47+
}
48+
49+
impl std::str::FromStr for BumpLevel {
50+
type Err = String;
51+
52+
fn from_str(s: &str) -> Result<Self, Self::Err> {
53+
use clap::ValueEnum;
54+
55+
for variant in Self::value_variants() {
56+
if variant.to_possible_value().unwrap().matches(s, false) {
57+
return Ok(*variant);
58+
}
59+
}
60+
Err(format!("Invalid variant: {s}"))
61+
}
2062
}

0 commit comments

Comments
 (0)