1 use std::fmt;
2 use std::str::FromStr;
3 
4 use util::HeaderValueString;
5 
6 /// `User-Agent` header, defined in
7 /// [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.5.3)
8 ///
9 /// The `User-Agent` header field contains information about the user
10 /// agent originating the request, which is often used by servers to help
11 /// identify the scope of reported interoperability problems, to work
12 /// around or tailor responses to avoid particular user agent
13 /// limitations, and for analytics regarding browser or operating system
14 /// use.  A user agent SHOULD send a User-Agent field in each request
15 /// unless specifically configured not to do so.
16 ///
17 /// # ABNF
18 ///
19 /// ```text
20 /// User-Agent = product *( RWS ( product / comment ) )
21 /// product         = token ["/" product-version]
22 /// product-version = token
23 /// ```
24 ///
25 /// # Example values
26 ///
27 /// * `CERN-LineMode/2.15 libwww/2.17b3`
28 /// * `Bunnies`
29 ///
30 /// # Notes
31 ///
32 /// * The parser does not split the value
33 ///
34 /// # Example
35 ///
36 /// ```
37 /// # extern crate headers;
38 /// use headers::UserAgent;
39 ///
40 /// let ua = UserAgent::from_static("hyper/0.12.2");
41 /// ```
42 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Header)]
43 pub struct UserAgent(HeaderValueString);
44 
45 impl UserAgent {
46     /// Create a `UserAgent` from a static string.
47     ///
48     /// # Panic
49     ///
50     /// Panics if the static string is not a legal header value.
from_static(src: &'static str) -> UserAgent51     pub fn from_static(src: &'static str) -> UserAgent {
52         UserAgent(HeaderValueString::from_static(src))
53     }
54 
55     /// View this `UserAgent` as a `&str`.
as_str(&self) -> &str56     pub fn as_str(&self) -> &str {
57         self.0.as_str()
58     }
59 }
60 
61 error_type!(InvalidUserAgent);
62 
63 impl FromStr for UserAgent {
64     type Err = InvalidUserAgent;
from_str(src: &str) -> Result<Self, Self::Err>65     fn from_str(src: &str) -> Result<Self, Self::Err> {
66         HeaderValueString::from_str(src)
67             .map(UserAgent)
68             .map_err(|_| InvalidUserAgent { _inner: () })
69     }
70 }
71 
72 impl fmt::Display for UserAgent {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result73     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74         fmt::Display::fmt(&self.0, f)
75     }
76 }
77 
78