summaryrefslogtreecommitdiffstats
path: root/openpgp/src/packet/unknown.rs
blob: 0819c375f960f8bf5349f07e88aaa8158d280547 (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
use packet::Tag;
use packet;
use Packet;

/// Holds an unknown packet.
///
/// This is used by the parser to hold packets that it doesn't know
/// how to process rather than abort.
///
/// This packet effectively holds a binary blob.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Unknown {
    /// CTB packet header fields.
    pub(crate) common: packet::Common,
    /// Packet tag.
    tag: Tag,
}

impl Unknown {
    /// Returns a new `Unknown` packet.
    pub fn new(tag: Tag) -> Self {
        Unknown {
            common: Default::default(),
            tag: tag,
        }
    }

    /// Gets the unknown packet's tag.
    pub fn tag(&self) -> Tag {
        self.tag
    }

    /// Sets the unknown packet's tag.
    pub fn set_tag(&mut self, tag: Tag) {
        self.tag = tag;
    }

    /// Sets the packet's contents.
    ///
    /// This is the raw packet content not include the CTB and length
    /// information, and not encoded using something like OpenPGP's
    /// partial body encoding.
    pub fn body(&self) -> Option<&[u8]> {
        self.common.body.as_ref().map(|b| b.as_slice())
    }

    /// Sets the packet's contents.
    ///
    /// This is the raw packet content not include the CTB and length
    /// information, and not encoded using something like OpenPGP's
    /// partial body encoding.
    pub fn set_body(&mut self, data: Vec<u8>) {
        self.common.body = Some(data);
    }

    /// Convert the `Unknown` struct to a `Packet`.
    pub fn to_packet(self) -> Packet {
        Packet::Unknown(self)
    }
}

impl From<Unknown> for Packet {
    fn from(s: Unknown) -> Self {
        s.to_packet()
    }
}