cling 0.1.3

A lightweight framework that simplifies building complex command-line applications with [clap.rs](https://clap.rs)
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! The main entry point for the cling framework.
use std::marker::PhantomData;
use std::process::{ExitCode, Termination};

use clap::Parser;

use super::error::{format_clap_error, CliErrorHandler};
use crate::error::CliError;
use crate::params::CollectedArgs;

mod _private {
    pub struct Build;
    pub struct Ready;
    pub struct Finished;
}

use _private::*;

#[doc(hidden)]
pub trait Run: Send + Sync {
    fn call<'a>(
        &'a self,
        args: &'a mut CollectedArgs,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<(), CliError>> + Send + 'a>,
    >;
}

type ClingReady<T> = Cling<T, Ready>;
/// A completed run of a cling program.
///
/// This is typically used to introspect the result after running the cling
/// application, but since it implements [Termination] trait, it can be used as
/// a return type in `main()` directly.
///
/// ```rust, no_run
/// use cling::prelude::*;
///
/// #[derive(Run, Parser, Debug, Clone)]
/// #[cling(run = "run")]
/// pub struct App {
///     /// Turn debugging information on
///     #[arg(short, long, action = ArgAction::Count)]
///     pub debug: u8,
/// }
///
/// fn run() {
///     println!("Hello Program!");
/// }
///
/// // Note that tokio here is only used as an example, you can use any async runtime.
/// #[tokio::main]
/// async fn main() -> ClingFinished<App> {
///     Cling::parse_and_run().await
/// }
/// ```
pub type ClingFinished<T> = Cling<T, Finished>;

/// A Cling program.
///
/// Example:
/// ```
/// use cling::prelude::*;
///
/// #[derive(Run, Parser, Debug, Clone)]
/// #[cling(run = "run")]
/// pub struct App {
///     /// Turn debugging information on
///     #[arg(short, long, action = ArgAction::Count)]
///     pub debug: u8,
/// }
///
///
/// pub async fn run() {
///     println!("Hello, world!");
/// }
///
/// #[tokio::main]
/// async fn main() -> ClingFinished<App> {
///     Cling::parse_and_run().await
/// }
/// ```
pub struct Cling<T, S = Build> {
    settings: Settings,
    _status: PhantomData<S>,
    inner: ClingInner<T>,
}

/// Holds configuration for cling framework.
#[allow(dead_code)]
#[derive(Default, Clone)]
struct Settings {}

enum ClingInner<T> {
    Ready {
        parsed: T,
        collected_params: CollectedArgs,
    },
    Finished {
        result: Result<(), CliError>,
        collected_params: CollectedArgs,
        _parsed_type: PhantomData<T>,
    },
}

impl<T: Run + Parser> Cling<T, Finished> {
    /// Instantiate a successfully finished Cling application. This is useful
    /// when you want to return a successful Cling instance from `main()`
    /// directly.
    pub fn success() -> ClingFinished<T> {
        ClingFinished {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Finished {
                result: Ok(()),
                collected_params: CollectedArgs::new(),
                _parsed_type: PhantomData,
            },
        }
    }

    /// Instantiate a failed finished Cling application. This is useful
    /// when you want to wrap an Error into a Cling instance to be returned from
    /// `main()`.
    pub fn failed(e: impl Into<CliError>) -> ClingFinished<T> {
        ClingFinished {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Finished {
                result: Err(e.into()),
                collected_params: CollectedArgs::new(),
                _parsed_type: PhantomData,
            },
        }
    }
}

/// Parses T with clap and runs until completion
impl<T: Run + Parser> Cling<T, Build> {
    /// Create a Cling application from a parsed clap struct.
    pub fn new(parsed: T) -> ClingReady<T> {
        ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed,
                collected_params: CollectedArgs::new(),
            },
        }
    }

    /// [Provisional]
    #[allow(dead_code)]
    fn with_settings(parsed: T, settings: Settings) -> ClingReady<T> {
        ClingReady {
            settings,
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed,
                collected_params: CollectedArgs::new(),
            },
        }
    }

    /// Parse command line arguments, run the program, and return the finished
    /// Cling application. [[`ClingFinished<T>`]] can be returned from `main()`
    /// directly which will handle printing errors and exiting with the
    /// correct exit code.
    pub async fn parse_and_run() -> ClingFinished<T> {
        let parsed =
            <T as clap::Parser>::try_parse().map_err(format_clap_error::<T>);
        match parsed {
            | Ok(parsed) => Cling::new(parsed).run().await,
            | Err(e) => {
                ClingFinished {
                    settings: Settings::default(),
                    _status: PhantomData,
                    inner: ClingInner::Finished {
                        result: Err(e.into()),
                        collected_params: CollectedArgs::new(),
                        _parsed_type: PhantomData,
                    },
                }
            }
        }
    }

    /// Parse command line arguments and exit if parsing failed.
    pub fn parse() -> ClingReady<T> {
        ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed: <T as clap::Parser>::parse(),
                collected_params: CollectedArgs::new(),
            },
        }
    }

    /// Attempt to parse command line arguments and return a runnable Cling
    /// application.
    pub fn try_parse() -> Result<ClingReady<T>, CliError> {
        Ok(ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed: <T as clap::Parser>::try_parse()
                    .map_err(format_clap_error::<T>)?,
                collected_params: CollectedArgs::new(),
            },
        })
    }

    pub fn try_parse_from<I, B>(itr: I) -> Result<ClingReady<T>, CliError>
    where
        I: IntoIterator<Item = B>,
        B: Into<std::ffi::OsString> + Clone,
    {
        Ok(ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed: <T as clap::Parser>::try_parse_from(itr)
                    .map_err(format_clap_error::<T>)?,
                collected_params: CollectedArgs::new(),
            },
        })
    }

    /// Parses input as a UNIX shell command.
    ///
    /// Example input string: `sub-command --debug=2`. Note that the input
    /// **must omit** the CLI binary name, otherwise clap parsing will
    /// fail.
    #[cfg(feature = "shlex")]
    pub fn try_parse_str(input: &str) -> Result<ClingReady<T>, CliError> {
        // binary name
        let bin_name = std::env::current_exe()
            .ok()
            .and_then(|pb| pb.file_name().map(|s| s.to_os_string()))
            .and_then(|s| s.into_string().ok())
            .unwrap();
        Self::try_parse_str_with_bin_name(&bin_name, input)
    }

    #[cfg(feature = "shlex")]
    pub fn try_parse_str_with_bin_name(
        bin_name: &str,
        input: &str,
    ) -> Result<ClingReady<T>, CliError> {
        let input = format!("{bin_name} {input}");
        let args = shlex::split(&input).ok_or(CliError::InputString)?;
        let parsed = <T as clap::Parser>::try_parse_from(args)
            .map_err(format_clap_error::<T>)?;
        Ok(ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed,
                collected_params: CollectedArgs::new(),
            },
        })
    }

    /// Parse command line arguments and aborts the program if parsing failed.
    pub fn parse_or_exit() -> ClingReady<T> {
        ClingReady {
            settings: Settings::default(),
            _status: PhantomData,
            inner: ClingInner::Ready {
                parsed: <T as clap::Parser>::try_parse()
                    .map_err(format_clap_error::<T>)
                    .unwrap_or_exit(),
                collected_params: CollectedArgs::new(),
            },
        }
    }

    /// Parses command line arguments, runs the program and exits afterwards.
    pub async fn default_run_and_exit() -> ! {
        Self::parse_or_exit().run_and_exit().await
    }
}

/// Cling is now ready to run.
impl<T: Run + Parser> Cling<T, Ready> {
    pub async fn run_and_exit(self) -> ! {
        let res = self.run().await;
        res.result().then_exit()
    }

    pub async fn run_with_state_and_exit<S>(self, state: S) -> !
    where
        S: Clone + Send + Sync + 'static,
    {
        self.run_with_state(state).await.result().then_exit()
    }

    /// Runs the app with a given state.
    pub async fn run(self) -> ClingFinished<T> {
        let ClingInner::Ready {
            parsed,
            mut collected_params,
        } = self.inner
        else {
            // This will never happen. run() is only implemented on
            // Cling::Ready.
            unreachable!()
        };

        let result = <T as Run>::call(&parsed, &mut collected_params).await;
        // We ensure that transitioning to ClingFinished only happens when we
        // have a result. Therefore, it's safe to unwrap() the result in
        // ClingFinished.
        ClingFinished {
            settings: self.settings,
            _status: PhantomData,
            inner: ClingInner::Finished {
                collected_params,
                result,
                _parsed_type: PhantomData,
            },
        }
    }

    pub async fn run_with_state<S>(mut self, state: S) -> ClingFinished<T>
    where
        S: Clone + Send + Sync + 'static,
    {
        let ClingInner::Ready {
            ref mut collected_params,
            ..
        } = self.inner
        else {
            // This will never happen. run_with_state() is only implemented on
            // Cling::Ready.
            unreachable!()
        };
        // Put the state the state
        collected_params.insert(
            crate::extractors::State(state),
            /* override_is_expected = */ true,
        );
        Self::run(self).await
    }
}

/// Cling program has terminated and results can be introspected.
impl<T: Run + Parser> Cling<T, Finished> {
    pub fn result_ref(&self) -> &Result<(), CliError> {
        let ClingInner::Finished { ref result, .. } = self.inner else {
            unreachable!()
        };
        result
    }

    pub fn result(self) -> Result<(), CliError> {
        let ClingInner::Finished { result, .. } = self.inner else {
            unreachable!()
        };
        result
    }

    pub fn is_success(&self) -> bool {
        self.result_ref().is_ok()
    }

    pub fn is_failure(&self) -> bool {
        self.result_ref().is_err()
    }

    pub fn collected_parameters(&self) -> &CollectedArgs {
        let ClingInner::Finished {
            ref collected_params,
            ..
        } = self.inner
        else {
            unreachable!()
        };
        collected_params
    }

    pub fn collected_arguments_mut(&mut self) -> &mut CollectedArgs {
        let ClingInner::Finished {
            ref mut collected_params,
            ..
        } = self.inner
        else {
            unreachable!()
        };
        collected_params
    }
}

/// Enables clap structs to be executed with cling.
///
/// This extension trait allows clap users to parse their clap structs as usual,
/// then run them with cling without constructing a [Cling] instance
/// explicitly.
///
/// Example:
/// ```
/// use cling::prelude::*;
///
/// #[derive(Run, Parser, Debug, Clone)]
/// #[cling(run = "run")]
/// pub struct App {
///     /// Turn debugging information on
///     #[arg(short, long, action = ArgAction::Count)]
///     pub debug: u8,
/// }
///
///
/// pub async fn run() {
///     println!("Hello, world!");
/// }
///
/// #[tokio::main]
/// async fn main() {
///     let app = App::parse();
///     let app = app.into_cling();
///     app.run_and_exit().await;
/// }
/// ```
pub trait ClapClingExt: Sized {
    fn into_cling(self) -> ClingReady<Self>;
}

impl<T> ClapClingExt for T
where
    T: Run + Parser + Sync + Send + 'static,
{
    fn into_cling(self) -> ClingReady<Self> {
        Cling::<T>::new(self)
    }
}

/// Allows main() to return ClingFinished and it'll report the error correctly
/// if any.
impl<T: Run + Parser> Termination for ClingFinished<T> {
    fn report(self) -> ExitCode {
        if let Err(e) = self.result() {
            // Silently ignore IO errors.
            let _ = e.print();
            return ExitCode::from(e.exit_code());
        }
        ExitCode::SUCCESS
    }
}

/// Convert a [CliError] into a [`ClingFinished`].
impl<T: Run + Parser> From<CliError> for ClingFinished<T> {
    fn from(value: CliError) -> Self {
        Cling::failed(value)
    }
}