noodles_gff/lazy/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
mod array;

use self::array::Array;

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

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

pub(super) fn parse_value(s: &str) -> Value<'_> {
    if is_array(s) {
        Value::Array(Array::new(s))
    } else {
        Value::String(s)
    }
}

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

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

    #[test]
    fn test_parse_value() {
        assert_eq!(parse_value("ndls"), Value::String("ndls"));
        assert_eq!(parse_value("nd,ls"), Value::Array(Array::new("nd,ls")));
    }

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