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
//! VCF record info field.

pub mod key;
pub mod value;

pub use self::{key::Key, value::Value};

use std::{error, fmt};

use crate::header::{
    record::value::{
        map::{info::Type, Info},
        Map,
    },
    Infos,
};

const MISSING_VALUE: &str = ".";
const SEPARATOR: char = '=';

/// An error returned when a raw VCF record info field fails to parse.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
    /// The key is missing.
    MissingKey,
    /// The key is invalid.
    InvalidKey(key::ParseError),
    /// The value is missing.
    MissingValue,
    /// The value is invalid.
    InvalidValue(value::ParseError),
}

impl error::Error for ParseError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::InvalidKey(e) => Some(e),
            Self::InvalidValue(e) => Some(e),
            _ => None,
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MissingKey => f.write_str("missing key"),
            Self::InvalidKey(_) => f.write_str("invalid key"),
            Self::MissingValue => f.write_str("missing value"),
            Self::InvalidValue(_) => f.write_str("invalid value"),
        }
    }
}

pub(crate) fn parse(s: &str, infos: &Infos) -> Result<(Key, Option<Value>), ParseError> {
    const MAX_COMPONENTS: usize = 2;

    let mut components = s.splitn(MAX_COMPONENTS, SEPARATOR);

    let key: Key = components
        .next()
        .ok_or(ParseError::MissingKey)
        .and_then(|t| t.parse().map_err(ParseError::InvalidKey))?;

    let value = if let Some(info) = infos.get(&key) {
        parse_value(&mut components, &key, info)?
    } else {
        let info = Map::<Info>::from(&key);
        parse_value(&mut components, &key, &info)?
    };

    Ok((key, value))
}

pub(crate) fn parse_value<'a, I>(
    iter: &mut I,
    key: &Key,
    info: &Map<Info>,
) -> Result<Option<Value>, ParseError>
where
    I: Iterator<Item = &'a str>,
{
    if let Type::Flag = info.ty() {
        let t = iter.next().unwrap_or_default();

        if t == MISSING_VALUE {
            Ok(None)
        } else {
            Value::from_str_info(t, info)
                .map(Some)
                .map_err(ParseError::InvalidValue)
        }
    } else if matches!(key, Key::Other(..)) {
        if let Some(t) = iter.next() {
            if t == MISSING_VALUE {
                Ok(None)
            } else {
                Value::from_str_info(t, info)
                    .map(Some)
                    .map_err(ParseError::InvalidValue)
            }
        } else {
            Ok(Some(Value::Flag))
        }
    } else if let Some(t) = iter.next() {
        if t == MISSING_VALUE {
            Ok(None)
        } else {
            Value::from_str_info(t, info)
                .map(Some)
                .map_err(ParseError::InvalidValue)
        }
    } else {
        Err(ParseError::MissingValue)
    }
}

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

    #[test]
    fn test_parse() -> Result<(), key::ParseError> {
        let header = crate::Header::builder()
            .add_info(key::ALLELE_COUNT, Map::<Info>::from(&key::ALLELE_COUNT))
            .add_info(
                key::SAMPLES_WITH_DATA_COUNT,
                Map::<Info>::from(&key::SAMPLES_WITH_DATA_COUNT),
            )
            .add_info(
                key::IS_SOMATIC_MUTATION,
                Map::<Info>::from(&key::IS_SOMATIC_MUTATION),
            )
            .add_info(
                key::BREAKEND_EVENT_ID,
                Map::<Info>::from(&key::BREAKEND_EVENT_ID),
            )
            .build();

        assert_eq!(parse("AC=.", header.infos()), Ok((key::ALLELE_COUNT, None)));

        assert_eq!(
            parse("NS=2", header.infos()),
            Ok((key::SAMPLES_WITH_DATA_COUNT, Some(Value::from(2))))
        );

        assert_eq!(
            parse("BQ=1.333", header.infos()),
            Ok((key::BASE_QUALITY, Some(Value::from(1.333))))
        );

        assert_eq!(
            parse("SOMATIC", header.infos()),
            Ok((key::IS_SOMATIC_MUTATION, Some(Value::Flag)))
        );

        assert_eq!(
            parse("EVENT=INV0", header.infos()),
            Ok((
                key::BREAKEND_EVENT_ID,
                Some(Value::from(vec![Some(String::from("INV0"))]))
            ))
        );

        let key = "NDLS".parse()?;
        assert_eq!(
            parse("NDLS=VCF", header.infos()),
            Ok((key, Some(Value::from("VCF"))))
        );

        let key = "FLG".parse()?;
        assert_eq!(parse("FLG", header.infos()), Ok((key, Some(Value::Flag))));

        Ok(())
    }
}