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
// (c) Copyright 2021 Trent Hauck
// All Rights Reserved
//! Module containing input and output related functionality.

pub mod mgf_parser;
pub mod mzml_parser;

use std::str::FromStr;

/// Types of formats that can be read or written.
///
/// # Examples
///
/// Create `Format::Mgf` from a string.
///
/// ```
/// use std::str::FromStr;
/// use msn_kit::io::Format;
///
/// let f = Format::from_str("mgf").unwrap();
/// assert_eq!(f, Format::Mgf);
/// ```
#[derive(Debug, PartialEq)]
pub enum Format {
    /// json newline format
    Json,

    /// Mascot Generic Format
    Mgf,

    /// mzML format specified here: <https://www.psidev.info/mzML>
    MzML,
}

/// Creates a `Format` type, from a string.
impl FromStr for Format {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "json" => Ok(Self::Json),
            "mgf" => Ok(Self::Mgf),
            "mzml" => Ok(Self::MzML),
            _ => Err("Cannot parse input format."),
        }
    }
}

mod tests {
    use crate::io::Format;
    use std::str::FromStr;

    #[test]
    fn from_str() {
        let inputs = vec!["json", "mgf", "mzml"];
        let expected = vec![Format::Json, Format::Mgf, Format::MzML];

        let actual: Vec<Format> = inputs
            .into_iter()
            .map(|i| Format::from_str(i).unwrap())
            .collect();
        assert_eq!(expected, actual);
    }
}