noodles_gff/record/attributes/field/
value.rs

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
mod array;

use std::{borrow::Cow, io};

use self::array::Array;
use super::percent_decode;

/// A raw GFF record attributes field value.
#[derive(Debug, Eq, PartialEq)]
pub enum Value<'a> {
    /// A string.
    String(Cow<'a, str>),
    /// An array.
    Array(Array<'a>),
}

impl AsRef<str> for Value<'_> {
    fn as_ref(&self) -> &str {
        match self {
            Value::String(s) => s,
            Value::Array(array) => array.as_ref(),
        }
    }
}

pub(super) fn parse_value(s: &str) -> io::Result<Value<'_>> {
    if is_array(s) {
        Ok(Value::Array(Array::new(s)))
    } else {
        percent_decode(s)
            .map(Value::String)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
    }
}

fn is_array(s: &str) -> bool {
    const SEPARATOR: char = ',';
    s.contains(SEPARATOR)
}

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

    #[test]
    fn test_parse_value() -> io::Result<()> {
        assert_eq!(parse_value("ndls")?, Value::String(Cow::from("ndls")));
        assert_eq!(parse_value("8,13")?, Value::Array(Array::new("8,13")));
        Ok(())
    }

    #[test]
    fn test_is_array() {
        assert!(is_array("8,13"));
        assert!(!is_array("ndls"));
    }
}