1#[derive(Debug, Clone)]
5pub struct CustomCommand {
6 pub name: String,
8
9 pub description: String,
11
12 pub usage: Option<String>,
14
15 pub source_plugin: Option<String>,
17}
18
19impl CustomCommand {
20 pub fn new(name: impl Into<String>) -> Self {
22 CustomCommand {
23 name: name.into(),
24 description: String::new(),
25 usage: None,
26 source_plugin: None,
27 }
28 }
29
30 pub fn with_description(mut self, desc: impl Into<String>) -> Self {
32 self.description = desc.into();
33 self
34 }
35
36 pub fn with_usage(mut self, usage: impl Into<String>) -> Self {
38 self.usage = Some(usage.into());
39 self
40 }
41
42 pub fn with_source(mut self, source: impl Into<String>) -> Self {
44 self.source_plugin = Some(source.into());
45 self
46 }
47
48 pub fn help_text(&self) -> String {
50 let mut text = String::new();
51
52 if !self.description.is_empty() {
53 text.push_str(&self.description);
54 text.push('\n');
55 }
56
57 if let Some(ref usage) = self.usage {
58 text.push('\n');
59 text.push_str("Usage: ");
60 text.push_str(usage);
61 text.push('\n');
62 }
63
64 if let Some(ref source) = self.source_plugin {
65 text.push('\n');
66 text.push_str("(Defined by: ");
67 text.push_str(source);
68 text.push(')');
69 }
70
71 text
72 }
73}
74
75pub struct CommandBuilder {
77 command: CustomCommand,
78}
79
80impl CommandBuilder {
81 pub fn new(name: impl Into<String>) -> Self {
83 CommandBuilder {
84 command: CustomCommand::new(name),
85 }
86 }
87
88 pub fn description(mut self, desc: impl Into<String>) -> Self {
90 self.command.description = desc.into();
91 self
92 }
93
94 pub fn usage(mut self, usage: impl Into<String>) -> Self {
96 self.command.usage = Some(usage.into());
97 self
98 }
99
100 pub fn source(mut self, source: impl Into<String>) -> Self {
102 self.command.source_plugin = Some(source.into());
103 self
104 }
105
106 pub fn build(self) -> CustomCommand {
108 self.command
109 }
110}