domain_core/rdata/
rfc2782.rs

1//! Record data from [RFC 2782].
2//!
3//! This RFC defines the Srv record type.
4//!
5//! [RFC 2782]: https://tools.ietf.org/html/rfc2782
6
7use std::fmt;
8use bytes::BufMut;
9use ::bits::compose::{Compose, Compress, Compressor};
10use ::bits::parse::{Parse, ParseAll, Parser, ParseOpenError, ShortBuf};
11use ::bits::rdata::RtypeRecordData;
12use ::master::scan::{CharSource, Scan, Scanner, ScanError};
13use ::iana::Rtype;
14
15
16//------------ Srv ---------------------------------------------------------
17
18#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
19pub struct Srv<N> {
20    priority: u16,
21    weight: u16,
22    port: u16,
23    target: N
24}
25
26impl<N> Srv<N> {
27    pub const RTYPE: Rtype = Rtype::Srv;
28
29    pub fn new(priority: u16, weight: u16, port: u16, target: N) -> Self {
30        Srv { priority, weight, port, target }
31    }
32
33    pub fn priority(&self) -> u16 { self.priority }
34    pub fn weight(&self) -> u16 { self.weight }
35    pub fn port(&self) -> u16 { self.port }
36    pub fn target(&self) -> &N { &self.target }
37}
38
39
40//--- Parse, ParseAll, Compose and Compress
41
42impl<N: Parse> Parse for Srv<N> {
43    type Err = <N as Parse>::Err;
44
45    fn parse(parser: &mut Parser) -> Result<Self, Self::Err> {
46        Ok(Self::new(u16::parse(parser)?, u16::parse(parser)?,
47                     u16::parse(parser)?, N::parse(parser)?))
48    }
49
50    fn skip(parser: &mut Parser) -> Result<(), Self::Err> {
51        u16::skip(parser)?;
52        u16::skip(parser)?;
53        u16::skip(parser)?;
54        N::skip(parser)
55    }
56}
57
58impl<N: ParseAll> ParseAll for Srv<N> where N::Err: From<ParseOpenError> {
59    type Err = N::Err;
60
61    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
62        if len < 7 {
63            return Err(ParseOpenError::ShortField.into())
64        }
65        Ok(Self::new(u16::parse(parser)?, u16::parse(parser)?,
66                     u16::parse(parser)?, N::parse_all(parser, len - 6)?))
67    }
68}
69
70impl<N: Compose> Compose for Srv<N> {
71    fn compose_len(&self) -> usize {
72        self.target.compose_len() + 6
73    }
74
75    fn compose<B: BufMut>(&self, buf: &mut B) {
76        self.priority.compose(buf);
77        self.weight.compose(buf);
78        self.port.compose(buf);
79        self.target.compose(buf);
80    }
81}
82
83impl<N: Compress> Compress for Srv<N> {
84    fn compress(&self, buf: &mut Compressor) -> Result<(), ShortBuf> {
85        buf.compose(&self.priority)?;
86        buf.compose(&self.weight)?;
87        buf.compose(&self.port)?;
88        self.target.compress(buf)
89    }
90}
91
92
93//--- RtypeRecordData
94
95impl<N> RtypeRecordData for Srv<N> {
96    const RTYPE: Rtype = Rtype::Srv;
97}
98
99
100//--- Scan and Display
101
102impl<N: Scan> Scan for Srv<N> {
103    fn scan<C: CharSource>(scanner: &mut Scanner<C>)
104                           -> Result<Self, ScanError> {
105        Ok(Self::new(u16::scan(scanner)?, u16::scan(scanner)?,
106                     u16::scan(scanner)?, N::scan(scanner)?))
107    }
108}
109
110impl<N: fmt::Display> fmt::Display for Srv<N> {
111    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112        write!(f, "{} {} {} {}", self.priority, self.weight, self.port,
113               self.target)
114    }
115}
116
117
118//------------ parsed --------------------------------------------------------
119
120pub mod parsed {
121    use ::bits::name::ParsedDname;
122
123    pub type Srv = super::Srv<ParsedDname>;
124}
125