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
//! 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 assembly key.
pub const ASSEMBLY: Key = Key::Standard(Standard::Assembly);

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

/// VCF header record meta key.
pub const META: Key = Key::Standard(Standard::Meta);

/// VCF header record pedigree database key.
pub const PEDIGREE_DB: Key = Key::Standard(Standard::PedigreeDb);

/// 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,
    /// Breakpoint assemblies URI (`assembly`).
    Assembly,
    /// Contig (`contig`).
    Contig,
    /// Meta (`META`).
    Meta,
    /// Pedigree database URI (`pedigreeDB`).
    PedigreeDb,
}

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),
            "assembly" => Some(Self::Assembly),
            "contig" => Some(Self::Contig),
            "META" => Some(Self::Meta),
            "pedigreeDB" => Some(Self::PedigreeDb),
            _ => 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::Assembly => "assembly",
            Self::Contig => "contig",
            Self::Meta => "META",
            Self::PedigreeDb => "pedigreeDB",
        }
    }
}

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!(ASSEMBLY.to_string(), "assembly");
        assert_eq!(CONTIG.to_string(), "contig");
        assert_eq!(META.to_string(), "META");
        assert_eq!(PEDIGREE_DB.to_string(), "pedigreeDB");
        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("assembly"), ASSEMBLY);
        assert_eq!(Key::from("contig"), CONTIG);
        assert_eq!(Key::from("META"), META);
        assert_eq!(Key::from("pedigreeDB"), PEDIGREE_DB);
        assert_eq!(
            Key::from("fileDate"),
            Key::Other(Other(String::from("fileDate")))
        );
    }
}