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

55 lines
1.4 KiB
Rust
Raw Normal View History

2017-12-21 12:32:14 +00:00
use bytes::Bytes;
2017-12-26 16:30:08 +00:00
use common_types::{Class, Type};
use common_types::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};
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,
;
}
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 {
ensure!(rr_class == T::CLASS, "class mismatch");
}
T::deserialize(data)
}
}
pub trait RRDataText: Sized {
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-21 12:32:14 +00:00
}
2017-12-26 16:30:08 +00:00
/*
impl<T: DnsTextData> RRDataText for T {
fn deserialize_rr_data(_ttl: u32, _rr_class: Class, _rr_type: Type, data: &mut Cursor<Bytes>) -> Result<Self> {
2017-12-21 12:32:14 +00:00
T::deserialize(data)
}
}
2017-12-26 16:30:08 +00:00
*/
2017-12-21 12:32:14 +00:00
2017-12-26 16:30:08 +00:00
pub trait RRData: RRDataPacket + DnsTextData {
2017-12-21 12:32:14 +00:00
fn rr_type(&self) -> Type;
}
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
}