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