| 1 | // Std |
| 2 | use std::{ |
| 3 | ffi::{OsStr, OsString}, |
| 4 | iter::{Cloned, Flatten}, |
| 5 | slice::Iter, |
| 6 | }; |
| 7 | |
| 8 | use crate::builder::ArgPredicate; |
| 9 | use crate::parser::ValueSource; |
| 10 | use crate::util::eq_ignore_case; |
| 11 | use crate::util::AnyValue; |
| 12 | use crate::util::AnyValueId; |
| 13 | use crate::INTERNAL_ERROR_MSG; |
| 14 | |
| 15 | #[derive (Debug, Clone)] |
| 16 | pub(crate) struct MatchedArg { |
| 17 | source: Option<ValueSource>, |
| 18 | indices: Vec<usize>, |
| 19 | type_id: Option<AnyValueId>, |
| 20 | vals: Vec<Vec<AnyValue>>, |
| 21 | raw_vals: Vec<Vec<OsString>>, |
| 22 | ignore_case: bool, |
| 23 | } |
| 24 | |
| 25 | impl MatchedArg { |
| 26 | pub(crate) fn new_arg(arg: &crate::Arg) -> Self { |
| 27 | let ignore_case = arg.is_ignore_case_set(); |
| 28 | Self { |
| 29 | source: None, |
| 30 | indices: Vec::new(), |
| 31 | type_id: Some(arg.get_value_parser().type_id()), |
| 32 | vals: Vec::new(), |
| 33 | raw_vals: Vec::new(), |
| 34 | ignore_case, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub(crate) fn new_group() -> Self { |
| 39 | let ignore_case = false; |
| 40 | Self { |
| 41 | source: None, |
| 42 | indices: Vec::new(), |
| 43 | type_id: None, |
| 44 | vals: Vec::new(), |
| 45 | raw_vals: Vec::new(), |
| 46 | ignore_case, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub(crate) fn new_external(cmd: &crate::Command) -> Self { |
| 51 | let ignore_case = false; |
| 52 | Self { |
| 53 | source: None, |
| 54 | indices: Vec::new(), |
| 55 | type_id: Some( |
| 56 | cmd.get_external_subcommand_value_parser() |
| 57 | .expect(INTERNAL_ERROR_MSG) |
| 58 | .type_id(), |
| 59 | ), |
| 60 | vals: Vec::new(), |
| 61 | raw_vals: Vec::new(), |
| 62 | ignore_case, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | pub(crate) fn indices(&self) -> Cloned<Iter<'_, usize>> { |
| 67 | self.indices.iter().cloned() |
| 68 | } |
| 69 | |
| 70 | pub(crate) fn get_index(&self, index: usize) -> Option<usize> { |
| 71 | self.indices.get(index).cloned() |
| 72 | } |
| 73 | |
| 74 | pub(crate) fn push_index(&mut self, index: usize) { |
| 75 | self.indices.push(index); |
| 76 | } |
| 77 | |
| 78 | pub(crate) fn vals(&self) -> Iter<'_, Vec<AnyValue>> { |
| 79 | self.vals.iter() |
| 80 | } |
| 81 | |
| 82 | pub(crate) fn into_vals(self) -> Vec<Vec<AnyValue>> { |
| 83 | self.vals |
| 84 | } |
| 85 | |
| 86 | pub(crate) fn vals_flatten(&self) -> Flatten<Iter<'_, Vec<AnyValue>>> { |
| 87 | self.vals.iter().flatten() |
| 88 | } |
| 89 | |
| 90 | pub(crate) fn into_vals_flatten(self) -> Flatten<std::vec::IntoIter<Vec<AnyValue>>> { |
| 91 | self.vals.into_iter().flatten() |
| 92 | } |
| 93 | |
| 94 | pub(crate) fn raw_vals(&self) -> Iter<'_, Vec<OsString>> { |
| 95 | self.raw_vals.iter() |
| 96 | } |
| 97 | |
| 98 | pub(crate) fn raw_vals_flatten(&self) -> Flatten<Iter<'_, Vec<OsString>>> { |
| 99 | self.raw_vals.iter().flatten() |
| 100 | } |
| 101 | |
| 102 | pub(crate) fn first(&self) -> Option<&AnyValue> { |
| 103 | self.vals_flatten().next() |
| 104 | } |
| 105 | |
| 106 | #[cfg (test)] |
| 107 | pub(crate) fn first_raw(&self) -> Option<&OsString> { |
| 108 | self.raw_vals_flatten().next() |
| 109 | } |
| 110 | |
| 111 | pub(crate) fn new_val_group(&mut self) { |
| 112 | self.vals.push(vec![]); |
| 113 | self.raw_vals.push(vec![]); |
| 114 | } |
| 115 | |
| 116 | pub(crate) fn append_val(&mut self, val: AnyValue, raw_val: OsString) { |
| 117 | // We assume there is always a group created before. |
| 118 | self.vals.last_mut().expect(INTERNAL_ERROR_MSG).push(val); |
| 119 | self.raw_vals |
| 120 | .last_mut() |
| 121 | .expect(INTERNAL_ERROR_MSG) |
| 122 | .push(raw_val); |
| 123 | } |
| 124 | |
| 125 | pub(crate) fn num_vals(&self) -> usize { |
| 126 | self.vals.iter().map(|v| v.len()).sum() |
| 127 | } |
| 128 | |
| 129 | // Will be used later |
| 130 | #[allow (dead_code)] |
| 131 | pub(crate) fn num_vals_last_group(&self) -> usize { |
| 132 | self.vals.last().map(|x| x.len()).unwrap_or(0) |
| 133 | } |
| 134 | |
| 135 | pub(crate) fn check_explicit(&self, predicate: &ArgPredicate) -> bool { |
| 136 | if self.source.map(|s| !s.is_explicit()).unwrap_or(false) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | match predicate { |
| 141 | ArgPredicate::Equals(val) => self.raw_vals_flatten().any(|v| { |
| 142 | if self.ignore_case { |
| 143 | // If `v` isn't utf8, it can't match `val`, so `OsStr::to_str` should be fine |
| 144 | eq_ignore_case(&v.to_string_lossy(), &val.to_string_lossy()) |
| 145 | } else { |
| 146 | OsString::as_os_str(v) == OsStr::new(val) |
| 147 | } |
| 148 | }), |
| 149 | ArgPredicate::IsPresent => true, |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | pub(crate) fn source(&self) -> Option<ValueSource> { |
| 154 | self.source |
| 155 | } |
| 156 | |
| 157 | pub(crate) fn set_source(&mut self, source: ValueSource) { |
| 158 | if let Some(existing) = self.source { |
| 159 | self.source = Some(existing.max(source)); |
| 160 | } else { |
| 161 | self.source = Some(source); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub(crate) fn type_id(&self) -> Option<AnyValueId> { |
| 166 | self.type_id |
| 167 | } |
| 168 | |
| 169 | pub(crate) fn infer_type_id(&self, expected: AnyValueId) -> AnyValueId { |
| 170 | self.type_id() |
| 171 | .or_else(|| { |
| 172 | self.vals_flatten() |
| 173 | .map(|v| v.type_id()) |
| 174 | .find(|actual| *actual != expected) |
| 175 | }) |
| 176 | .unwrap_or(expected) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl PartialEq for MatchedArg { |
| 181 | fn eq(&self, other: &MatchedArg) -> bool { |
| 182 | let MatchedArg { |
| 183 | source: self_source, |
| 184 | indices: self_indices, |
| 185 | type_id: self_type_id, |
| 186 | vals: _, |
| 187 | raw_vals: self_raw_vals, |
| 188 | ignore_case: self_ignore_case, |
| 189 | } = self; |
| 190 | let MatchedArg { |
| 191 | source: other_source, |
| 192 | indices: other_indices, |
| 193 | type_id: other_type_id, |
| 194 | vals: _, |
| 195 | raw_vals: other_raw_vals, |
| 196 | ignore_case: other_ignore_case, |
| 197 | } = other; |
| 198 | self_source == other_source |
| 199 | && self_indices == other_indices |
| 200 | && self_type_id == other_type_id |
| 201 | && self_raw_vals == other_raw_vals |
| 202 | && self_ignore_case == other_ignore_case |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl Eq for MatchedArg {} |
| 207 | |
| 208 | #[cfg (test)] |
| 209 | mod tests { |
| 210 | use super::*; |
| 211 | |
| 212 | #[test ] |
| 213 | fn test_grouped_vals_first() { |
| 214 | let mut m = MatchedArg::new_group(); |
| 215 | m.new_val_group(); |
| 216 | m.new_val_group(); |
| 217 | m.append_val(AnyValue::new(String::from("bbb" )), "bbb" .into()); |
| 218 | m.append_val(AnyValue::new(String::from("ccc" )), "ccc" .into()); |
| 219 | assert_eq!(m.first_raw(), Some(&OsString::from("bbb" ))); |
| 220 | } |
| 221 | } |
| 222 | |