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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! VCF record info field.

pub mod value;

pub use self::value::Value;

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

use crate::header::{
    self,
    info::{Key, Type},
    Info, Infos,
};

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

/// A VCF record info field.
#[derive(Clone, Debug, PartialEq)]
pub struct Field {
    key: Key,
    value: Option<Value>,
}

impl Field {
    /// Parses a raw VCF record info field.
    pub fn try_from_str(s: &str, infos: &Infos) -> Result<Self, ParseError> {
        parse(s, infos)
    }

    /// Creates a VCF record info field.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::{
    ///     header::info::Key,
    ///     record::info::{field::Value, Field},
    /// };
    ///
    /// let field = Field::new(Key::SamplesWithDataCount, Some(Value::Integer(1)));
    /// ```
    pub fn new(key: Key, value: Option<Value>) -> Self {
        Self { key, value }
    }

    /// Returns the field key.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::{
    ///     header::info::Key,
    ///     record::info::{field::Value, Field},
    /// };
    ///
    /// let field = Field::new(Key::SamplesWithDataCount, Some(Value::Integer(1)));
    /// assert_eq!(field.key(), &Key::SamplesWithDataCount);
    /// ```
    pub fn key(&self) -> &Key {
        &self.key
    }

    /// Returns the field value.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::{
    ///     header::info::Key,
    ///     record::info::{field::Value, Field},
    /// };
    ///
    /// let field = Field::new(Key::SamplesWithDataCount, Some(Value::Integer(1)));
    /// assert_eq!(field.value(), Some(&Value::Integer(1)));
    /// ```
    pub fn value(&self) -> Option<&Value> {
        self.value.as_ref()
    }

    /// Returns a mutable reference to the value.
    ///
    /// # Examples
    ///
    /// ```
    /// use noodles_vcf::{
    ///     header::info::Key,
    ///     record::info::{field::Value, Field},
    /// };
    ///
    /// let mut field = Field::new(Key::SamplesWithDataCount, Some(Value::Integer(1)));
    /// *field.value_mut() = Some(Value::Integer(2));
    /// assert_eq!(field.value(), Some(&Value::Integer(2)));
    /// ```
    pub fn value_mut(&mut self) -> &mut Option<Value> {
        &mut self.value
    }
}

impl fmt::Display for Field {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.value() {
            None => write!(f, "{}{}{}", self.key, SEPARATOR, MISSING_VALUE),
            Some(Value::Flag) => write!(f, "{}", self.key),
            Some(value) => write!(f, "{}{}{}", self.key, SEPARATOR, value),
        }
    }
}

/// 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(header::info::key::ParseError),
    /// The value is missing.
    MissingValue,
    /// The value is invalid.
    InvalidValue(value::ParseError),
}

impl error::Error for ParseError {}

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(e) => write!(f, "invalid key: {}", e),
            Self::MissingValue => f.write_str("missing value"),
            Self::InvalidValue(e) => write!(f, "invalid value: {}", e),
        }
    }
}

impl FromStr for Field {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from_str(s, &Infos::default())
    }
}

fn parse(s: &str, infos: &Infos) -> Result<Field, 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, info)?
    } else {
        let info = header::Info::from(key.clone());
        parse_value(&mut components, &info)?
    };

    Ok(Field::new(key, value))
}

fn parse_value<'a, I>(iter: &mut I, info: &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 let Key::Other(..) = info.id() {
        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_fmt() -> Result<(), crate::header::info::key::ParseError> {
        let field = Field::new(Key::AlleleCount, None);
        assert_eq!(field.to_string(), "AC=.");

        let field = Field::new(Key::SamplesWithDataCount, Some(Value::Integer(2)));
        assert_eq!(field.to_string(), "NS=2");

        let field = Field::new(Key::BaseQuality, Some(Value::Float(1.333)));
        assert_eq!(field.to_string(), "BQ=1.333");

        let field = Field::new(Key::IsSomaticMutation, Some(Value::Flag));
        assert_eq!(field.to_string(), "SOMATIC");

        let field = Field::new("NOODLES".parse()?, Some(Value::String(String::from("VCF"))));
        assert_eq!(field.to_string(), "NOODLES=VCF");

        Ok(())
    }

    #[test]
    fn test_parse() -> Result<(), crate::header::info::key::ParseError> {
        let header = crate::Header::builder()
            .add_info(Info::from(Key::AlleleCount))
            .add_info(Info::from(Key::SamplesWithDataCount))
            .add_info(Info::from(Key::IsSomaticMutation))
            .add_info(Info::from(Key::BreakendEventId))
            .build();

        assert_eq!(
            parse("AC=.", header.infos()),
            Ok(Field::new(Key::AlleleCount, None))
        );

        assert_eq!(
            parse("NS=2", header.infos()),
            Ok(Field::new(
                Key::SamplesWithDataCount,
                Some(Value::Integer(2))
            ))
        );

        assert_eq!(
            parse("BQ=1.333", header.infos()),
            Ok(Field::new(Key::BaseQuality, Some(Value::Float(1.333))))
        );

        assert_eq!(
            parse("SOMATIC", header.infos()),
            Ok(Field::new(Key::IsSomaticMutation, Some(Value::Flag)))
        );

        assert_eq!(
            parse("EVENT=INV0", header.infos()),
            Ok(Field::new(
                Key::BreakendEventId,
                Some(Value::String(String::from("INV0")))
            ))
        );

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

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

        Ok(())
    }
}