1use super::*;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct SetterArg {
10 pub name: String,
11 pub aliases: Vec<String>,
12 pub delimiter: String,
13 pub optional: bool
14}
15
16impl SetterArg {
17 pub fn new(name: &str, aliases: Vec<&str>, delimiter: &str, optional: bool) -> Box<Self> {
18 let mut aliases_new = Vec::with_capacity(aliases.len());
19
20 for alias in aliases {
21 aliases_new.push(String::from(alias));
22 }
23
24 Box::new(Self {
25 name: String::from(name),
26 aliases: aliases_new,
27 delimiter: String::from(delimiter),
28 optional
29 })
30 }
31
32 pub fn with_name(name: &str) -> Box<Self> {
33 Box::new(Self {
34 name: String::from(name),
35 aliases: Vec::new(),
36 delimiter: String::from("="),
37 optional: true
38 })
39 }
40
41 pub fn with_delimiter(name: &str, delimiter: &str) -> Box<Self> {
42 Box::new(Self {
43 name: String::from(name),
44 aliases: Vec::new(),
45 delimiter: String::from(delimiter),
46 optional: true
47 })
48 }
49}
50
51impl Argument for SetterArg {
52 fn get_name(&self) -> &String {
53 &self.name
54 }
55
56 fn get_aliases(&self) -> &Vec<String> {
57 &self.aliases
58 }
59
60 fn add_alias(&mut self, alias: String) {
61 if let Err(_) = self.aliases.binary_search(&alias) {
62 self.aliases.push(alias);
63 }
64 }
65
66 fn is_optional(&self) -> bool {
67 self.optional
68 }
69
70 fn try_parse(&self, args: &Vec<String>) -> Option<(Vec<String>, ArgumentValue)> {
71 if args.len() == 0 {
72 return None
73 }
74
75 let mut new_args = Vec::new();
76
77 let mut names = Vec::with_capacity(self.aliases.len() + 1);
78
79 names.push(&self.name);
80
81 for alias in &self.aliases {
82 names.push(alias);
83 }
84
85 for i in 0..args.len() {
86 for name in &names {
87 if args[i].len() < name.len() + self.delimiter.len() {
88 continue;
89 }
90
91 if &args[i][..name.len()] == name.as_str() && &args[i][name.len()..name.len() + self.delimiter.len()] == self.delimiter {
92 for j in i+1..args.len() {
93 new_args.push(args[j].clone());
94 }
95
96 return Some((new_args, ArgumentValue {
97 name: self.name.clone(),
98 value: args[i][name.len() + 1..].to_string()
99 }))
100 }
101 }
102
103 new_args.push(args[i].clone());
104 }
105
106 None
107 }
108}