Skip to content

Commit 65897de

Browse files
committed
push
1 parent 4351dc6 commit 65897de

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;
@@ -19,9 +40,30 @@ use crate::Headers;
1940
/// Timing](https://w3c.github.io/server-timing/#the-server-timing-header-field)
2041
/// header spec. Read more on
2142
/// [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing).
43+
///
44+
/// # Examples
45+
///
46+
/// ```
47+
/// # fn main() -> http_types::Result<()> {
48+
/// #
49+
/// use http_types::Response;
50+
/// use http_types::trace::{ServerTiming, Metric};
51+
///
52+
/// let mut timings = ServerTiming::new();
53+
/// timings.push(Metric::new("server".to_owned(), None, None)?);
54+
///
55+
/// let mut res = Response::new(200);
56+
/// timings.apply(&mut res);
57+
///
58+
/// let timings = ServerTiming::from_headers(res)?;
59+
/// let entry = timings.iter().next().unwrap();
60+
/// assert_eq!(entry.name(), "server");
61+
/// #
62+
/// # Ok(()) }
63+
/// ```
2264
#[derive(Debug)]
2365
pub struct ServerTiming {
24-
timings: Vec<Entry>,
66+
timings: Vec<Metric>,
2567
}
2668

2769
impl ServerTiming {
@@ -48,7 +90,7 @@ impl ServerTiming {
4890
}
4991

5092
/// Push an entry into the list of entries.
51-
pub fn push(&mut self, entry: Entry) {
93+
pub fn push(&mut self, entry: Metric) {
5294
self.timings.push(entry);
5395
}
5496

@@ -75,7 +117,7 @@ impl ServerTiming {
75117
}
76118

77119
impl IntoIterator for ServerTiming {
78-
type Item = Entry;
120+
type Item = Metric;
79121
type IntoIter = IntoIter;
80122

81123
#[inline]
@@ -85,7 +127,7 @@ impl IntoIterator for ServerTiming {
85127
}
86128

87129
impl<'a> IntoIterator for &'a ServerTiming {
88-
type Item = &'a Entry;
130+
type Item = &'a Metric;
89131
type IntoIter = Iter<'a>;
90132

91133
#[inline]
@@ -95,7 +137,7 @@ impl<'a> IntoIterator for &'a ServerTiming {
95137
}
96138

97139
impl<'a> IntoIterator for &'a mut ServerTiming {
98-
type Item = &'a mut Entry;
140+
type Item = &'a mut Metric;
99141
type IntoIter = IterMut<'a>;
100142

101143
#[inline]
@@ -107,11 +149,11 @@ impl<'a> IntoIterator for &'a mut ServerTiming {
107149
/// A borrowing iterator over entries in `ServerTiming`.
108150
#[derive(Debug)]
109151
pub struct IntoIter {
110-
inner: std::vec::IntoIter<Entry>,
152+
inner: std::vec::IntoIter<Metric>,
111153
}
112154

113155
impl Iterator for IntoIter {
114-
type Item = Entry;
156+
type Item = Metric;
115157

116158
fn next(&mut self) -> Option<Self::Item> {
117159
self.inner.next()
@@ -126,11 +168,11 @@ impl Iterator for IntoIter {
126168
/// A lending iterator over entries in `ServerTiming`.
127169
#[derive(Debug)]
128170
pub struct Iter<'a> {
129-
inner: slice::Iter<'a, Entry>,
171+
inner: slice::Iter<'a, Metric>,
130172
}
131173

132174
impl<'a> Iterator for Iter<'a> {
133-
type Item = &'a Entry;
175+
type Item = &'a Metric;
134176

135177
fn next(&mut self) -> Option<Self::Item> {
136178
self.inner.next()
@@ -145,11 +187,11 @@ impl<'a> Iterator for Iter<'a> {
145187
/// A mutable iterator over entries in `ServerTiming`.
146188
#[derive(Debug)]
147189
pub struct IterMut<'a> {
148-
inner: slice::IterMut<'a, Entry>,
190+
inner: slice::IterMut<'a, Metric>,
149191
}
150192

151193
impl<'a> Iterator for IterMut<'a> {
152-
type Item = &'a mut Entry;
194+
type Item = &'a mut Metric;
153195

154196
fn next(&mut self) -> Option<Self::Item> {
155197
self.inner.next()
@@ -169,7 +211,7 @@ mod test {
169211
#[test]
170212
fn smoke() -> crate::Result<()> {
171213
let mut timings = ServerTiming::new();
172-
timings.push(Entry::new("server".to_owned(), None, None)?);
214+
timings.push(Metric::new("server".to_owned(), None, None)?);
173215

174216
let mut headers = Headers::new();
175217
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)