-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomplex.rs
More file actions
164 lines (144 loc) · 4.08 KB
/
Copy pathcomplex.rs
File metadata and controls
164 lines (144 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use cling::prelude::*;
use colored::Colorize;
use static_assertions::assert_impl_all;
#[derive(Run, Collect, Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
#[cling(run = "init")]
pub struct CliArgs {
/// What?
#[arg(short)]
use_me: bool,
#[clap(flatten)]
// `collected` attribute allows collecting types that do not implement the
// [Collect] trait. This is useful when you need to collect external types.
// However, those types must implement [Clone] and cling will log a
// warning if the same type is collected multiple times in the execution
// path of the command (if logging is enabled).
#[cling(collect)]
verbosity: clap_verbosity_flag::Verbosity,
// colors will be collected because [Colors] implement [Collect]
#[arg(short)]
colors: Option<Vec<Colors>>,
#[command(flatten)]
pub common: CommonArgs,
#[command(subcommand)]
pub command: Commands,
}
#[derive(ValueEnum, Collect, Debug, Clone)]
pub enum Colors {
Red,
Green,
Blue,
}
#[derive(Run, Subcommand, Debug, Clone)]
pub enum Commands {
/// Calculate things
Calculator(Calculator),
/// What's my name?
#[cling(run = "groot")]
#[command(name = "whoami")]
WhoAmI,
}
#[derive(Collect, Args, Debug, Clone)]
pub struct CommonArgs {
/// Turn debugging information on
#[arg(short, long, action = clap::ArgAction::Count)]
pub debug: u8,
}
#[derive(Run, Collect, Parser, Debug, Clone)]
#[cling(run = "run_calc")]
pub struct Calculator {
/// Enable color output
#[arg(short, long, global = true)]
pub color: bool,
#[command(subcommand)]
pub operation: CalcOperations,
}
#[derive(Run, Subcommand, Debug, Clone)]
pub enum CalcOperations {
/// Add two numbers
Add(AddArgs),
/// Subtract two numbers
Subtract(SubtractArgs),
}
#[derive(Run, Collect, Parser, Debug, Clone)]
#[cling(run = "run_add")]
pub struct AddArgs {
pub num1: u64,
pub num2: u64,
}
#[derive(Run, Collect, Parser, Debug, Clone)]
#[cling(run = "run_subtract")]
pub struct SubtractArgs {
pub num1: u64,
pub num2: u64,
}
#[derive(Clone, Debug)]
struct Database {
_data: String,
}
async fn run_calc(
State(database): State<Database>,
calc: &Calculator,
) -> State<Database> {
println!(
"Database is currently {:?} but we will override that!",
database
);
let database = Database {
_data: "Loads of calculator data".to_owned(),
};
println!(">> Calculator: {:?}", calc);
State(database)
}
async fn init(
Collected(verbosity): Collected<clap_verbosity_flag::Verbosity>,
// Can also be extracted by reference.
// Collected(verbosity): Collected<&clap_verbosity_flag::Verbosity>,
common: &CommonArgs,
colors: &Option<Vec<Colors>>,
) -> State<Database> {
println!(
">> Hello world! {:?}, color: {:?}, verbosity: {:?}",
common,
colors,
verbosity.log_level().unwrap()
);
let database = Database {
_data: "Loads of data".to_owned(),
};
State(database)
}
// Note that handlers can be sync as well.
fn groot() {
println!("I'm groot!");
}
// my add handler
async fn run_add(
// database state was created in init() by returning State() as an effect.
State(database): State<Database>,
calc: &Calculator,
add: &AddArgs,
) -> Result<(), CliError> {
println!("Database: {:?}", database);
let output =
format!("{} + {} = {}", add.num1, add.num2, add.num1 + add.num2);
if calc.color {
println!("{}", output.green());
} else {
println!("{output}");
}
Ok(())
}
// Note that this is SYNC handler.
// Fails in runtime, we expect AddArgs but this will never be collected because
// `AddArgs` is never visited while traversing the type tree during execution.
pub fn run_subtract(_calc: &Calculator, _add: &AddArgs) {
println!("Never gets called!");
}
assert_impl_all!(CliArgs: ClapClingExt, cling::prelude::Parser, Send, Sync, Run);
#[tokio::main]
async fn main() -> ClingFinished<CliArgs> {
env_logger::builder().init();
Cling::parse().run().await
}