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
#[derive(Debug, Clone)]
enum TagName {
    Static(&'static str),
    Dynamic(String),
}

/// A CBOR tag.
#[derive(Debug, Clone)]
pub struct Tag {
    value: u64,
    name: Option<TagName>,
}

impl Tag {
    /// Creates a new CBOR tag with the given value and no name.
    pub const fn new(value: u64) -> Tag {
        Self { value, name: None }
    }

    /// Creates a new CBOR tag with the given value and associated name.
    pub fn new_with_name<N: Into<String>>(value: u64, name: N) -> Tag {
        Self { value, name: Some(TagName::Dynamic(name.into())) }
    }

    /// Creates a new CBOR tag at compile time with the given value and associated name.
    pub const fn new_with_static_name(value: u64, name: &'static str) -> Tag {
        Self { value, name: Some(TagName::Static(name)) }
    }

    /// Returns the wrapped tag value.
    pub fn value(&self) -> u64 {
        self.value
    }

    /// Returns the tag's associated name, if any.
    pub fn name(&self) -> Option<String> {
        match &self.name {
            Some(TagName::Static(name)) => Some(name.to_string()),
            Some(TagName::Dynamic(name)) => Some(name.clone()),
            None => None,
        }
    }
}

impl PartialEq for Tag {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl Eq for Tag { }

impl std::hash::Hash for Tag {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.value.hash(state);
    }
}

impl std::fmt::Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.name {
            Some(TagName::Static(name)) => write!(f, "{}", name),
            Some(TagName::Dynamic(name)) => write!(f, "{}", name),
            None => write!(f, "{}", self.value),
        }
    }
}

impl From<u64> for Tag {
    fn from(value: u64) -> Self {
        Tag::new(value)
    }
}

impl From<i32> for Tag {
    fn from(value: i32) -> Self {
        Tag::new(value as u64)
    }
}

impl From<usize> for Tag {
    fn from(value: usize) -> Self {
        Tag::new(value as u64)
    }
}