1use crate::schema::Select;
4use crate::util::ListLine;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Selected {
9 pub planned: bool,
11 pub args: Vec<String>,
13}
14
15fn tokens(sel: &Select, args: &[String]) -> (bool, Vec<String>) {
17 let mut found = false;
18 let mut out = Vec::new();
19 let eq = format!("{}=", sel.option);
20 let mut i = 0;
21 while i < args.len() {
22 let a = &args[i];
23 let value = if a == &sel.option {
24 found = true;
25 i += 1;
26 args.get(i).map(String::as_str)
27 } else if let Some(v) = a.strip_prefix(&eq) {
28 found = true;
29 Some(v)
30 } else {
31 None
32 };
33 if let Some(v) = value {
34 out.extend(
35 v.split(sel.separator.as_str())
36 .map(str::trim)
37 .filter(|t| !t.is_empty())
38 .map(str::to_string),
39 );
40 }
41 i += 1;
42 }
43 (found, out)
44}
45
46fn stripped(sel: &Select, args: &[String]) -> Vec<String> {
47 let eq = format!("{}=", sel.option);
48 let mut out = Vec::new();
49 let mut i = 0;
50 while i < args.len() {
51 if args[i] == sel.option {
52 i += 2;
53 continue;
54 }
55 if !args[i].starts_with(&eq) {
56 out.push(args[i].clone());
57 }
58 i += 1;
59 }
60 out
61}
62
63pub fn select_line(sel: &Select, line: &ListLine) -> Selected {
65 let (found, toks) = tokens(sel, &line.args);
66 let planned = if found {
67 toks.iter().any(|t| t == &sel.value)
68 } else {
69 sel.when_absent != "none"
70 };
71 let args = if sel.strip {
72 stripped(sel, &line.args)
73 } else {
74 line.args.clone()
75 };
76 Selected { planned, args }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82
83 fn sel(strip: bool, when_absent: &str, separator: &str) -> Select {
84 Select {
85 option: "--only-targets".into(),
86 value: "a".into(),
87 separator: separator.into(),
88 when_absent: when_absent.into(),
89 strip,
90 }
91 }
92
93 fn line(args: &[&str]) -> ListLine {
94 ListLine {
95 raw: String::new(),
96 easyconfig: "Foo-1.0.eb".into(),
97 args: args.iter().map(|s| s.to_string()).collect(),
98 }
99 }
100
101 #[test]
102 fn absent_option_follows_when_absent() {
103 assert!(select_line(&sel(false, "all", ","), &line(&["--rebuild"])).planned);
104 assert!(!select_line(&sel(false, "none", ","), &line(&["--rebuild"])).planned);
105 }
106
107 #[test]
108 fn matching_and_non_matching_tokens() {
109 let s = sel(false, "all", ",");
110 assert!(select_line(&s, &line(&["--only-targets=a"])).planned);
111 assert!(!select_line(&s, &line(&["--only-targets=b"])).planned);
112 assert!(!select_line(&s, &line(&["--only-targets="])).planned);
113 assert!(select_line(&s, &line(&["--only-targets-x=b"])).planned);
115 }
116
117 #[test]
118 fn multiple_tokens_and_separator() {
119 let s = sel(false, "none", ",");
120 assert!(select_line(&s, &line(&["--only-targets=b,c,a"])).planned);
121 assert!(!select_line(&s, &line(&["--only-targets=b,c"])).planned);
122 let s = sel(false, "none", "+");
123 assert!(select_line(&s, &line(&["--only-targets=b+a"])).planned);
124 assert!(!select_line(&s, &line(&["--only-targets=b,a"])).planned);
125 }
126
127 #[test]
128 fn separate_value_token_form() {
129 let s = sel(true, "none", ",");
130 let r = select_line(&s, &line(&["--rebuild", "--only-targets", "b,a", "-f"]));
131 assert!(r.planned);
132 assert_eq!(r.args, ["--rebuild", "-f"]);
133 }
134
135 #[test]
136 fn strip_removes_the_option_only_when_set() {
137 let l = line(&["--rebuild", "--only-targets=a", "--x"]);
138 assert_eq!(
139 select_line(&sel(true, "all", ","), &l).args,
140 ["--rebuild", "--x"]
141 );
142 assert_eq!(select_line(&sel(false, "all", ","), &l).args, l.args);
143 }
144}