noodles_gff/record/attributes/field/
value.rs

1mod array;
2
3use std::borrow::Cow;
4
5use bstr::BStr;
6
7use self::array::Array;
8use super::percent_decode;
9
10/// A GFF record attributes field value.
11#[derive(Debug, Eq, PartialEq)]
12pub enum Value<'a> {
13    /// A string.
14    String(Cow<'a, BStr>),
15    /// An array.
16    Array(Array<'a>),
17}
18
19impl AsRef<BStr> for Value<'_> {
20    fn as_ref(&self) -> &BStr {
21        match self {
22            Value::String(s) => s,
23            Value::Array(array) => array.as_ref(),
24        }
25    }
26}
27
28impl<'a> From<Value<'a>> for crate::feature::record::attributes::field::Value<'a> {
29    fn from(value: Value<'a>) -> Self {
30        match value {
31            Value::String(s) => Self::String(s),
32            Value::Array(array) => Self::Array(Box::new(array)),
33        }
34    }
35}
36
37pub(crate) fn parse_value(src: &[u8]) -> Value<'_> {
38    if is_array(src) {
39        Value::Array(Array::new(src))
40    } else {
41        Value::String(percent_decode(src))
42    }
43}
44
45fn is_array(src: &[u8]) -> bool {
46    const SEPARATOR: u8 = b',';
47    src.contains(&SEPARATOR)
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_parse_value() {
56        assert_eq!(
57            parse_value(b"ndls"),
58            Value::String(Cow::from(BStr::new("ndls")))
59        );
60        assert_eq!(parse_value(b"8,13"), Value::Array(Array::new(b"8,13")));
61    }
62
63    #[test]
64    fn test_is_array() {
65        assert!(is_array(b"8,13"));
66        assert!(!is_array(b"ndls"));
67    }
68}