use std::fmt; use bytes::BufMut; use ::iana::{Class, Rtype}; use super::compose::{Compose, Compress, Compressor}; use super::name::ToDname; use super::parse::{Parse, Parser, ShortBuf}; //------------ Question ------------------------------------------------------ #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Question { qname: N, qtype: Rtype, qclass: Class, } /// # Creation and Conversion /// impl Question { pub fn new(qname: N, qtype: Rtype, qclass: Class) -> Self { Question { qname, qtype, qclass } } pub fn new_in(qname: N, qtype: Rtype) -> Self { Question { qname, qtype, qclass: Class::In } } } /// # Field Access /// impl Question { pub fn qname(&self) -> &N { &self.qname } pub fn qtype(&self) -> Rtype { self.qtype } pub fn qclass(&self) -> Class { self.qclass } } //--- From impl From<(N, Rtype, Class)> for Question { fn from((name, rtype, class): (N, Rtype, Class)) -> Self { Question::new(name, rtype, class) } } impl From<(N, Rtype)> for Question { fn from((name, rtype): (N, Rtype)) -> Self { Question::new(name, rtype, Class::In) } } //--- Parse, Compose, and Compress impl Parse for Question { type Err = ::Err; fn parse(parser: &mut Parser) -> Result { Ok(Question::new( N::parse(parser)?, Rtype::parse(parser)?, Class::parse(parser)? )) } } impl Compose for Question { fn compose_len(&self) -> usize { self.qname.compose_len() + self.qtype.compose_len() + self.qclass.compose_len() } fn compose(&self, buf: &mut B) { self.qname.compose(buf); self.qtype.compose(buf); self.qclass.compose(buf); } } impl Compress for Question { fn compress(&self, buf: &mut Compressor) -> Result<(), ShortBuf> { self.qname.compress(buf)?; buf.compose(&self.qtype)?; buf.compose(&self.qclass) } } //--- Display impl fmt::Display for Question { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}.\t{}\t{}", self.qname, self.qtype, self.qclass) } }