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
use derive_more::{Display, From, Into};

/// A CSV value
#[derive(Debug, Clone, Hash, PartialEq, Eq, From, Into, Display)]
pub struct RawValue(pub String);

impl RawValue {
    pub fn parse_bool(&self) -> Parsed<bool> {
        match self.0.trim().to_lowercase().as_ref() {
            "" => Parsed::Missing,
            "1" | "t" => Parsed::Some(true),
            "0" | "f" => Parsed::Some(false),
            otherwise => otherwise
                .parse()
                .map(Parsed::Some)
                .unwrap_or(Parsed::Invalid),
        }
    }

    pub fn parse_i64(&self) -> Parsed<i64> {
        match self.0.trim() {
            "" => Parsed::Missing,
            otherwise => otherwise
                .parse()
                .map(Parsed::Some)
                .unwrap_or(Parsed::Invalid),
        }
    }

    pub fn parse_f64(&self) -> Parsed<f64> {
        match self.0.trim().to_lowercase().as_ref() {
            "" => Parsed::Missing,
            "nan" => Parsed::Some(f64::NAN),
            otherwise => otherwise
                .parse()
                .map(Parsed::Some)
                .unwrap_or(Parsed::Invalid),
        }
    }
}

impl From<&str> for RawValue {
    fn from(s: &str) -> Self {
        s.to_string().into()
    }
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub enum Parsed<A> {
    Invalid,
    Missing,
    Some(A),
}

impl<T> Parsed<T> {
    pub fn get(self) -> Option<T> {
        match self {
            Parsed::Missing => None,
            Parsed::Invalid => None,
            Parsed::Some(x) => Some(x),
        }
    }

    pub fn or_else(self, op: impl FnOnce() -> Parsed<T>) -> Parsed<T> {
        match self {
            Parsed::Missing => op(),
            Parsed::Invalid => op(),
            some => some,
        }
    }

    pub fn unwrap_or_else(self, op: impl FnOnce() -> T) -> T {
        match self {
            Parsed::Missing => op(),
            Parsed::Invalid => op(),
            Parsed::Some(t) => t,
        }
    }

    pub fn map<U>(self, op: impl FnOnce(T) -> U) -> Parsed<U> {
        match self {
            Parsed::Missing => Parsed::Missing,
            Parsed::Invalid => Parsed::Invalid,
            Parsed::Some(t) => Parsed::Some(op(t)),
        }
    }

    pub fn is_some(&self) -> bool {
        matches!(self, Parsed::Some(_))
    }
}