Add or modify a parameter to the command line Returns `Action::Added` if the parameter did not exist before and was added. Returns `Action::Modified` if the parameter existed before, but contained a different value. The value was updated to the newly-requested value. Returns `Action::Existed` if the parameter existed before, and contained the same value as the newly-requested value. No modifi
(&mut self, param: &Parameter)
| 249 | /// contained the same value as the newly-requested value. No |
| 250 | /// modification was made. |
| 251 | pub fn add_or_modify(&mut self, param: &Parameter) -> Action { |
| 252 | let mut new_params = Vec::new(); |
| 253 | let mut modified = false; |
| 254 | let mut seen_key = false; |
| 255 | |
| 256 | for p in self.iter() { |
| 257 | if p.key == param.key { |
| 258 | if !seen_key { |
| 259 | // This is the first time we've seen this key. |
| 260 | // We will replace it with the new parameter. |
| 261 | if p != *param { |
| 262 | modified = true; |
| 263 | } |
| 264 | new_params.push(param.parameter); |
| 265 | } else { |
| 266 | // This is a subsequent parameter with the same key. |
| 267 | // We will remove it, which constitutes a modification. |
| 268 | modified = true; |
| 269 | } |
| 270 | seen_key = true; |
| 271 | } else { |
| 272 | new_params.push(p.parameter); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if !seen_key { |
| 277 | // The parameter was not found, so we append it. |
| 278 | let self_mut = self.0.to_mut(); |
| 279 | if self_mut |
| 280 | .last() |
| 281 | .filter(|v| !v.is_ascii_whitespace()) |
| 282 | .is_some() |
| 283 | { |
| 284 | self_mut.push(b' '); |
| 285 | } |
| 286 | self_mut.extend_from_slice(param.parameter); |
| 287 | return Action::Added; |
| 288 | } |
| 289 | if modified { |
| 290 | self.0 = Cow::Owned(new_params.join(b" ".as_slice())); |
| 291 | Action::Modified |
| 292 | } else { |
| 293 | // The parameter already existed with the same content, and there were no duplicates. |
| 294 | Action::Existed |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// Remove parameter(s) with the given key from the command line |
| 299 | /// |