summaryrefslogtreecommitdiff
path: root/src/tag.rs
blob: 14fd39ea9ac2ff542d38fbe5ef23bdf9f7171bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2017-2018 int08h LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use error::Error;

/// An unsigned 32-bit value (key) that maps to a byte-string (value).
#[derive(Debug, PartialEq, Eq, PartialOrd, Hash, Clone)]
pub enum Tag {
    // Enforcement of the "tags in strictly increasing order" rule is done using the
    // little-endian encoding of the ASCII tag value; e.g. 'SIG\x00' is 0x00474953 and
    // 'NONC' is 0x434e4f4e.
    //
    // Tags are written here in ascending order
    SIG,
    NONC,
    DELE,
    PATH,
    RADI,
    PUBK,
    MIDP,
    SREP,
    MINT,
    ROOT,
    CERT,
    MAXT,
    INDX,
    PAD,
}

impl Tag {
    /// Translates a tag into its on-the-wire representation
    pub fn wire_value(&self) -> &'static [u8] {
        match *self {
            Tag::CERT => b"CERT",
            Tag::DELE => b"DELE",
            Tag::INDX => b"INDX",
            Tag::MAXT => b"MAXT",
            Tag::MIDP => b"MIDP",
            Tag::MINT => b"MINT",
            Tag::NONC => b"NONC",
            Tag::PAD => b"PAD\xff",
            Tag::PATH => b"PATH",
            Tag::PUBK => b"PUBK",
            Tag::RADI => b"RADI",
            Tag::ROOT => b"ROOT",
            Tag::SIG => b"SIG\x00",
            Tag::SREP => b"SREP",
        }
    }

    /// Return the `Tag` corresponding to the on-the-wire representation in `bytes` or an
    /// `Error::InvalidTag` if `bytes` do not correspond to a valid tag.
    pub fn from_wire(bytes: &[u8]) -> Result<Self, Error> {
        match bytes {
            b"CERT" => Ok(Tag::CERT),
            b"DELE" => Ok(Tag::DELE),
            b"INDX" => Ok(Tag::INDX),
            b"MAXT" => Ok(Tag::MAXT),
            b"MIDP" => Ok(Tag::MIDP),
            b"MINT" => Ok(Tag::MINT),
            b"NONC" => Ok(Tag::NONC),
            b"PAD\xff" => Ok(Tag::PAD),
            b"PATH" => Ok(Tag::PATH),
            b"PUBK" => Ok(Tag::PUBK),
            b"RADI" => Ok(Tag::RADI),
            b"ROOT" => Ok(Tag::ROOT),
            b"SIG\x00" => Ok(Tag::SIG),
            b"SREP" => Ok(Tag::SREP),
            _ => Err(Error::InvalidTag(Box::from(bytes))),
        }
    }
}