1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::path::PathBuf;
use std::fmt::Debug;

/// **`ArgsValue`**
#[derive(Debug)]
pub struct ArgsValue<'app> {
    inner: Box<ArgsValueParse<'app> + 'app>,
}

impl<'app> ArgsValue<'app> {
    pub fn new(value: Box<ArgsValueParse<'app> + 'app>) -> Self {
        ArgsValue { inner: value }
    }
}

impl<'app> AsRef<Box<ArgsValueParse<'app> + 'app>> for ArgsValue<'app> {
    fn as_ref(&self) -> &Box<ArgsValueParse<'app> + 'app> {
        &self.inner
    }
}
impl<'app> AsMut<Box<ArgsValueParse<'app> + 'app>> for ArgsValue<'app> {
    fn as_mut(&mut self) -> &mut Box<ArgsValueParse<'app> + 'app> {
        &mut self.inner
    }
}
/// **You can use custom `ArgsValue` by `impl` it**
///
/// ### **Explain**
///
/// * `into(self)` convert it(`&mut T`)  to `ArgsValue`.
///
///
/// * `default(&self)` is `Arguments`'s default value's str for help message print
///
///
/// * `parse(&mut self, args_name: String, msg: &[String])` maintains the value, and return message by `Result<(),String>`.
///
///   `args_name` is current `cmd`'s `args_name`, `msg` is the `&[String]` need to pasre.
///
/// * `check(&self, opt_name: &str)` check value and return message by `Result<(),String>`.
pub trait ArgsValueParse<'app>: Debug {
    fn into(self) -> ArgsValue<'app>;
    fn default(&self) -> Option<String>;
    fn parse(&mut self, args_name: &str, msg: &[String]) -> Result<(), String>;
    fn check(&self, args_name: &str) -> Result<(), String>;
}


impl<'app, 's: 'app> ArgsValueParse<'app> for &'s mut Vec<String> {
    fn into(self) -> ArgsValue<'app> {
        ArgsValue { inner: Box::from(self) }
    }
    fn default(&self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(format!("{:?}", self))
        }
    }
    fn parse(&mut self, _: &str, msg: &[String]) -> Result<(), String> {
        self.clear(); // What due to ?
        msg.iter()
            .filter(|s| !s.is_empty())
            .map(|s| self.push(s.to_string()))
            .count();
        Ok(())
    }
    fn check(&self, args_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("Arguments({}) missing", args_name))
        } else {
            Ok(())
        }
    }
}
impl<'app, 's: 'app> ArgsValueParse<'app> for &'s mut Vec<PathBuf> {
    fn into(self) -> ArgsValue<'app> {
        ArgsValue { inner: Box::from(self) }
    }
    fn default(&self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(format!("{:?}", self))
        }
    }
    fn parse(&mut self, _: &str, msg: &[String]) -> Result<(), String> {
        self.clear(); // What due to ?
        msg.iter()
            .filter(|s| !s.is_empty())
            .map(|s| self.push(PathBuf::from(s)))
            .count();
        Ok(())
    }
    fn check(&self, args_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("Arguments({}) missing", args_name))
        } else {
            Ok(())
        }
    }
}

impl<'app, 's: 'app> ArgsValueParse<'app> for &'s mut Vec<char> {
    fn into(self) -> ArgsValue<'app> {
        ArgsValue { inner: Box::from(self) }
    }
    fn default(&self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(format!("{:?}", self))
        }
    }
    fn parse(&mut self, args_name: &str, msg: &[String]) -> Result<(), String> {
        self.clear();
        for str in msg {
            let chars: Vec<char> = str.chars().collect();
            if chars.len() != 1 {
                return Err(format!("Arguments({}): {:?} is invalid", args_name, str));
            } else {
                self.push(chars[0]);
            }
        }
        Ok(())
    }
    fn check(&self, args_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("Arguments({}) missing", args_name))
        } else {
            Ok(())
        }
    }
}

macro_rules! add_vec_impl {
    ($($t:ty)*) => ($(
        impl<'app, 's: 'app> ArgsValueParse<'app> for &'s mut Vec<$t> {
        fn into(self) -> ArgsValue<'app> {
        ArgsValue { inner: Box::from(self) }
    }
    fn default(&self) -> Option<String> {
        if self.is_empty() {
            None
        } else {
            Some(format!("{:?}",self))
        }
    }
    fn parse(&mut self, args_name: &str, msg: &[String]) -> Result<(), String> {
                self.clear();
                let vs: Vec<&String> = msg.iter().filter(|s| !s.trim().is_empty()).collect();
                for str in &vs {
                    self.push(str.parse::<$t>()
                               .map_err(|_| {
                                            format!("Arguments({}) parse<{}> fails: \"{}\"",
                                                    args_name,
                                                    stringify!($t),
                                                    str)
                                        })?)
                }
                Ok(())
    }
    fn check(&self, args_name: &str) -> Result<(), String> {
        if self.is_empty() {
            Err(format!("Arguments({}) missing", args_name))
        } else {
            Ok(())
        }
    }
        }
    )*)
}
add_vec_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
add_vec_impl! { IpAddr Ipv4Addr Ipv6Addr SocketAddr SocketAddrV4 SocketAddrV6 }