clap/args/arg_builder/
valued.rs
1use std::{
2 ffi::{OsStr, OsString},
3 rc::Rc,
4};
5
6use crate::{map::VecMap, Arg};
7
8#[allow(missing_debug_implementations)]
9#[derive(Clone)]
10pub struct Valued<'a, 'b>
11where
12 'a: 'b,
13{
14 pub possible_vals: Option<Vec<&'b str>>,
15 pub val_names: Option<VecMap<&'b str>>,
16 pub num_vals: Option<u64>,
17 pub max_vals: Option<u64>,
18 pub min_vals: Option<u64>,
19 #[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
20 pub validator: Option<Rc<Fn(String) -> Result<(), String>>>,
21 #[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
22 pub validator_os: Option<Rc<Fn(&OsStr) -> Result<(), OsString>>>,
23 pub val_delim: Option<char>,
24 pub default_val: Option<&'b OsStr>,
25 #[cfg_attr(feature = "cargo-clippy", allow(clippy::type_complexity))]
26 pub default_vals_ifs: Option<VecMap<(&'a str, Option<&'b OsStr>, &'b OsStr)>>,
27 pub env: Option<(&'a OsStr, Option<OsString>)>,
28 pub terminator: Option<&'b str>,
29}
30
31impl<'n, 'e> Default for Valued<'n, 'e> {
32 fn default() -> Self {
33 Valued {
34 possible_vals: None,
35 num_vals: None,
36 min_vals: None,
37 max_vals: None,
38 val_names: None,
39 validator: None,
40 validator_os: None,
41 val_delim: None,
42 default_val: None,
43 default_vals_ifs: None,
44 env: None,
45 terminator: None,
46 }
47 }
48}
49
50impl<'n, 'e> Valued<'n, 'e> {
51 pub fn fill_in(&mut self) {
52 if let Some(ref vec) = self.val_names {
53 if vec.len() > 1 {
54 self.num_vals = Some(vec.len() as u64);
55 }
56 }
57 }
58}
59
60impl<'n, 'e, 'z> From<&'z Arg<'n, 'e>> for Valued<'n, 'e> {
61 fn from(a: &'z Arg<'n, 'e>) -> Self {
62 let mut v = a.v.clone();
63 if let Some(ref vec) = a.v.val_names {
64 if vec.len() > 1 {
65 v.num_vals = Some(vec.len() as u64);
66 }
67 }
68 v
69 }
70}