hyper_sync/header/common/
last_event_id.rs

1use std::fmt::{self, Display};
2
3use header::{self, Header, Raw};
4
5/// `Last-Event-ID` header, defined in
6/// [RFC3864](https://html.spec.whatwg.org/multipage/references.html#refsRFC3864)
7///
8/// The `Last-Event-ID` header contains information about
9/// the last event in an http interaction so that it's easier to
10/// track of event state. This is helpful when working
11/// with [Server-Sent-Events](http://www.html5rocks.com/en/tutorials/eventsource/basics/). If the connection were to be dropped, for example, it'd
12/// be useful to let the server know what the last event you
13/// received was.
14///
15/// The spec is a String with the id of the last event, it can be
16/// an empty string which acts a sort of "reset".
17///
18/// # Example
19/// ```
20/// use hyper_sync::header::{Headers, LastEventId};
21///
22/// let mut headers = Headers::new();
23/// headers.set(LastEventId("1".to_owned()));
24/// ```
25#[derive(Clone, Debug, PartialEq)]
26pub struct LastEventId(pub String);
27
28impl Header for LastEventId {
29    #[inline]
30    fn header_name() -> &'static str {
31        static NAME: &'static str = "Last-Event-ID";
32        NAME
33    }
34
35    #[inline]
36    fn parse_header(raw: &Raw) -> ::Result<Self> {
37        match raw.one() {
38            Some(line) if line.is_empty() => Ok(LastEventId("".to_owned())),
39            Some(line) => header::parsing::from_raw_str(line).map(LastEventId),
40            None => Err(::Error::Header),
41        }
42    }
43
44    #[inline]
45    fn fmt_header(&self, f: &mut header::Formatter) -> fmt::Result {
46        f.fmt_line(self)
47    }
48}
49
50impl Display for LastEventId {
51    #[inline]
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        Display::fmt(&self.0, f)
54    }
55}
56
57__hyper__deref!(LastEventId => String);
58
59__hyper__tm!(LastEventId, tests {
60    // Initial state
61    test_header!(test1, vec![b""]);
62    // Own testcase
63    test_header!(test2, vec![b"1"], Some(LastEventId("1".to_owned())));
64});