1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#[doc(hidden)]
pub use super::tokenizers::WarningTokenizer as Tokenizer;
use crate::common::Uri;
use rsip_derives::TypedHeader;
use std::convert::{TryFrom, TryInto};
#[derive(TypedHeader, Eq, PartialEq, Clone, Debug)]
pub struct Warning {
pub code: u16,
pub uri: Uri,
pub text: String,
}
impl<'a> TryFrom<Tokenizer<'a>> for Warning {
type Error = crate::Error;
fn try_from(tokenizer: Tokenizer) -> Result<Self, Self::Error> {
Ok(Self {
code: tokenizer.code.parse::<u16>()?,
uri: Uri {
host_with_port: tokenizer.host.try_into()?,
..Default::default()
},
text: tokenizer.text.into(),
})
}
}
impl std::fmt::Display for Warning {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.text.starts_with('"') && self.text.ends_with('"') {
write!(f, "{} {} {}", self.code, self.uri, self.text)
} else {
write!(f, "{} {} \"{}\"", self.code, self.uri, self.text)
}
}
}