Debian Codegraph @

debian/missing-sources/proptest/examples/dateparser_v1.rs

raw
1 //-
2 // Copyright 2017 Jason Lingle
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use proptest::prelude::*;
11
12 fn parse_date(s: &str) -> Option<(u32, u32, u32)> {
13 if 10 != s.len() {
14 return None;
15 }
16 // !
17 if "-" != &s[4..5] || "-" != &s[7..8] {
18 return None;
19 }
20
21 let year = &s[0..4];
22 let month = &s[6..7]; // !
23 let day = &s[8..10];
24
25 year.parse::<u32>().ok().and_then(|y| {
26 month
27 .parse::<u32>()
28 .ok()
29 .and_then(|m| day.parse::<u32>().ok().map(|d| (y, m, d)))
30 })
31 }
32
33 // NB We omit #[test] on these functions so that main() can call them.
34 proptest! {
35 fn doesnt_crash(s in "\\PC*") {
36 parse_date(&s);
37 }
38 }
39
40 fn main() {
41 assert_eq!(None, parse_date("2017-06-1"));
42 assert_eq!(None, parse_date("2017-06-170"));
43 assert_eq!(None, parse_date("2017006-17"));
44 assert_eq!(None, parse_date("2017-06017"));
45 assert_eq!(Some((2017, 6, 17)), parse_date("2017-06-17"));
46
47 doesnt_crash();
48 }