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
use crate::SharedString;
use smallvec::SmallVec;
use std::fmt;

/// A record of vendor-specific trace data across tracing systems.
#[derive(Debug, Clone)]
pub struct TraceState {
    /// Vendor-specific trace state.
    states: SmallVec<[(SharedString, String); 2]>,
}

impl TraceState {
    /// Creates a new instance.
    #[inline]
    pub fn new() -> Self {
        Self {
            states: SmallVec::new(),
        }
    }

    /// Constructs an instance from the `tracestate` header value.
    pub fn from_tracestate(tracestate: &str) -> Self {
        let states = tracestate
            .replace(' ', "")
            .split(',')
            .filter_map(|state| {
                state
                    .split_once('=')
                    .map(|(key, value)| (key.to_owned().into(), value.to_owned()))
            })
            .collect();
        Self { states }
    }

    /// Pushes a key-value pair into the list of states. If an entry with the key already exists,
    /// the value will be updated.
    #[inline]
    pub fn push(&mut self, key: impl Into<SharedString>, value: impl ToString) {
        let states = &mut self.states;
        let key = key.into();
        let value = value.to_string();
        if let Some(index) = states.iter().position(|(k, _)| k.as_ref() == key.as_ref()) {
            states[index] = (key, value);
        } else {
            states.push((key, value));
        }
    }
}

impl Default for TraceState {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for TraceState {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let output = self
            .states
            .iter()
            .map(|(key, value)| format!("{key}={value}"))
            .collect::<Vec<_>>()
            .join(",");
        write!(f, "{output}")
    }
}