Skip to main content

gobby_core/
cli_contract.rs

1use serde::Serialize;
2
3#[derive(Debug, Serialize)]
4pub struct CliContract {
5    pub tool: &'static str,
6    pub contract_version: u32,
7    pub summary: &'static str,
8    pub global_flags: Vec<FlagContract>,
9    pub scope: Option<ScopeContract>,
10    pub commands: Vec<CommandContract>,
11    pub error_codes: Vec<&'static str>,
12}
13
14#[derive(Debug, Serialize)]
15pub struct CommandContract {
16    pub name: &'static str,
17    pub summary: &'static str,
18    pub daemon_consumed: bool,
19    pub positionals: Vec<PositionalContract>,
20    pub flags: Vec<FlagContract>,
21    pub json_output_keys: Vec<&'static str>,
22}
23
24#[derive(Debug, Serialize)]
25pub struct FlagContract {
26    pub name: &'static str,
27    pub takes_value: bool,
28    pub value_name: Option<&'static str>,
29    pub allowed_values: Vec<&'static str>,
30    pub required: bool,
31    pub repeatable: bool,
32}
33
34#[derive(Debug, Serialize)]
35pub struct PositionalContract {
36    pub name: &'static str,
37    pub required: bool,
38    pub repeatable: bool,
39}
40
41#[derive(Debug, Serialize)]
42pub struct ScopeContract {
43    pub flags: Vec<FlagContract>,
44    pub default: &'static str,
45    pub identity_keys: Vec<&'static str>,
46}
47
48impl FlagContract {
49    pub fn switch(name: &'static str) -> Self {
50        Self {
51            name,
52            takes_value: false,
53            value_name: None,
54            allowed_values: Vec::new(),
55            required: false,
56            repeatable: false,
57        }
58    }
59
60    pub fn value(name: &'static str, value_name: &'static str) -> Self {
61        Self {
62            name,
63            takes_value: true,
64            value_name: Some(value_name),
65            allowed_values: Vec::new(),
66            required: false,
67            repeatable: false,
68        }
69    }
70
71    pub fn repeatable_value(name: &'static str, value_name: &'static str) -> Self {
72        Self {
73            repeatable: true,
74            ..Self::value(name, value_name)
75        }
76    }
77
78    pub fn required(mut self) -> Self {
79        self.required = true;
80        self
81    }
82
83    pub fn allowed(mut self, values: Vec<&'static str>) -> Self {
84        self.allowed_values = values;
85        self
86    }
87}
88
89impl PositionalContract {
90    pub fn required(name: &'static str) -> Self {
91        Self {
92            name,
93            required: true,
94            repeatable: false,
95        }
96    }
97
98    pub fn repeatable(name: &'static str) -> Self {
99        Self {
100            name,
101            required: true,
102            repeatable: true,
103        }
104    }
105}