domain_core/bits/opt/
rfc7314.rs

1//! EDNS Options from RFC 7314
2
3use bytes::BufMut;
4use ::bits::compose::Compose;
5use ::bits::message_builder::OptBuilder;
6use ::bits::parse::{ParseAll, Parser, ParseAllError, ShortBuf};
7use ::iana::OptionCode;
8use super::CodeOptData;
9
10
11//------------ Expire --------------------------------------------------------
12
13#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
14pub struct Expire(Option<u32>);
15
16impl Expire {
17
18    pub fn new(expire: Option<u32>) -> Self {
19        Expire(expire)
20    }
21
22    pub fn push(builder: &mut OptBuilder, expire: Option<u32>)
23                -> Result<(), ShortBuf> {
24        builder.push(&Self::new(expire))
25    }
26
27    pub fn expire(self) -> Option<u32> {
28        self.0
29    }
30}
31
32
33//--- ParseAll and Compose
34
35impl ParseAll for Expire {
36    type Err = ParseAllError;
37
38    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
39        if len == 0 {
40            Ok(Expire::new(None))
41        }
42        else {
43            u32::parse_all(parser, len).map(|res| Expire::new(Some(res)))
44        }
45    }
46}
47
48impl Compose for Expire {
49    fn compose_len(&self) -> usize {
50        match self.0 {
51            Some(_) => 4,
52            None => 0,
53        }
54    }
55
56    fn compose<B: BufMut>(&self, buf: &mut B) {
57        if let Some(value) = self.0 {
58            value.compose(buf)
59        }
60    }
61}
62
63
64//--- OptData
65
66impl CodeOptData for Expire {
67    const CODE: OptionCode = OptionCode::Expire;
68}
69