1use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct ModuleActions {
8 pub module: String,
9 pub actions: Vec<ActionSpec>,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct ActionSpec {
14 pub name: String,
15 pub help: String,
16 #[serde(default)]
17 pub args: Vec<ArgSpec>,
18}
19
20impl ActionSpec {
21 pub fn new(name: impl Into<String>, help: impl Into<String>) -> Self {
22 Self {
23 name: name.into(),
24 help: help.into(),
25 args: Vec::new(),
26 }
27 }
28
29 pub fn arg(mut self, arg: ArgSpec) -> Self {
30 self.args.push(arg);
31 self
32 }
33
34 pub fn usage(&self) -> String {
36 let mut line = self.name.clone();
37 for arg in &self.args {
38 let dots = if arg.rest { "..." } else { "" };
39 let word = if arg.optional {
40 format!(" [{}{dots}]", arg.name)
41 } else {
42 format!(" <{}{dots}>", arg.name)
43 };
44 line.push_str(&word);
45 }
46 line
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
51pub struct ArgSpec {
52 pub name: String,
53 pub help: String,
54 pub kind: ArgKind,
55 #[serde(default)]
57 pub optional: bool,
58 #[serde(default)]
61 pub rest: bool,
62 #[serde(default, skip_serializing_if = "Option::is_none")]
66 pub source: Option<String>,
67}
68
69impl ArgSpec {
70 fn new(name: impl Into<String>, help: impl Into<String>, kind: ArgKind) -> Self {
71 Self {
72 name: name.into(),
73 help: help.into(),
74 kind,
75 optional: false,
76 rest: false,
77 source: None,
78 }
79 }
80
81 pub fn string(name: impl Into<String>, help: impl Into<String>) -> Self {
82 Self::new(name, help, ArgKind::String)
83 }
84
85 pub fn int(name: impl Into<String>, help: impl Into<String>) -> Self {
86 Self::new(name, help, ArgKind::Int)
87 }
88
89 pub fn float(name: impl Into<String>, help: impl Into<String>) -> Self {
90 Self::new(name, help, ArgKind::Float)
91 }
92
93 pub fn bool(name: impl Into<String>, help: impl Into<String>) -> Self {
94 Self::new(name, help, ArgKind::Bool)
95 }
96
97 pub fn choice<I, S>(name: impl Into<String>, help: impl Into<String>, values: I) -> Self
98 where
99 I: IntoIterator<Item = S>,
100 S: Into<String>,
101 {
102 let values = values.into_iter().map(Into::into).collect();
103 Self::new(name, help, ArgKind::Choice { values })
104 }
105
106 pub fn optional(mut self) -> Self {
107 self.optional = true;
108 self
109 }
110
111 pub fn rest(mut self) -> Self {
112 self.rest = true;
113 self
114 }
115
116 pub fn source(mut self, source: impl Into<String>) -> Self {
118 self.source = Some(source.into());
119 self
120 }
121}
122
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124#[serde(tag = "type", rename_all = "snake_case")]
125pub enum ArgKind {
126 String,
127 Int,
129 Float,
130 Bool,
132 Choice {
134 values: Vec<String>,
135 },
136}
137
138#[cfg(test)]
139mod tests {
140 use serde_json::json;
141
142 use super::*;
143
144 #[test]
145 fn usage_marks_optional_and_rest_arguments() {
146 let spec = ActionSpec::new("show", "Show a view")
147 .arg(ArgSpec::string("view", "Which view"))
148 .arg(ArgSpec::string("text", "Body").optional().rest());
149 assert_eq!(spec.usage(), "show <view> [text...]");
150 }
151
152 #[test]
153 fn builder_produces_the_listed_shape() {
154 let spec = ActionSpec::new("dnd", "Do not disturb")
155 .arg(ArgSpec::choice("state", "New state", ["on", "off", "toggle"]).optional());
156
157 assert_eq!(
158 serde_json::to_value(&spec).unwrap(),
159 json!({
160 "name": "dnd",
161 "help": "Do not disturb",
162 "args": [{
163 "name": "state",
164 "help": "New state",
165 "kind": { "type": "choice", "values": ["on", "off", "toggle"] },
166 "optional": true,
167 "rest": false
168 }]
169 })
170 );
171 }
172}