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 
22 use std::fmt;
23 
24 /// Represents a version of the HTTP spec.
25 #[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)]
26 pub struct Version(Http);
27 
28 impl 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     /// `HTTP/3.0`
42     pub const HTTP_3: Version = Version(Http::H3);
43 }
44 
45 #[derive(PartialEq, PartialOrd, Copy, Clone, Eq, Ord, Hash)]
46 enum Http {
47     Http09,
48     Http10,
49     Http11,
50     H2,
51     H3,
52     __NonExhaustive,
53 }
54 
55 impl Default for Version {
56     #[inline]
default() -> Version57     fn default() -> Version {
58         Version::HTTP_11
59     }
60 }
61 
62 impl fmt::Debug for Version {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result63     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64         use self::Http::*;
65 
66         f.write_str(match self.0 {
67             Http09 => "HTTP/0.9",
68             Http10 => "HTTP/1.0",
69             Http11 => "HTTP/1.1",
70             H2 => "HTTP/2.0",
71             H3 => "HTTP/3.0",
72             __NonExhaustive => unreachable!(),
73         })
74     }
75 }
76