summaryrefslogtreecommitdiff
path: root/learn_20/src/main.rs
blob: 8f3961da2b2a044e72c6bac0219dfba28c3ea9ad (plain)
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
#[derive(Debug, PartialEq, Eq)]
struct Version {
    major: u32,
    minor: u32,
    patch: u32,
}

impl Version {
    fn new(major: u32, minor: u32, patch: u32) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }
}

impl std::fmt::Display for Version {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

#[derive(Debug, PartialEq, Eq)]
enum ParseVersionError {
    WrongCount,
    EmptyComponent,
    InvalidComponent,
    OutOfRange,
}

impl std::fmt::Display for ParseVersionError {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        let msg = match self {
            ParseVersionError::WrongCount => "wrong component count",
            ParseVersionError::EmptyComponent => "empty component",
            ParseVersionError::InvalidComponent => "invalid component",
            ParseVersionError::OutOfRange => "out of range",
        };
        write!(f, "{}", msg)
    }
}

impl std::str::FromStr for Version {
    type Err = ParseVersionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let elts: Vec<&str> = s.split('.').collect();
        if elts.len() != 3 {
            return Err(ParseVersionError::WrongCount);
        }
        
        for i in 0..3 {
            if elts[i].is_empty() {
                return Err(ParseVersionError::EmptyComponent);
            }
        }

        for i in 0..3 {
            if !elts[i].bytes().all(|b| b.is_ascii_digit()) {
                return Err(ParseVersionError::InvalidComponent);
            }
        }
        
        let major = elts[0].parse::<u32>().map_err(|_|
            ParseVersionError::OutOfRange)?;
        let minor = elts[1].parse::<u32>().map_err(|_|
            ParseVersionError::OutOfRange)?;
        let patch = elts[2].parse::<u32>().map_err(|_|
            ParseVersionError::OutOfRange)?;
        Ok(Version::new(major, minor, patch))
    }
}

fn main() {
    ()
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use crate::*;

    #[test]
    fn test_new() {
        let v = Version::new(1,2,3);
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
    }

    #[test]
    fn test_display() {
        let v = Version::new(1,2,3);
        assert_eq!(v.to_string(), "1.2.3");
        assert_eq!(format!("{}", v), "1.2.3");
    }

    #[test]
    fn test_from_str() {
        let v = Version::new(1,2,3);
        assert!(Version::from_str("1.2.3").is_ok_and(|x| x == v));
        assert!(Version::from_str("0.0.0").is_ok_and(|x| x == Version::new(0,0,0)));
        assert!(Version::from_str("01.02.03").is_ok_and(|x| x == v));
        assert!(Version::from_str("4294967295.0.0").is_ok());
        assert!(Version::from_str("1.2").is_err_and(|x| x == ParseVersionError::WrongCount));
        assert!(Version::from_str("1.2.3.4").is_err_and(|x| x == ParseVersionError::WrongCount));
        assert!(Version::from_str("1..3").is_err_and(|x| x == ParseVersionError::EmptyComponent));
        assert!(Version::from_str("1.a.3").is_err_and(|x| x == ParseVersionError::InvalidComponent));
        assert!(Version::from_str("1.-2.3").is_err_and(|x| x == ParseVersionError::InvalidComponent));
        assert!(Version::from_str("1.4294967296.3").is_err_and(|x| x == ParseVersionError::OutOfRange));
        assert!(Version::from_str("1.99999999999999999999999.3").is_err_and(|x| x == ParseVersionError::OutOfRange));
        assert!(Version::from_str(" 1.2.3").is_err_and(|x| x == ParseVersionError::InvalidComponent));
    }

    #[test]
    fn test_round_trip() {
        let version: Version = "1.24.3".parse().unwrap();
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 24);
        assert_eq!(version.patch, 3);
        assert_eq!(version.to_string(), "1.24.3");

        let version1 = Version::from_str("2.5.10").unwrap();
        assert_eq!(version1, Version::new(2, 5, 10));
    }
}