hickory_proto/rr/
rr_key.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 std::cmp::Ordering;
9
10use crate::rr::{LowerName, RecordType};
11
12/// Accessor key for RRSets in the Authority.
13#[derive(Eq, PartialEq, Debug, Hash, Clone)]
14pub struct RrKey {
15    /// Matches the name in the Record of this key
16    pub name: LowerName,
17    /// Matches the type of the Record of this key
18    pub record_type: RecordType,
19}
20
21impl RrKey {
22    /// Creates a new key to access the Authority.
23    ///
24    /// # Arguments
25    ///
26    /// * `name` - domain name to lookup.
27    /// * `record_type` - the `RecordType` to lookup.
28    ///
29    /// # Return value
30    ///
31    /// A new key to access the Authorities.
32    /// TODO: make all cloned params pass by value.
33    pub fn new(name: LowerName, record_type: RecordType) -> Self {
34        Self { name, record_type }
35    }
36
37    /// Returns the name of the key
38    pub fn name(&self) -> &LowerName {
39        &self.name
40    }
41}
42
43impl PartialOrd for RrKey {
44    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45        Some(self.cmp(other))
46    }
47}
48
49impl Ord for RrKey {
50    fn cmp(&self, other: &Self) -> Ordering {
51        let order = self.name.cmp(&other.name);
52        if order == Ordering::Equal {
53            self.record_type.cmp(&other.record_type)
54        } else {
55            order
56        }
57    }
58}