Skip to content

Commit 68767e9

Browse files
committed
Merge branch 'server-timings' of https://github.com/http-rs/http-types into server-timings
2 parents f101cd4 + 65897de commit 68767e9

File tree

4 files changed

+72
-30
lines changed

4 files changed

+72
-30
lines changed

src/trace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ pub mod server_timing;
1313
mod trace_context;
1414

1515
#[doc(inline)]
16-
pub use server_timing::ServerTiming;
16+
pub use server_timing::{Metric, ServerTiming};
1717
pub use trace_context::TraceContext;

src/trace/server_timing/entry.rs renamed to src/trace/server_timing/metric.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::time::Duration;
22

33
use crate::headers::HeaderValue;
44

5-
/// An individual `ServerTiming` entry.
5+
/// An individual entry into `ServerTiming`.
66
//
77
// # Implementation notes
88
//
@@ -15,14 +15,14 @@ use crate::headers::HeaderValue;
1515
//
1616
// Multiple different entries per line are supported; separated with a `,`.
1717
#[derive(Debug, Clone, Eq, PartialEq)]
18-
pub struct Entry {
18+
pub struct Metric {
1919
pub(crate) name: String,
2020
pub(crate) dur: Option<Duration>,
2121
pub(crate) desc: Option<String>,
2222
}
2323

24-
impl Entry {
25-
/// Create a new instance of `Entry`.
24+
impl Metric {
25+
/// Create a new instance of `Metric`.
2626
///
2727
/// # Errors
2828
///
@@ -52,8 +52,8 @@ impl Entry {
5252
}
5353
}
5454

55-
impl From<Entry> for HeaderValue {
56-
fn from(entry: Entry) -> HeaderValue {
55+
impl From<Metric> for HeaderValue {
56+
fn from(entry: Metric) -> HeaderValue {
5757
let mut string = entry.name;
5858

5959
// Format a `Duration` into the format that the spec expects.
@@ -85,16 +85,16 @@ mod test {
8585
let dur = Duration::from_secs(1);
8686
let desc = String::from("A server timing");
8787

88-
let val: HeaderValue = Entry::new(name.clone(), None, None)?.into();
88+
let val: HeaderValue = Metric::new(name.clone(), None, None)?.into();
8989
assert_eq!(val, "Server");
9090

91-
let val: HeaderValue = Entry::new(name.clone(), Some(dur), None)?.into();
91+
let val: HeaderValue = Metric::new(name.clone(), Some(dur), None)?.into();
9292
assert_eq!(val, "Server; dur=1000");
9393

94-
let val: HeaderValue = Entry::new(name.clone(), None, Some(desc.clone()))?.into();
94+
let val: HeaderValue = Metric::new(name.clone(), None, Some(desc.clone()))?.into();
9595
assert_eq!(val, r#"Server; desc="A server timing""#);
9696

97-
let val: HeaderValue = Entry::new(name.clone(), Some(dur), Some(desc.clone()))?.into();
97+
let val: HeaderValue = Metric::new(name.clone(), Some(dur), Some(desc.clone()))?.into();
9898
assert_eq!(val, r#"Server; dur=1000; desc="A server timing""#);
9999
Ok(())
100100
}

src/trace/server_timing/mod.rs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
//! Metrics and descriptions for the given request-response cycle.
2-
3-
mod entry;
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! # fn main() -> http_types::Result<()> {
7+
//! #
8+
//! use http_types::Response;
9+
//! use http_types::trace::{ServerTiming, Metric};
10+
//!
11+
//! let mut timings = ServerTiming::new();
12+
//! timings.push(Metric::new("server".to_owned(), None, None)?);
13+
//!
14+
//! let mut res = Response::new(200);
15+
//! timings.apply(&mut res);
16+
//!
17+
//! let timings = ServerTiming::from_headers(res)?;
18+
//! let entry = timings.iter().next().unwrap();
19+
//! assert_eq!(entry.name(), "server");
20+
//! #
21+
//! # Ok(()) }
22+
//! ```
23+
24+
mod metric;
425
mod parse;
526

6-
pub use entry::Entry;
27+
pub use metric::Metric;
728
use parse::parse_header;
829

930
use std::convert::AsMut;
@@ -20,9 +41,30 @@ use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, SERVER_TI
2041
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
2142
/// header spec. Read more on
2243
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
44+
///
45+
/// # Examples
46+
///
47+
/// ```
48+
/// # fn main() -> http_types::Result<()> {
49+
/// #
50+
/// use http_types::Response;
51+
/// use http_types::trace::{ServerTiming, Metric};
52+
///
53+
/// let mut timings = ServerTiming::new();
54+
/// timings.push(Metric::new("server".to_owned(), None, None)?);
55+
///
56+
/// let mut res = Response::new(200);
57+
/// timings.apply(&mut res);
58+
///
59+
/// let timings = ServerTiming::from_headers(res)?;
60+
/// let entry = timings.iter().next().unwrap();
61+
/// assert_eq!(entry.name(), "server");
62+
/// #
63+
/// # Ok(()) }
64+
/// ```
2365
#[derive(Debug)]
2466
pub struct ServerTiming {
25-
timings: Vec<Entry>,
67+
timings: Vec<Metric>,
2668
}
2769

2870
impl ServerTiming {
@@ -68,7 +110,7 @@ impl ServerTiming {
68110
}
69111

70112
/// Push an entry into the list of entries.
71-
pub fn push(&mut self, entry: Entry) {
113+
pub fn push(&mut self, entry: Metric) {
72114
self.timings.push(entry);
73115
}
74116

@@ -95,7 +137,7 @@ impl ServerTiming {
95137
}
96138

97139
impl IntoIterator for ServerTiming {
98-
type Item = Entry;
140+
type Item = Metric;
99141
type IntoIter = IntoIter;
100142

101143
#[inline]
@@ -105,7 +147,7 @@ impl IntoIterator for ServerTiming {
105147
}
106148

107149
impl<'a> IntoIterator for &'a ServerTiming {
108-
type Item = &'a Entry;
150+
type Item = &'a Metric;
109151
type IntoIter = Iter<'a>;
110152

111153
// #[inline]serv
@@ -115,7 +157,7 @@ impl<'a> IntoIterator for &'a ServerTiming {
115157
}
116158

117159
impl<'a> IntoIterator for &'a mut ServerTiming {
118-
type Item = &'a mut Entry;
160+
type Item = &'a mut Metric;
119161
type IntoIter = IterMut<'a>;
120162

121163
#[inline]
@@ -127,11 +169,11 @@ impl<'a> IntoIterator for &'a mut ServerTiming {
127169
/// A borrowing iterator over entries in `ServerTiming`.
128170
#[derive(Debug)]
129171
pub struct IntoIter {
130-
inner: std::vec::IntoIter<Entry>,
172+
inner: std::vec::IntoIter<Metric>,
131173
}
132174

133175
impl Iterator for IntoIter {
134-
type Item = Entry;
176+
type Item = Metric;
135177

136178
fn next(&mut self) -> Option<Self::Item> {
137179
self.inner.next()
@@ -146,11 +188,11 @@ impl Iterator for IntoIter {
146188
/// A lending iterator over entries in `ServerTiming`.
147189
#[derive(Debug)]
148190
pub struct Iter<'a> {
149-
inner: slice::Iter<'a, Entry>,
191+
inner: slice::Iter<'a, Metric>,
150192
}
151193

152194
impl<'a> Iterator for Iter<'a> {
153-
type Item = &'a Entry;
195+
type Item = &'a Metric;
154196

155197
fn next(&mut self) -> Option<Self::Item> {
156198
self.inner.next()
@@ -165,11 +207,11 @@ impl<'a> Iterator for Iter<'a> {
165207
/// A mutable iterator over entries in `ServerTiming`.
166208
#[derive(Debug)]
167209
pub struct IterMut<'a> {
168-
inner: slice::IterMut<'a, Entry>,
210+
inner: slice::IterMut<'a, Metric>,
169211
}
170212

171213
impl<'a> Iterator for IterMut<'a> {
172-
type Item = &'a mut Entry;
214+
type Item = &'a mut Metric;
173215

174216
fn next(&mut self) -> Option<Self::Item> {
175217
self.inner.next()
@@ -197,7 +239,7 @@ mod test {
197239
#[test]
198240
fn smoke() -> crate::Result<()> {
199241
let mut timings = ServerTiming::new();
200-
timings.push(Entry::new("server".to_owned(), None, None)?);
242+
timings.push(Metric::new("server".to_owned(), None, None)?);
201243

202244
let mut headers = Headers::new();
203245
timings.apply(&mut headers);

src/trace/server_timing/parse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::time::Duration;
22

3-
use super::Entry;
3+
use super::Metric;
44
use crate::Status;
55
use crate::{ensure, format_err};
66

77
/// Parse multiple entries from a single header.
88
///
99
/// Each entry is comma-delimited.
10-
pub(super) fn parse_header(s: &str, entries: &mut Vec<Entry>) -> crate::Result<()> {
10+
pub(super) fn parse_header(s: &str, entries: &mut Vec<Metric>) -> crate::Result<()> {
1111
for part in s.trim().split(',') {
1212
let entry = parse_entry(part)?;
1313
entries.push(entry);
@@ -27,7 +27,7 @@ pub(super) fn parse_header(s: &str, entries: &mut Vec<Entry>) -> crate::Result<(
2727
/// ```
2828
//
2929
/// Source: https://w3c.github.io/server-timing/#the-server-timing-header-field
30-
fn parse_entry(s: &str) -> crate::Result<Entry> {
30+
fn parse_entry(s: &str) -> crate::Result<Metric> {
3131
let mut parts = s.trim().split(';');
3232

3333
// Get the name. This is non-optional.
@@ -86,7 +86,7 @@ fn parse_entry(s: &str) -> crate::Result<Entry> {
8686
}
8787
}
8888

89-
Ok(Entry {
89+
Ok(Metric {
9090
name: name.to_string(),
9191
dur,
9292
desc,

0 commit comments

Comments
 (0)