Coverage Report

Created: 2024-02-20 21:15

/builds/xfbs/cindy/common/src/tag/value.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::tag::{ParseError, TagFilter};
2
use std::{
3
    fmt::{Display, Formatter, Result as FmtResult},
4
    str::FromStr,
5
};
6
7
421k
#[derive(
Clone7.58k
,
Debug343
,
P171k
artialE
q171k
, Eq,
P178k
artialOr
d178k
, Ord,
Hash0
)]
8
pub struct Tag(String, String);
9
10
impl Tag {
11
105k
    pub fn new(tag: String, value: String) -> Self {
12
105k
        Tag(tag, value)
13
105k
    }
14
15
42.5k
    pub fn name(&self) -> &str {
16
42.5k
        &self.0
17
42.5k
    }
18
19
28.4k
    pub fn value(&self) -> &str {
20
28.4k
        &self.1
21
28.4k
    }
22
23
4.77k
    pub fn filter(&self) -> TagFilter<'_> {
24
4.77k
        TagFilter::new(Some(&self.0), Some(&self.1))
25
4.77k
    }
26
27
0
    pub fn into_filter(self) -> TagFilter<'static> {
28
0
        TagFilter::new(Some(self.0), Some(self.1))
29
0
    }
30
}
31
32
impl FromStr for Tag {
33
    type Err = ParseError;
34
35
26
    fn from_str(input: &str) -> Result<Self, Self::Err> {
36
26
        match input.split_once(':') {
37
26
            Some((tag, value)) => Ok(Tag::new(tag.into(), value.into())),
38
0
            None => Err(ParseError::MissingColon),
39
        }
40
26
    }
41
}
42
43
impl Display for Tag {
44
22
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
45
22
        write!(f, "{}:{}", self.0, self.1)
46
22
    }
47
}
48
49
#[cfg(test)]
50
mod tests {
51
    use super::*;
52
53
1
    #[test]
54
1
    fn tag_methods() {
55
1
        let tag = Tag::new("name".into(), "value".into());
56
1
        assert_eq!(tag.name(), "name");
57
1
        assert_eq!(tag.value(), "value");
58
1
    }
59
60
1
    #[test]
61
1
    fn tag_from_str() {
62
1
        let tag: Tag = "name:value".parse().unwrap();
63
1
        assert_eq!(tag.name(), "name");
64
1
        assert_eq!(tag.value(), "value");
65
1
    }
66
}