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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//! [`Property`] converter.
use crate::*;
use core::convert::TryFrom;

impl IntoProperty for Property {
    fn into_property(self) -> Property {
        self
    }
}

impl IntoProperty for String {
    fn into_property(self) -> Property {
        Property::Str(self)
    }
}

impl IntoProperty for &str {
    fn into_property(self) -> Property {
        Property::Str(self.to_owned())
    }
}

macro_rules! impl_into_property {
    ($x:ident) => {
        impl IntoProperty for $x {
            #[allow(trivial_numeric_casts)]
            fn into_property(self) -> Property {
                Property::Int(self as i64)
            }
        }
    };
    ($x:ident, $($y:ident),+) => {
        impl_into_property!($x);
        impl_into_property!($($y),+);
    };
}

impl_into_property!(u8, u16, u32, i8, i16, i32, i64);

macro_rules! impl_into_property_str {
    ($x:ident) => {
        impl IntoProperty for $x {
            fn into_property(self) -> Property {
                Property::Str(self.to_string())
            }
        }
    };
    ($x:ident, $($y:ident),+) => {
        impl_into_property_str!($x);
        impl_into_property_str!($($y),+);
    };
}

impl_into_property_str!(u64, u128, i128, isize, usize);

macro_rules! impl_float_into_property {
    ($x:ident) => {
        impl IntoProperty for $x {
            #[allow(trivial_numeric_casts)]
            fn into_property(self) -> Property {
                Property::Float(self as f64)
            }
        }
    };
    ($x:ident, $($y:ident),+) => {
        impl_float_into_property!($x);
        impl_float_into_property!($($y),+);
    };
}

impl_float_into_property!(f32, f64);

impl FromProperty for Property {
    fn from_property(a: Property) -> Result<Self, PropertyError> {
        Ok(a)
    }
}

fn check_f64(f: f64) -> Result<f64, PropertyError> {
    if f.is_finite() {
        Ok(f)
    } else {
        Err(PropertyError::parse_failed("f64 value is infinite"))
    }
}

impl FromProperty for String {
    fn from_property(p: Property) -> Result<Self, PropertyError> {
        match p {
            Property::Str(str) => Ok(str),
            Property::Int(i64) => Ok(i64.to_string()),
            Property::Float(f64) => Ok(check_f64(f64)?.to_string()),
            Property::Bool(bool) => Ok(bool.to_string()),
        }
    }
}

impl FromProperty for char {
    fn from_property(p: Property) -> Result<Self, PropertyError> {
        match p {
            Property::Str(str) => {
                let mut chars = str.chars();
                if let Some(c) = chars.next() {
                    if chars.next().is_none() {
                        return Ok(c);
                    }
                }
                Err(PropertyError::parse_failed("Invalid char value"))
            }
            Property::Int(_) => Err(PropertyError::parse_failed(
                "Integer value cannot convert to char",
            )),
            Property::Float(_) => Err(PropertyError::parse_failed(
                "Float value cannot convert to char",
            )),
            Property::Bool(_) => Err(PropertyError::parse_failed(
                "Bool value cannot convert to char",
            )),
        }
    }
}

macro_rules! impl_from_property {
    ($x:ident) => {
        impl FromProperty for $x {
            fn from_property(p: Property) -> Result<Self, PropertyError> {
                match p {
                    Property::Str(str) => Ok(str.parse::<$x>()?),
                    Property::Int(i64) => Ok(<$x>::try_from(i64)?),
                    Property::Float(f64) => Ok(check_f64(f64)? as $x),
                    Property::Bool(_) => Err(PropertyError::ParseFail(
                        format!("Bool value cannot convert to {}",stringify!($x)),
                    )),
                }
            }
        }
    };
    ($x:ident, $($y:ident),+) => {
        impl_from_property!($x);
        impl_from_property!($($y),+);
    };
}

impl_from_property!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);

macro_rules! impl_float_from_property {
    ($x:ident) => {
        impl FromProperty for $x {
            #[allow(trivial_numeric_casts)]
            fn from_property(p: Property) -> Result<Self, PropertyError> {
                match p {
                    Property::Str(str) => Ok(str.parse::<$x>()?),
                    Property::Int(i64) => Ok(i64 as $x),
                    Property::Float(f64) => Ok(check_f64(f64)? as $x),
                    Property::Bool(_) => Err(PropertyError::ParseFail(
                        format!("Bool value cannot convert to {}",stringify!($x)),
                    )),
                }
            }
        }
    };
    ($x:ident, $($y:ident),+) => {
        impl_float_from_property!($x);
        impl_float_from_property!($($y),+);
    };
}

impl_float_from_property!(f64, f32);

impl FromProperty for bool {
    fn from_property(p: Property) -> Result<Self, PropertyError> {
        lazy_static::lazy_static! {
        static ref STR_YES: HashSet<String> = vec!["yes","y","1","true","t"].into_iter().map(|a|a.to_string()).collect();
        static ref STR_NO: HashSet<String> = vec!["no","n","0","false","f"].into_iter().map(|a|a.to_string()).collect();
        }
        match p {
            Property::Str(str) => {
                let str = str.to_lowercase();
                if STR_YES.contains(&str) {
                    Ok(true)
                } else if STR_NO.contains(&str) {
                    Ok(false)
                } else {
                    Err(PropertyError::parse_failed("Str cannot convert to bool"))
                }
            }
            Property::Int(i64) => Ok(i64 != 0),
            Property::Float(f64) => Ok(check_f64(f64)? != 0.0),
            Property::Bool(bool) => Ok(bool),
        }
    }
}

#[cfg(feature = "enable_toml")]
impl FromProperty for ::toml::value::Datetime {
    fn from_property(p: Property) -> Result<Self, PropertyError> {
        use std::str::FromStr;
        match p {
            Property::Str(v) => Ok(Self::from_str(&v)?),
            _ => Err(PropertyError::parse_failed(
                "Datetime only support string value parse.",
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    #[test]
    fn bool_tests() {
        assert_eq!(
            Ok(true),
            bool::from_property(Property::Str("yes".to_owned()))
        );
        assert_eq!(Ok(true), bool::from_property(Property::Str("y".to_owned())));
        assert_eq!(Ok(true), bool::from_property(Property::Str("1".to_owned())));
        assert_eq!(
            Ok(true),
            bool::from_property(Property::Str("true".to_owned()))
        );
        assert_eq!(Ok(true), bool::from_property(Property::Str("t".to_owned())));
        assert_eq!(Ok(true), bool::from_property(Property::Int(1)));
        assert_eq!(Ok(true), bool::from_property(Property::Float(1.0)));
        assert_eq!(Ok(true), bool::from_property(Property::Bool(true)));

        assert_eq!(
            Ok(false),
            bool::from_property(Property::Str("no".to_owned()))
        );
        assert_eq!(
            Ok(false),
            bool::from_property(Property::Str("n".to_owned()))
        );
        assert_eq!(
            Ok(false),
            bool::from_property(Property::Str("0".to_owned()))
        );
        assert_eq!(
            Ok(false),
            bool::from_property(Property::Str("false".to_owned()))
        );
        assert_eq!(
            Ok(false),
            bool::from_property(Property::Str("f".to_owned()))
        );
        assert_eq!(Ok(false), bool::from_property(Property::Int(0)));
        assert_eq!(Ok(false), bool::from_property(Property::Float(0.0)));
        assert_eq!(Ok(false), bool::from_property(Property::Bool(false)));

        assert_eq!(
            true,
            bool::from_property(Property::Str("x".to_owned())).is_err()
        );
    }

    #[test]
    fn option_test() {
        assert_eq!(
            Ok(None),
            <Option<String>>::from_err(PropertyError::NotFound("".to_owned()))
        );
    }

    #[quickcheck]
    fn num_tests(i: i64) {
        assert_eq!(Ok(i), i64::from_property(Property::Str(format!("{}", i))));
        assert_eq!(Ok(i), i64::from_property(Property::Int(i)));
        assert_eq!(
            Err(PropertyError::parse_failed(
                "Bool value cannot convert to i64"
            )),
            i64::from_property(Property::Bool(true))
        );
    }

    #[quickcheck]
    fn u8_tests(i: u8) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn u16_tests(i: u16) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn u32_tests(i: u32) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn u64_tests(i: u64) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn u128_tests(i: u128) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i8_tests(i: i8) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i16_tests(i: i16) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i32_tests(i: i32) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i64_tests(i: i64) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i128_tests(i: i128) -> bool {
        FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn f32_tests(i: f32) -> bool {
        !i.is_finite() || FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn f64_tests(i: f64) -> bool {
        !i.is_finite() || FromProperty::from_property(IntoProperty::into_property(i)) == Ok(i)
    }

    #[quickcheck]
    fn i64_convert_tests(i: i64) -> bool {
        let u8: Result<u8, PropertyError> = FromProperty::from_property(Property::Int(i));
        let u16: Result<u16, PropertyError> = FromProperty::from_property(Property::Int(i));
        let u32: Result<u32, PropertyError> = FromProperty::from_property(Property::Int(i));
        let u64: Result<u64, PropertyError> = FromProperty::from_property(Property::Int(i));
        let u128: Result<u128, PropertyError> = FromProperty::from_property(Property::Int(i));
        let i8: Result<i8, PropertyError> = FromProperty::from_property(Property::Int(i));
        let i16: Result<i16, PropertyError> = FromProperty::from_property(Property::Int(i));
        let i32: Result<i32, PropertyError> = FromProperty::from_property(Property::Int(i));
        let i64: Result<i64, PropertyError> = FromProperty::from_property(Property::Int(i));
        let i128: Result<i128, PropertyError> = FromProperty::from_property(Property::Int(i));
        let f32: Result<f32, PropertyError> = FromProperty::from_property(Property::Int(i));
        let f64: Result<f64, PropertyError> = FromProperty::from_property(Property::Int(i));
        vec![
            i >= 0 && i <= (u8::MAX as i64) && u8.is_ok() || u8.is_err(),
            i >= 0 && i <= (u16::MAX as i64) && u16.is_ok() || u16.is_err(),
            i >= 0 && i <= (u32::MAX as i64) && u32.is_ok() || u32.is_err(),
            i >= 0 && u64.is_ok() || u64.is_err(),
            i >= 0 && u128.is_ok() || u128.is_err(),
            i >= (i8::MIN as i64) && i <= (i8::MAX as i64) && i8.is_ok() || i8.is_err(),
            i >= (i16::MIN as i64) && i <= (i16::MAX as i64) && i16.is_ok() || i16.is_err(),
            i >= (i32::MIN as i64) && i <= (i32::MAX as i64) && i32.is_ok() || i32.is_err(),
            i64.is_ok(),
            i128.is_ok(),
            f32.is_ok() && f32.unwrap_or(0.0).is_finite(),
            f64.is_ok() && f64.unwrap_or(0.0).is_finite(),
        ]
        .iter()
        .all(|a| *a)
    }

    #[quickcheck]
    fn f64_convert_tests(i: f64) -> bool {
        let u8: Result<u8, PropertyError> = FromProperty::from_property(Property::Float(i));
        let u16: Result<u16, PropertyError> = FromProperty::from_property(Property::Float(i));
        let u32: Result<u32, PropertyError> = FromProperty::from_property(Property::Float(i));
        let u64: Result<u64, PropertyError> = FromProperty::from_property(Property::Float(i));
        let u128: Result<u128, PropertyError> = FromProperty::from_property(Property::Float(i));
        let i8: Result<i8, PropertyError> = FromProperty::from_property(Property::Float(i));
        let i16: Result<i16, PropertyError> = FromProperty::from_property(Property::Float(i));
        let i32: Result<i32, PropertyError> = FromProperty::from_property(Property::Float(i));
        let i64: Result<i64, PropertyError> = FromProperty::from_property(Property::Float(i));
        let i128: Result<i128, PropertyError> = FromProperty::from_property(Property::Float(i));
        let f32: Result<f32, PropertyError> = FromProperty::from_property(Property::Float(i));
        let f64: Result<f64, PropertyError> = FromProperty::from_property(Property::Float(i));

        vec![
            i.is_finite() && u8.is_ok() || u8.is_err(),
            i.is_finite() && u16.is_ok() || u16.is_err(),
            i.is_finite() && u32.is_ok() || u32.is_err(),
            i.is_finite() && u64.is_ok() || u64.is_err(),
            i.is_finite() && u128.is_ok() || u128.is_err(),
            i.is_finite() && i8.is_ok() || i8.is_err(),
            i.is_finite() && i16.is_ok() || i16.is_err(),
            i.is_finite() && i32.is_ok() || i32.is_err(),
            i.is_finite() && i64.is_ok() || i64.is_err(),
            i.is_finite() && i128.is_ok() || i128.is_err(),
            i.is_finite() && f32.is_ok() || f32.is_err(),
            i.is_finite() && f64.is_ok() || f64.is_err(),
        ]
        .iter()
        .all(|a| *a)
    }
}