sip_codec/headers/
content_length.rs1use std::fmt::{Display, Error, Formatter};
2
3use http::header::HeaderValue;
4
5use crate::headers::ParseHeader;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct ContentLength(pub usize);
9
10impl ParseHeader for ContentLength {
11 fn header_name() -> &'static [&'static str] {
12 &["content-length", "l"]
13 }
14
15 fn decode<'a>(headers: impl IntoIterator<Item = &'a HeaderValue>) -> Option<Self> {
16 headers
17 .into_iter()
18 .next()
19 .and_then(|header| header.to_str().ok())
20 .and_then(|value| value.parse().ok())
21 .map(Self)
22 }
23}
24
25impl Into<usize> for ContentLength {
26 fn into(self) -> usize {
27 self.0
28 }
29}
30
31impl Display for ContentLength {
32 fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
33 write!(f, "{}", self.0)
34 }
35}