Skip to main content

rawcmd/
param_summary.rs

1use std::str::FromStr;
2
3/// Structure which holds param summary.
4#[derive(Debug, Clone, PartialEq)]
5pub struct ParamSummary {
6    name: String,
7    description: Option<String>,
8    value: Option<String>,
9    default_value: Option<String>,
10    provided: bool,
11}
12
13/// Structure implementation.
14impl ParamSummary {
15
16    // Returns new instance.
17    pub fn with_name<
18        S: Into<String>,
19    >(
20        name: S,
21        description: Option<String>,
22        value: Option<String>,
23        default_value: Option<String>,
24        provided: bool,
25    ) -> Self {
26        Self {
27            name: name.into(),
28            description,
29            value,
30            default_value,
31            provided,
32        }
33    }
34}
35
36/// Structure implementation.
37impl ParamSummary {
38
39    /// Returns name.
40    pub fn name(&self) -> &String {
41        &self.name
42    }
43
44    /// Returns description.
45    pub fn description(&self) -> &Option<String> {
46        &self.description
47    }
48
49    /// Returns value.
50    pub fn value(&self) -> &Option<String> {
51        &self.value
52    }
53
54    /// Returns value.
55    pub fn to_value<T>(&self) -> Option<T>
56        where
57        T: FromStr,
58    {
59        match &self.value {
60            Some(v) => match v.parse::<T>() {
61                Ok(v) => Some(v),
62                Err(_) => None,
63            },
64            None => None,
65        }
66    }
67
68    /// Returns default value.
69    pub fn default_value(&self) -> &Option<String> {
70        &self.default_value
71    }
72
73    /// Returns true if the param has value.
74    pub fn provided(&self) -> bool {
75        self.provided
76    }
77}