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
//! Raw GFF record attributes.

pub mod field;

use std::{fmt, io, iter};

use self::field::{parse_field, Value};

/// Raw GFF record attributes.
pub struct Attributes<'a>(&'a str);

impl<'a> Attributes<'a> {
    pub(super) fn new(buf: &'a str) -> Self {
        Self(buf)
    }

    /// Returns whether there are any attributes.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the value of the given tag.
    pub fn get(&self, tag: &str) -> Option<io::Result<Value<'_>>> {
        for result in self.iter() {
            match result {
                Ok((t, value)) => {
                    if t == tag {
                        return Some(Ok(value));
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }

        None
    }

    /// Returns an iterator over all tag-value pairs.
    pub fn iter(&self) -> impl Iterator<Item = io::Result<(&str, Value<'_>)>> {
        let mut src = self.0;

        iter::from_fn(move || {
            if src.is_empty() {
                None
            } else {
                Some(parse_field(&mut src))
            }
        })
    }
}

impl<'a> AsRef<str> for Attributes<'a> {
    fn as_ref(&self) -> &str {
        self.0
    }
}

impl<'a> fmt::Debug for Attributes<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut formatter = f.debug_map();

        for result in self.iter() {
            let (tag, value) = result.map_err(|_| fmt::Error)?;
            formatter.entry(&tag, &value);
        }

        formatter.finish()
    }
}

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

    #[test]
    fn test_is_empty() {
        let attributes = Attributes::new("");
        assert!(attributes.is_empty());

        let attributes = Attributes::new("gene_id=ndls0;gene_name=gene0");
        assert!(!attributes.is_empty());
    }

    #[test]
    fn test_get() {
        let attributes = Attributes::new("gene_id=ndls0;gene_name=gene0");
        assert!(attributes.get("gene_name").is_some());
        assert!(attributes.get("comment").is_none());
    }

    #[test]
    fn test_iter() -> io::Result<()> {
        let attributes = Attributes::new("");
        assert!(attributes.iter().next().is_none());

        let attributes = Attributes::new("gene_id=ndls0;gene_name=gene0");
        let actual: Vec<_> = attributes.iter().collect::<Result<_, _>>()?;
        let expected = vec![
            ("gene_id", Value::String("ndls0")),
            ("gene_name", Value::String("gene0")),
        ];
        assert_eq!(actual, expected);

        Ok(())
    }
}