sequoia_openpgp/packet/
trust.rs

1use std::fmt;
2
3#[cfg(test)]
4use quickcheck::{Arbitrary, Gen};
5
6use crate::packet;
7use crate::Packet;
8
9/// Holds a Trust packet.
10///
11/// Trust packets are used to hold implementation specific information
12/// in an implementation-defined format.  Trust packets are normally
13/// not exported.
14///
15/// See [Section 5.10 of RFC 9580] for details.
16///
17///   [Section 5.10 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.10
18// IMPORTANT: If you add fields to this struct, you need to explicitly
19// IMPORTANT: implement PartialEq, Eq, and Hash.
20#[derive(Clone, PartialEq, Eq, Hash)]
21pub struct Trust {
22    pub(crate) common: packet::Common,
23    value: Vec<u8>,
24}
25
26assert_send_and_sync!(Trust);
27
28impl From<Vec<u8>> for Trust {
29    fn from(u: Vec<u8>) -> Self {
30        Trust {
31            common: Default::default(),
32            value: u,
33        }
34    }
35}
36
37impl fmt::Display for Trust {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        let trust = String::from_utf8_lossy(&self.value[..]);
40        write!(f, "{}", trust)
41    }
42}
43
44impl fmt::Debug for Trust {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        f.debug_struct("Trust")
47            .field("value", &crate::fmt::hex::encode(&self.value))
48            .finish()
49    }
50}
51
52impl Trust {
53    /// Gets the trust packet's value.
54    pub fn value(&self) -> &[u8] {
55        self.value.as_slice()
56    }
57}
58
59impl From<Trust> for Packet {
60    fn from(s: Trust) -> Self {
61        Packet::Trust(s)
62    }
63}
64
65#[cfg(test)]
66impl Arbitrary for Trust {
67    fn arbitrary(g: &mut Gen) -> Self {
68        Vec::<u8>::arbitrary(g).into()
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    use crate::parse::Parse;
76    use crate::serialize::MarshalInto;
77
78    quickcheck! {
79        fn roundtrip(p: Trust) -> bool {
80            let q = Trust::from_bytes(&p.to_vec().unwrap()).unwrap();
81            assert_eq!(p, q);
82            true
83        }
84    }
85}