zino_http/timing/
server_timing.rs

1use super::TimingMetric;
2use smallvec::SmallVec;
3use std::fmt;
4
5/// Performance metrics for the request-response cycle.
6/// See [the spec](https://w3c.github.io/server-timing).
7#[derive(Debug, Clone)]
8pub struct ServerTiming {
9    /// Server timing metrics.
10    metrics: SmallVec<[TimingMetric; 4]>,
11}
12
13impl ServerTiming {
14    /// Creates a new instance.
15    #[inline]
16    pub fn new() -> Self {
17        Self {
18            metrics: SmallVec::new(),
19        }
20    }
21
22    /// Pushes an entry into the list of metrics.
23    #[inline]
24    pub fn push(&mut self, metric: TimingMetric) {
25        self.metrics.push(metric);
26    }
27}
28
29impl Default for ServerTiming {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl fmt::Display for ServerTiming {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        let output = self
38            .metrics
39            .iter()
40            .map(|metric| metric.to_string())
41            .collect::<Vec<_>>()
42            .join(", ");
43        write!(f, "{output}")
44    }
45}