hickory_proto/op/
lower_query.rs

1// Copyright 2015-2017 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use core::fmt::{self, Display};
9
10use crate::error::*;
11use crate::op::Query;
12use crate::rr::LowerName;
13use crate::rr::{DNSClass, RecordType};
14use crate::serialize::binary::*;
15
16/// Identical to [crate::op::Query], except that the Name is guaranteed to be in lower case form
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct LowerQuery {
19    name: LowerName,
20    original: Query,
21}
22
23impl LowerQuery {
24    /// Create a new query from name and type, class defaults to IN
25    pub fn query(query: Query) -> Self {
26        Self {
27            name: LowerName::new(query.name()),
28            original: query,
29        }
30    }
31
32    /// ```text
33    /// QNAME           a domain name represented as a sequence of labels, where
34    ///                 each label consists of a length octet followed by that
35    ///                 number of octets.  The domain name terminates with the
36    ///                 zero length octet for the null label of the root.  Note
37    ///                 that this field may be an odd number of octets; no
38    ///                 padding is used.
39    /// ```
40    pub fn name(&self) -> &LowerName {
41        &self.name
42    }
43
44    /// Returns the original with the `Name`s case preserved
45    pub fn original(&self) -> &Query {
46        &self.original
47    }
48
49    /// ```text
50    /// QTYPE           a two octet code which specifies the type of the query.
51    ///                 The values for this field include all codes valid for a
52    ///                 TYPE field, together with some more general codes which
53    ///                 can match more than one type of RR.
54    /// ```
55    pub fn query_type(&self) -> RecordType {
56        self.original.query_type()
57    }
58
59    /// ```text
60    /// QCLASS          a two octet code that specifies the class of the query.
61    ///                 For example, the QCLASS field is IN for the Internet.
62    /// ```
63    pub fn query_class(&self) -> DNSClass {
64        self.original.query_class()
65    }
66}
67
68impl From<Query> for LowerQuery {
69    fn from(query: Query) -> Self {
70        Self::query(query)
71    }
72}
73
74impl BinEncodable for LowerQuery {
75    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
76        self.original.emit(encoder)
77    }
78}
79
80impl<'r> BinDecodable<'r> for LowerQuery {
81    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self> {
82        let original = Query::read(decoder)?;
83        Ok(Self::query(original))
84    }
85}
86
87impl Display for LowerQuery {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
89        write!(
90            f,
91            "name: {} type: {} class: {}",
92            self.name,
93            self.original.query_type(),
94            self.original.query_class()
95        )
96    }
97}