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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::collections::HashMap;
use std::fmt;
use std::iter::FusedIterator;

/// Metadata for a metric.
///
/// Metrics can have arbitrary key-value pairs stored as metadata. This allows
/// for labelling them with whatever metadata you may find relevant.
#[derive(Clone)]
pub struct Metadata(Impl);

#[derive(Clone)]
enum Impl {
    Static(&'static phf::Map<&'static str, &'static str>),
    Dynamic(HashMap<String, String>),
}

impl Metadata {
    /// Create a new metadata map from a hashmap.
    pub fn new(map: HashMap<String, String>) -> Self {
        Self(Impl::Dynamic(map))
    }

    pub(crate) const fn default_const() -> Self {
        const EMPTY_MAP: phf::Map<&str, &str> = phf::Map::new();

        Self::new_static(&EMPTY_MAP)
    }

    pub(crate) const fn new_static(map: &'static phf::Map<&'static str, &'static str>) -> Self {
        Self(Impl::Static(map))
    }

    /// Indicates whether this metadata instance is empty.
    pub fn is_empty(&self) -> bool {
        match &self.0 {
            Impl::Static(map) => map.is_empty(),
            Impl::Dynamic(map) => map.is_empty(),
        }
    }

    /// Get the number of entries contained within this metadata instance.
    pub fn len(&self) -> usize {
        match &self.0 {
            Impl::Static(map) => map.len(),
            Impl::Dynamic(map) => map.len(),
        }
    }

    /// Determins if `key` is in `Metadata`.
    pub fn contains_key(&self, key: &str) -> bool {
        match &self.0 {
            Impl::Static(map) => map.contains_key(key),
            Impl::Dynamic(map) => map.contains_key(key),
        }
    }

    /// Get the value that `key` corresponds to.
    pub fn get(&self, key: &str) -> Option<&str> {
        match &self.0 {
            Impl::Static(map) => map.get(key).copied(),
            Impl::Dynamic(map) => map.get(key).map(|s| &**s),
        }
    }

    /// Return an iterator over the entries of this `Metadata`.
    pub fn iter(&self) -> MetadataIter {
        MetadataIter(match &self.0 {
            Impl::Static(map) => IterImpl::Static(map.entries()),
            Impl::Dynamic(map) => IterImpl::Dynamic(map.iter()),
        })
    }
}

impl From<HashMap<String, String>> for Metadata {
    fn from(value: HashMap<String, String>) -> Self {
        Self(Impl::Dynamic(value))
    }
}

impl Default for Metadata {
    fn default() -> Self {
        Self::default_const()
    }
}

impl fmt::Debug for Metadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            Impl::Static(map) => map.fmt(f),
            Impl::Dynamic(map) => map.fmt(f),
        }
    }
}

impl<'a> IntoIterator for &'a Metadata {
    type Item = (&'a str, &'a str);
    type IntoIter = MetadataIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator over the entries of a [`Metadata`].
///
/// See [`Metadata::iter`].
#[derive(Clone, Debug)]
pub struct MetadataIter<'a>(IterImpl<'a>);

impl<'a> MetadataIter<'a> {
    fn map_entries((key, value): (&&'a str, &&'a str)) -> (&'a str, &'a str) {
        (*key, *value)
    }

    fn map_iter((key, value): (&'a String, &'a String)) -> (&'a str, &'a str) {
        (&**key, &**value)
    }
}

#[derive(Clone, Debug)]
enum IterImpl<'a> {
    Static(phf::map::Entries<'a, &'static str, &'static str>),
    Dynamic(std::collections::hash_map::Iter<'a, String, String>),
}

impl<'a> Iterator for MetadataIter<'a> {
    type Item = (&'a str, &'a str);

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            IterImpl::Static(iter) => iter.next().map(Self::map_entries),
            IterImpl::Dynamic(iter) => iter.next().map(Self::map_iter),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            IterImpl::Static(iter) => iter.size_hint(),
            IterImpl::Dynamic(iter) => iter.size_hint(),
        }
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        match &mut self.0 {
            IterImpl::Static(iter) => iter.nth(n).map(Self::map_entries),
            IterImpl::Dynamic(iter) => iter.nth(n).map(Self::map_iter),
        }
    }

    fn fold<B, F>(self, init: B, mut f: F) -> B
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> B,
    {
        match self.0 {
            IterImpl::Static(iter) => iter.fold(init, move |acc, (k, v)| f(acc, (*k, *v))),
            IterImpl::Dynamic(iter) => iter.fold(init, move |acc, (k, v)| f(acc, (&**k, &**v))),
        }
    }

    fn count(self) -> usize
    where
        Self: Sized,
    {
        self.len()
    }
}

impl<'a> ExactSizeIterator for MetadataIter<'a> {}

impl<'a> FusedIterator for MetadataIter<'a> {}