mco_http/header/common/
pragma.rs1use std::fmt;
2
3use crate::header::{Header, HeaderFormat, parsing};
4
5#[derive(Clone, PartialEq, Debug)]
33pub enum Pragma {
34 NoCache,
36 Ext(String),
38}
39
40impl Header for Pragma {
41 fn header_name() -> &'static str {
42 "Pragma"
43 }
44
45 fn parse_header(raw: &[Vec<u8>]) -> crate::Result<Pragma> {
46 parsing::from_one_raw_str(raw).and_then(|s: String| {
47 let slice = &s.to_ascii_lowercase()[..];
48 match slice {
49 "no-cache" => Ok(Pragma::NoCache),
50 _ => Ok(Pragma::Ext(s)),
51 }
52 })
53 }
54}
55
56impl HeaderFormat for Pragma {
57 fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
58 fmt::Display::fmt(self, f)
59 }
60}
61
62impl fmt::Display for Pragma {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 f.write_str(match *self {
65 Pragma::NoCache => "no-cache",
66 Pragma::Ext(ref string) => &string[..],
67 })
68 }
69}
70
71#[test]
72fn test_parse_header() {
73 let a: Pragma = Header::parse_header([b"no-cache".to_vec()].as_ref()).unwrap();
74 let b = Pragma::NoCache;
75 assert_eq!(a, b);
76 let c: Pragma = Header::parse_header([b"FoObar".to_vec()].as_ref()).unwrap();
77 let d = Pragma::Ext("FoObar".to_owned());
78 assert_eq!(c, d);
79 let e: crate::Result<Pragma> = Header::parse_header([b"".to_vec()].as_ref());
80 assert_eq!(e.ok(), None);
81}