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
//! GFF directive version.

use std::{error, fmt, num, str::FromStr};

const MAJOR_VERSION: u32 = 3;

const DELIMITER: char = '.';
const MAX_COMPONENT_COUNT: usize = 3;

/// A GFF directive version.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GffVersion {
    major: u32,
    minor: Option<u32>,
    patch: Option<u32>,
}

impl GffVersion {
    /// Returns the major version.
    ///
    /// # Examples
    ///
    /// ```
    /// # use noodles_gff::directive::gff_version;
    /// use noodles_gff::directive::GffVersion;
    /// let version: GffVersion = "3.1.26".parse()?;
    /// assert_eq!(version.major(), 3);
    /// # Ok::<(), gff_version::ParseError>(())
    /// ```
    pub fn major(&self) -> u32 {
        self.major
    }

    /// Returns the minor version.
    ///
    /// # Examples
    ///
    /// ```
    /// # use noodles_gff::directive::gff_version;
    /// use noodles_gff::directive::GffVersion;
    /// let version: GffVersion = "3.1.26".parse()?;
    /// assert_eq!(version.minor(), Some(1));
    /// # Ok::<(), gff_version::ParseError>(())
    /// ```
    pub fn minor(&self) -> Option<u32> {
        self.minor
    }

    /// Returns the patch version.
    ///
    /// # Examples
    ///
    /// ```
    /// # use noodles_gff::directive::gff_version;
    /// use noodles_gff::directive::GffVersion;
    /// let version: GffVersion = "3.1.26".parse()?;
    /// assert_eq!(version.patch(), Some(26));
    /// # Ok::<(), gff_version::ParseError>(())
    /// ```
    pub fn patch(&self) -> Option<u32> {
        self.patch
    }
}

impl Default for GffVersion {
    fn default() -> Self {
        Self {
            major: MAJOR_VERSION,
            minor: None,
            patch: None,
        }
    }
}

impl fmt::Display for GffVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.major())?;

        if let Some(minor) = self.minor() {
            write!(f, "{DELIMITER}{minor}")?;

            if let Some(patch) = self.patch() {
                write!(f, "{DELIMITER}{patch}")?;
            }
        }

        Ok(())
    }
}

/// An error returned when a raw GFF version directive fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The input is empty.
    Empty,
    /// The major version is missing.
    MissingMajorVersion,
    /// The major version is invalid.
    InvalidMajorVersion(num::ParseIntError),
    /// The minor version is invalid.
    InvalidMinorVersion(num::ParseIntError),
    /// The patch version is invalid.
    InvalidPatchVersion(num::ParseIntError),
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::InvalidMajorVersion(e)
            | Self::InvalidMinorVersion(e)
            | Self::InvalidPatchVersion(e) => Some(e),
            _ => None,
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => f.write_str("empty input"),
            Self::MissingMajorVersion => f.write_str("missing major version"),
            Self::InvalidMajorVersion(_) => f.write_str("invalid major version"),
            Self::InvalidMinorVersion(_) => f.write_str("invalid minor version"),
            Self::InvalidPatchVersion(_) => f.write_str("invalid patch version"),
        }
    }
}

impl FromStr for GffVersion {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Err(ParseError::Empty);
        }

        let mut components = s.splitn(MAX_COMPONENT_COUNT, DELIMITER);

        let major = components
            .next()
            .ok_or(ParseError::MissingMajorVersion)
            .and_then(|t| t.parse().map_err(ParseError::InvalidMajorVersion))?;

        let minor = match components.next() {
            Some(t) => t
                .parse()
                .map(Some)
                .map_err(ParseError::InvalidMinorVersion)?,
            None => None,
        };

        let patch = match components.next() {
            Some(t) => t
                .parse()
                .map(Some)
                .map_err(ParseError::InvalidPatchVersion)?,
            None => None,
        };

        Ok(Self {
            major,
            minor,
            patch,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fmt() {
        let version = GffVersion {
            major: 3,
            minor: None,
            patch: None,
        };
        assert_eq!(version.to_string(), "3");

        let version = GffVersion {
            major: 3,
            minor: Some(1),
            patch: None,
        };
        assert_eq!(version.to_string(), "3.1");

        let version = GffVersion {
            major: 3,
            minor: Some(1),
            patch: Some(26),
        };
        assert_eq!(version.to_string(), "3.1.26");
    }

    #[test]
    fn test_from_str() {
        assert_eq!(
            "3".parse(),
            Ok(GffVersion {
                major: 3,
                minor: None,
                patch: None
            })
        );

        assert_eq!(
            "3.1".parse(),
            Ok(GffVersion {
                major: 3,
                minor: Some(1),
                patch: None
            })
        );

        assert_eq!(
            "3.1.26".parse(),
            Ok(GffVersion {
                major: 3,
                minor: Some(1),
                patch: Some(26),
            })
        );

        assert_eq!("".parse::<GffVersion>(), Err(ParseError::Empty));

        assert!(matches!(
            "a".parse::<GffVersion>(),
            Err(ParseError::InvalidMajorVersion(_))
        ));

        assert!(matches!(
            "3.b".parse::<GffVersion>(),
            Err(ParseError::InvalidMinorVersion(_))
        ));

        assert!(matches!(
            "3.1.c".parse::<GffVersion>(),
            Err(ParseError::InvalidPatchVersion(_))
        ));
    }
}