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
//! The Interactive-clap library is an add-on for the Command Line Argument
//! Parser (https://crates.io/crates/clap). Interactive-clap allows you to parse
//! command line options. The peculiarity of this macro is that in the absence
//! of command line parameters, the interactive mode of entering these data by
//! the user is activated.

pub use interactive_clap_derive::{InteractiveClap, ToCliArgs};

pub trait ToCli {
    type CliVariant;
}

impl ToCli for String {
    type CliVariant = String;
}

impl ToCli for u128 {
    type CliVariant = u128;
}

impl ToCli for u64 {
    type CliVariant = u64;
}

impl ToCli for bool {
    type CliVariant = bool;
}

pub trait ToInteractiveClapContextScope {
    type InteractiveClapContextScope;
}

pub trait ToCliArgs {
    fn to_cli_args(&self) -> std::collections::VecDeque<String>;
}

pub enum ResultFromCli<T, E> {
    Ok(T),
    Cancel(Option<T>),
    Back,
    Err(Option<T>, E),
}

pub trait FromCli {
    type FromCliContext;
    type FromCliError;
    fn from_cli(
        optional_clap_variant: Option<<Self as ToCli>::CliVariant>,
        context: Self::FromCliContext,
    ) -> ResultFromCli<<Self as ToCli>::CliVariant, Self::FromCliError>
    where
        Self: Sized + ToCli;
}

pub enum SelectVariantOrBack<T: strum::EnumMessage> {
    Variant(T),
    Back,
}
impl<T: strum::EnumMessage> std::fmt::Display for SelectVariantOrBack<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Self::Variant(variant) = self {
            f.write_str(variant.get_message().unwrap())
        } else {
            f.write_str("back")
        }
    }
}