rust-dnsbox/lib/dnsbox-base/src/ser/rrdata.rs

96 lines
2.4 KiB
Rust
Raw Normal View History

2017-12-21 12:32:14 +00:00
use bytes::Bytes;
2017-12-26 21:23:47 +00:00
use common_types::{Class, Type, classes};
2017-12-21 12:32:14 +00:00
use errors::*;
use ser::DnsPacketData;
2017-12-26 16:30:08 +00:00
use ser::text::{DnsTextData, DnsTextFormatter};
2017-12-26 21:23:47 +00:00
use std::borrow::Cow;
2017-12-26 16:30:08 +00:00
use std::fmt;
use std::io::Cursor;
pub trait RRDataPacket {
fn deserialize_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self>
where
Self: Sized,
;
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type;
2017-12-26 16:30:08 +00:00
}
impl<T: DnsPacketData + StaticRRData> RRDataPacket for T {
fn deserialize_rr_data(_ttl: u32, rr_class: Class, rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self> {
ensure!(rr_type == T::TYPE, "type mismatch");
if T::CLASS != classes::ANY {
2017-12-26 21:23:47 +00:00
ensure!(rr_class == T::CLASS, "class mismatch: got {}, need {}", rr_class, T::CLASS);
2017-12-26 16:30:08 +00:00
}
T::deserialize(data)
}
2017-12-26 21:23:47 +00:00
fn rr_type(&self) -> Type {
T::TYPE
}
2017-12-26 16:30:08 +00:00
}
2017-12-26 21:23:47 +00:00
pub trait RRDataText {
2017-12-26 16:30:08 +00:00
fn dns_parse_rr_data(ttl: u32, rr_class: Class, rr_type: Type, data: &mut &str) -> Result<Self>
where
Self: Sized,
;
2017-12-21 12:32:14 +00:00
2017-12-26 16:30:08 +00:00
// format might fail if there is no (known) text representation.
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result;
2017-12-26 21:23:47 +00:00
fn rr_type_txt(&self) -> Cow<'static, str> {
unimplemented!()
}
// (type, rrdata)
fn text(&self) -> Result<(String, String)> {
use std::fmt::Write;
let mut buf = String::new();
match write!(&mut buf, "{}", DnsDisplayRR(self)) {
Ok(()) => {
return Ok((self.rr_type_txt().into(), buf))
},
Err(_) => (),
}
buf.clear();
unimplemented!()
}
2017-12-21 12:32:14 +00:00
}
2017-12-26 21:23:47 +00:00
impl<T: DnsTextData + StaticRRData> RRDataText for T {
fn dns_parse_rr_data(_ttl: u32, rr_class: Class, rr_type: Type, data: &mut &str) -> Result<Self>
where
Self: Sized,
{
ensure!(rr_type == T::TYPE, "type mismatch");
if T::CLASS != classes::ANY {
ensure!(rr_class == T::CLASS, "class mismatch: got {}, need {}", rr_class, T::CLASS);
}
T::dns_parse(data)
}
fn dns_format_rr_data(&self, f: &mut DnsTextFormatter) -> fmt::Result {
self.dns_format(f)
2017-12-21 12:32:14 +00:00
}
}
2017-12-26 21:23:47 +00:00
#[derive(Debug)]
pub struct DnsDisplayRR<'a, T: RRDataText + ?Sized + 'a>(pub &'a T);
impl<'a, T: RRDataText + ?Sized + 'a> fmt::Display for DnsDisplayRR<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.dns_format_rr_data(&mut DnsTextFormatter::new(f))
}
}
pub trait RRData: RRDataPacket + RRDataText {
2017-12-21 12:32:14 +00:00
}
2017-12-16 20:58:18 +00:00
2017-12-21 12:32:14 +00:00
pub trait StaticRRData: RRData {
const TYPE: Type;
const NAME: &'static str;
2017-12-26 16:30:08 +00:00
// classes::ANY marks class independent types
const CLASS: Class;
2017-12-16 20:58:18 +00:00
}