noodles_vcf/header/record/
key.rs

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
//! VCF header record key.

pub mod other;

pub use self::other::Other;

use std::fmt;

/// VCF header record file format key.
pub const FILE_FORMAT: Key = Key::Standard(Standard::FileFormat);

/// VCF header record info key.
pub const INFO: Key = Key::Standard(Standard::Info);

/// VCF header record filter key.
pub const FILTER: Key = Key::Standard(Standard::Filter);

/// VCF header record format key.
pub const FORMAT: Key = Key::Standard(Standard::Format);

/// VCF header record alternative allele key.
pub const ALTERNATIVE_ALLELE: Key = Key::Standard(Standard::AlternativeAllele);

/// VCF header record contig key.
pub const CONTIG: Key = Key::Standard(Standard::Contig);

/// A standard VCF record key.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Standard {
    /// File format (`fileformat`).
    FileFormat,
    /// Information (`INFO`).
    Info,
    /// Filter (`FILTER`)
    Filter,
    /// Genotype format (`FORMAT`).
    Format,
    /// Symbolic alternate allele (`ALT`).
    AlternativeAllele,
    /// Contig (`contig`).
    Contig,
}

impl Standard {
    fn new(s: &str) -> Option<Self> {
        match s {
            "fileformat" => Some(Self::FileFormat),
            "INFO" => Some(Self::Info),
            "FILTER" => Some(Self::Filter),
            "FORMAT" => Some(Self::Format),
            "ALT" => Some(Self::AlternativeAllele),
            "contig" => Some(Self::Contig),
            _ => None,
        }
    }
}

impl AsRef<str> for Standard {
    fn as_ref(&self) -> &str {
        match self {
            Self::FileFormat => "fileformat",
            Self::Info => "INFO",
            Self::Filter => "FILTER",
            Self::Format => "FORMAT",
            Self::AlternativeAllele => "ALT",
            Self::Contig => "contig",
        }
    }
}

impl fmt::Display for Standard {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

/// A VCF header record key.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Key {
    /// A standard key.
    Standard(Standard),
    /// Any nonstandard key.
    Other(Other),
}

impl Key {
    /// Creates a nonstandard key.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::header::record::Key;
    /// assert!(Key::other("INFO").is_none());
    /// assert!(Key::other("fileDate").is_some());
    /// ```
    pub fn other(s: &str) -> Option<Other> {
        match Self::from(s) {
            Self::Standard(_) => None,
            Self::Other(tag) => Some(tag),
        }
    }
}

impl AsRef<str> for Key {
    fn as_ref(&self) -> &str {
        match self {
            Self::Standard(tag) => tag.as_ref(),
            Self::Other(tag) => tag.as_ref(),
        }
    }
}

impl fmt::Display for Key {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl From<&str> for Key {
    fn from(s: &str) -> Self {
        match Standard::new(s) {
            Some(tag) => Self::Standard(tag),
            None => Self::Other(Other(s.into())),
        }
    }
}

impl From<String> for Key {
    fn from(s: String) -> Self {
        match Standard::new(&s) {
            Some(tag) => Self::Standard(tag),
            None => Self::Other(Other(s)),
        }
    }
}

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

    #[test]
    fn test_fmt() {
        assert_eq!(FILE_FORMAT.to_string(), "fileformat");
        assert_eq!(INFO.to_string(), "INFO");
        assert_eq!(FILTER.to_string(), "FILTER");
        assert_eq!(FORMAT.to_string(), "FORMAT");
        assert_eq!(ALTERNATIVE_ALLELE.to_string(), "ALT");
        assert_eq!(CONTIG.to_string(), "contig");
        assert_eq!(
            Key::Other(Other(String::from("fileDate"))).to_string(),
            "fileDate"
        );
    }

    #[test]
    fn test_from() {
        assert_eq!(Key::from("fileformat"), FILE_FORMAT);
        assert_eq!(Key::from("INFO"), INFO);
        assert_eq!(Key::from("FILTER"), FILTER);
        assert_eq!(Key::from("FORMAT"), FORMAT);
        assert_eq!(Key::from("ALT"), ALTERNATIVE_ALLELE);
        assert_eq!(Key::from("contig"), CONTIG);
        assert_eq!(
            Key::from("fileDate"),
            Key::Other(Other(String::from("fileDate")))
        );
    }
}