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
//! VCF header file format.

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

/// A VCF header file format.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct FileFormat {
    major: u32,
    minor: u32,
}

static PREFIX: &str = "VCFv";

const MAJOR_VERSION: u32 = 4;
const MINOR_VERSION: u32 = 3;

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

impl FileFormat {
    /// Creates a file format.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::header::FileFormat;
    /// let file_format = FileFormat::new(4, 3);
    /// ```
    pub fn new(major: u32, minor: u32) -> Self {
        Self { major, minor }
    }

    /// Returns the major version.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::header::FileFormat;
    /// let file_format = FileFormat::new(4, 3);
    /// assert_eq!(file_format.major(), 4);
    /// ```
    pub fn major(&self) -> u32 {
        self.major
    }

    /// Returns the minor version.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::header::FileFormat;
    /// let file_format = FileFormat::new(4, 3);
    /// assert_eq!(file_format.minor(), 3);
    /// ```
    pub fn minor(&self) -> u32 {
        self.minor
    }
}

impl Default for FileFormat {
    fn default() -> Self {
        Self {
            major: MAJOR_VERSION,
            minor: MINOR_VERSION,
        }
    }
}

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

/// An error returned when a raw VCF header file format fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The input is empty.
    Empty,
    /// The prefix is invalid.
    InvalidPrefix,
    /// The major version is missing.
    MissingMajorVersion,
    /// The major version is invalid.
    InvalidMajorVersion(num::ParseIntError),
    /// The minor version is missing.
    MissingMinorVersion,
    /// The minor version is invalid.
    InvalidMinorVersion(num::ParseIntError),
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::InvalidMajorVersion(e) | Self::InvalidMinorVersion(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::InvalidPrefix => f.write_str("invalid prefix"),
            Self::MissingMajorVersion => f.write_str("missing major version"),
            Self::InvalidMajorVersion(_) => f.write_str("invalid major version"),
            Self::MissingMinorVersion => f.write_str("missing minor version"),
            Self::InvalidMinorVersion(_) => f.write_str("invalid minor version"),
        }
    }
}

impl FromStr for FileFormat {
    type Err = ParseError;

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

        let raw_version = match s.strip_prefix(PREFIX) {
            Some(t) => t,
            None => return Err(ParseError::InvalidPrefix),
        };

        if raw_version.is_empty() {
            return Err(ParseError::MissingMajorVersion);
        }

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

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

        let minor = components
            .next()
            .ok_or(ParseError::MissingMinorVersion)
            .and_then(|t| t.parse().map_err(ParseError::InvalidMinorVersion))?;

        Ok(Self::new(major, minor))
    }
}

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

    #[test]
    fn test_default() {
        let file_format = FileFormat::default();
        assert_eq!(file_format.major(), MAJOR_VERSION);
        assert_eq!(file_format.minor(), MINOR_VERSION);
    }

    #[test]
    fn test_fmt() {
        let file_format = FileFormat::new(4, 3);
        assert_eq!(file_format.to_string(), "VCFv4.3");
    }

    #[test]
    fn test_from_str() {
        assert_eq!("VCFv4.3".parse(), Ok(FileFormat::new(4, 3)));

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

        assert_eq!("4.3".parse::<FileFormat>(), Err(ParseError::InvalidPrefix));
        assert_eq!(
            "NDLv4.3".parse::<FileFormat>(),
            Err(ParseError::InvalidPrefix)
        );

        assert_eq!(
            "VCFv".parse::<FileFormat>(),
            Err(ParseError::MissingMajorVersion)
        );

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

        assert_eq!(
            "VCFv4".parse::<FileFormat>(),
            Err(ParseError::MissingMinorVersion)
        );

        assert!(matches!(
            "VCFv4.x".parse::<FileFormat>(),
            Err(ParseError::InvalidMinorVersion(_))
        ));
    }
}