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
//! VCF record alternate bases allele symbol and structural variant.

pub mod structural_variant;

pub use self::structural_variant::StructuralVariant;

use std::{error, fmt, str::FromStr};

/// A VCF alternate bases allele symbol.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Symbol {
    /// A structural variant.
    StructuralVariant(StructuralVariant),
    /// A nonstructural variant.
    NonstructuralVariant(String),
    /// An unspecific symbol.
    Unspecified,
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StructuralVariant(sv) => write!(f, "{}", sv),
            Self::NonstructuralVariant(nsv) => f.write_str(nsv),
            Self::Unspecified => f.write_str("*"),
        }
    }
}

/// An error returned when a raw VCF record alternate base allele symbol fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The input is empty.
    Empty,
    /// The nonstructural variant is invalid.
    InvalidNonstructuralVariant,
}

impl error::Error for ParseError {}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => f.write_str("empty input"),
            Self::InvalidNonstructuralVariant => f.write_str("invalid nonstructural variant"),
        }
    }
}

impl FromStr for Symbol {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Err(ParseError::Empty),
            "*" | "NON_REF" => Ok(Self::Unspecified),
            _ => s
                .parse::<StructuralVariant>()
                .map(Self::StructuralVariant)
                .or_else(|_| {
                    if is_valid_id(s) {
                        Ok(Self::NonstructuralVariant(s.into()))
                    } else {
                        Err(ParseError::InvalidNonstructuralVariant)
                    }
                }),
        }
    }
}

fn is_valid_id_char(c: char) -> bool {
    !c.is_ascii_whitespace() && !matches!(c, ',' | '<' | '>')
}

fn is_valid_id(s: &str) -> bool {
    s.chars().all(is_valid_id_char)
}

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

    #[test]
    fn test_fmt() {
        let symbol =
            Symbol::StructuralVariant(StructuralVariant::from(structural_variant::Type::Deletion));
        assert_eq!(symbol.to_string(), "DEL");

        let symbol = Symbol::NonstructuralVariant(String::from("CN:0"));
        assert_eq!(symbol.to_string(), "CN:0");

        let symbol = Symbol::Unspecified;
        assert_eq!(symbol.to_string(), "*");
    }

    #[test]
    fn test_from_str() {
        assert_eq!(
            "DEL".parse(),
            Ok(Symbol::StructuralVariant(StructuralVariant::from(
                structural_variant::Type::Deletion
            )))
        );

        assert_eq!(
            "CN:0".parse(),
            Ok(Symbol::NonstructuralVariant(String::from("CN:0")))
        );

        assert_eq!("NON_REF".parse(), Ok(Symbol::Unspecified));
        assert_eq!("*".parse(), Ok(Symbol::Unspecified));

        assert_eq!("".parse::<Symbol>(), Err(ParseError::Empty));

        assert_eq!(
            "CN 0".parse::<Symbol>(),
            Err(ParseError::InvalidNonstructuralVariant)
        );
        assert_eq!(
            "CN,0".parse::<Symbol>(),
            Err(ParseError::InvalidNonstructuralVariant)
        );
        assert_eq!(
            "CN>0".parse::<Symbol>(),
            Err(ParseError::InvalidNonstructuralVariant)
        );
        assert_eq!(
            "CN<0".parse::<Symbol>(),
            Err(ParseError::InvalidNonstructuralVariant)
        );
    }
}