msn_kit/io/
mod.rs

1// (c) Copyright 2021 Trent Hauck
2// All Rights Reserved
3//! Module containing input and output related functionality.
4
5pub mod mgf_parser;
6pub mod mzml_parser;
7
8use std::str::FromStr;
9
10/// Types of formats that can be read or written.
11///
12/// # Examples
13///
14/// Create `Format::Mgf` from a string.
15///
16/// ```
17/// use std::str::FromStr;
18/// use msn_kit::io::Format;
19///
20/// let f = Format::from_str("mgf").unwrap();
21/// assert_eq!(f, Format::Mgf);
22/// ```
23#[derive(Debug, PartialEq)]
24pub enum Format {
25    /// json newline format
26    Json,
27
28    /// Mascot Generic Format
29    Mgf,
30
31    /// mzML format specified here: <https://www.psidev.info/mzML>
32    MzML,
33}
34
35/// Creates a `Format` type, from a string.
36impl FromStr for Format {
37    type Err = &'static str;
38
39    fn from_str(s: &str) -> Result<Self, Self::Err> {
40        match s {
41            "json" => Ok(Self::Json),
42            "mgf" => Ok(Self::Mgf),
43            "mzml" => Ok(Self::MzML),
44            _ => Err("Cannot parse input format."),
45        }
46    }
47}
48
49mod tests {
50    use crate::io::Format;
51    use std::str::FromStr;
52
53    #[test]
54    fn from_str() {
55        let inputs = vec!["json", "mgf", "mzml"];
56        let expected = vec![Format::Json, Format::Mgf, Format::MzML];
57
58        let actual: Vec<Format> = inputs
59            .into_iter()
60            .map(|i| Format::from_str(i).unwrap())
61            .collect();
62        assert_eq!(expected, actual);
63    }
64}