http_with_url/
version.rs

1//! HTTP version
2//!
3//! This module contains a definition of the `Version` type. The `Version`
4//! type is intended to be accessed through the root of the crate
5//! (`http::Version`) rather than this module.
6//!
7//! The `Version` type contains constants that represent the various versions
8//! of the HTTP protocol.
9//!
10//! # Examples
11//!
12//! ```
13//! use http::Version;
14//!
15//! let http11 = Version::HTTP_11;
16//! let http2 = Version::HTTP_2;
17//! assert!(http11 != http2);
18//!
19//! println!("{:?}", http2);
20//! ```
21
22use std::fmt;
23
24/// Represents a version of the HTTP spec.
25#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)]
26pub struct Version(Http);
27
28impl Version {
29    /// `HTTP/0.9`
30    pub const HTTP_09: Version = Version(Http::Http09);
31
32    /// `HTTP/1.0`
33    pub const HTTP_10: Version = Version(Http::Http10);
34
35    /// `HTTP/1.1`
36    pub const HTTP_11: Version = Version(Http::Http11);
37
38    /// `HTTP/2.0`
39    pub const HTTP_2: Version = Version(Http::H2);
40}
41
42#[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)]
43enum Http {
44    Http09,
45    Http10,
46    Http11,
47    H2,
48}
49
50impl Default for Version {
51    #[inline]
52    fn default() -> Version {
53        Version::HTTP_11
54    }
55}
56
57impl fmt::Debug for Version {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        use self::Http::*;
60
61        f.write_str(match self.0 {
62            Http09 => "HTTP/0.9",
63            Http10 => "HTTP/1.0",
64            Http11 => "HTTP/1.1",
65            H2     => "HTTP/2.0",
66        })
67    }
68}