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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Convenient downcasting from Packets to Packet Bodies.

use crate::packet::{
    Packet,
    Unknown,
    Signature,
    OnePassSig,
    Key,
    key,
    Marker,
    Trust,
    UserID,
    UserAttribute,
    Literal,
    CompressedData,
    PKESK,
    SKESK,
    SEIP,
    MDC,
    AED,
};

/// Convenient downcasting from Packets to Packet Bodies.
///
/// This trait offers functionality similar to [`std::any::Any`],
/// hence the name.
///
/// # Sealed trait
///
/// This trait is [sealed] and cannot be implemented for types outside
/// this crate.  Therefore it can be extended in a non-breaking way.
///
/// [sealed]: https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed
pub trait Any<T>: crate::seal::Sealed {
    /// Attempts to downcast to `T`, returning the packet if it fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sequoia_openpgp::packet::prelude::*;
    /// let p: Packet = Marker::default().into();
    /// let m: Marker = p.downcast().unwrap();
    /// # let _ = m;
    /// ```
    fn downcast(self) -> std::result::Result<T, Packet>;

    /// Attempts to downcast to `&T`, returning `None` if it fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sequoia_openpgp::packet::prelude::*;
    /// let p: Packet = Marker::default().into();
    /// let m: &Marker = p.downcast_ref().unwrap();
    /// # let _ = m;
    /// ```
    fn downcast_ref(&self) -> Option<&T>;

    /// Attempts to downcast to `&mut T`, returning `None` if it fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use sequoia_openpgp::packet::prelude::*;
    /// let mut p: Packet = Marker::default().into();
    /// let m: &mut Marker = p.downcast_mut().unwrap();
    /// # let _ = m;
    /// ```
    fn downcast_mut(&mut self) -> Option<&mut T>;
}

macro_rules! impl_downcast_for {
    ($typ: tt) => {
        impl_downcast_for!($typ => $typ);
    };
    ($($typ: tt)|* => $subtyp: ty) => {
        #[allow(deprecated)]
        impl Any<$subtyp> for Packet {
            fn downcast(self) -> std::result::Result<$subtyp, Packet> {
                match self {
                    $(Packet::$typ(v) => Ok(v.into()),)*
                    p => Err(p),
                }
            }

            fn downcast_ref(&self) -> Option<&$subtyp> {
                match self {
                    $(Packet::$typ(v) => Some(v.into()),)*
                    _ => None,
                }
            }

            fn downcast_mut(&mut self) -> Option<&mut $subtyp> {
                match self {
                    $(Packet::$typ(v) => Some(v.into()),)*
                    _ => None,
                }
            }
        }
    };
}

macro_rules! impl_downcasts {
    ($($typ:ident, )*) => {
        $(impl_downcast_for!($typ);)*

        /// Checks that all packet types have implementations of `Any`.
        ///
        /// Not visible outside this module, isn't supposed to be
        /// called, this is a compile-time check.
        #[allow(unused, deprecated)]
        fn check_exhaustion(p: Packet) {
            match p {
                $(Packet::$typ(_) => (),)*
                // The downcasts to Key<P, R> are handled below.
                Packet::PublicKey(_) => (),
                Packet::PublicSubkey(_) => (),
                Packet::SecretKey(_) => (),
                Packet::SecretSubkey(_) => (),
            }
        }
    }
}

impl_downcasts!(
    Unknown,
    Signature,
    OnePassSig,
    Marker,
    Trust,
    UserID,
    UserAttribute,
    Literal,
    CompressedData,
    PKESK,
    SKESK,
    SEIP,
    MDC,
    AED,
);

// We ow selectively implement downcasts for the key types that alias
// with the packet type.

// 1. PublicParts, any role.
impl_downcast_for!(PublicKey | SecretKey
                   => Key<key::PublicParts, key::PrimaryRole>);
impl_downcast_for!(PublicSubkey | SecretSubkey
                   => Key<key::PublicParts, key::SubordinateRole>);
impl_downcast_for!(PublicKey | PublicSubkey | SecretKey | SecretSubkey
                   => Key<key::PublicParts, key::UnspecifiedRole>);

// 2. SecretParts, any role.
impl_downcast_for!(SecretKey
                   => Key<key::SecretParts, key::PrimaryRole>);
impl_downcast_for!(SecretSubkey
                   => Key<key::SecretParts, key::SubordinateRole>);
impl_downcast_for!(SecretKey | SecretSubkey
                   => Key<key::SecretParts, key::UnspecifiedRole>);

// 3. UnspecifiedParts, any role.
impl_downcast_for!(PublicKey | SecretKey
                   => Key<key::UnspecifiedParts, key::PrimaryRole>);
impl_downcast_for!(PublicSubkey | SecretSubkey
                   => Key<key::UnspecifiedParts, key::SubordinateRole>);
impl_downcast_for!(PublicKey | PublicSubkey | SecretKey | SecretSubkey
                   => Key<key::UnspecifiedParts, key::UnspecifiedRole>);

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn downcast() {
        let p: Packet = Marker::default().into();
        let mut p = Any::<UserID>::downcast(p).unwrap_err();
        let r: Option<&UserID> = p.downcast_ref();
        assert!(r.is_none());
        let r: Option<&mut UserID> = p.downcast_mut();
        assert!(r.is_none());
        let _: &Marker = p.downcast_ref().unwrap();
        let _: &mut Marker = p.downcast_mut().unwrap();
        let _: Marker = p.downcast().unwrap();
    }
}