noodles_vcf/header/record/key/
other.rs

1//! VCF header record other key.
2
3use std::{borrow::Borrow, error, fmt, str::FromStr};
4
5/// A nonstandard VCF record key.
6#[derive(Clone, Debug, Eq, Hash, PartialEq)]
7pub struct Other(pub(super) String);
8
9impl AsRef<str> for Other {
10    fn as_ref(&self) -> &str {
11        &self.0
12    }
13}
14
15impl Borrow<str> for Other {
16    fn borrow(&self) -> &str {
17        &self.0
18    }
19}
20
21impl fmt::Display for Other {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        self.as_ref().fmt(f)
24    }
25}
26
27/// An error returned when a raw VCF header record other key fails to a parse.
28#[derive(Clone, Debug, Eq, PartialEq)]
29pub enum ParseError {
30    /// The input is invalid.
31    Invalid,
32}
33
34impl error::Error for ParseError {}
35
36impl fmt::Display for ParseError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::Invalid => write!(f, "invalid input"),
40        }
41    }
42}
43
44impl FromStr for Other {
45    type Err = ParseError;
46
47    fn from_str(s: &str) -> Result<Self, Self::Err> {
48        use super::Key;
49
50        match Key::from(s) {
51            Key::Standard(_) => Err(ParseError::Invalid),
52            Key::Other(key) => Ok(key),
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_from_str() {
63        assert_eq!("NOODLES".parse(), Ok(Other(String::from("NOODLES"))));
64        assert_eq!("INFO".parse::<Other>(), Err(ParseError::Invalid));
65    }
66}