rama_http_headers/common/
expect.rs1use std::fmt;
2
3use rama_http_types::{HeaderName, HeaderValue};
4
5use crate::util::IterExt;
6use crate::{Error, HeaderDecode, HeaderEncode, TypedHeader};
7
8#[derive(Clone, PartialEq)]
25pub struct Expect(());
26
27impl Expect {
28 pub const CONTINUE: Self = Self(());
30}
31
32impl TypedHeader for Expect {
33 fn name() -> &'static HeaderName {
34 &::rama_http_types::header::EXPECT
35 }
36}
37
38impl HeaderDecode for Expect {
39 fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
40 values
41 .just_one()
42 .and_then(|value| {
43 if value == "100-continue" {
44 Some(Self::CONTINUE)
45 } else {
46 None
47 }
48 })
49 .ok_or_else(Error::invalid)
50 }
51}
52
53impl HeaderEncode for Expect {
54 fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
55 values.extend(::std::iter::once(HeaderValue::from_static("100-continue")));
56 }
57}
58
59impl fmt::Debug for Expect {
60 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 f.debug_tuple("Expect").field(&"100-continue").finish()
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::super::test_decode;
68 use super::Expect;
69
70 #[test]
71 fn expect_continue() {
72 assert_eq!(
73 test_decode::<Expect>(&["100-continue"]),
74 Some(Expect::CONTINUE),
75 );
76 }
77
78 #[test]
79 fn expectation_failed() {
80 assert_eq!(test_decode::<Expect>(&["sandwich"]), None,);
81 }
82
83 #[test]
84 fn too_many_values() {
85 assert_eq!(
86 test_decode::<Expect>(&["100-continue", "100-continue"]),
87 None,
88 );
89 }
90}