1 use std::fmt;
2 
3 #[cfg(any(test, feature = "quickcheck"))]
4 use quickcheck::{Arbitrary, Gen};
5 
6 use crate::packet;
7 use 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 4880] for details.
16 ///
17 ///   [Section 5.10 of RFC 4880]: https://tools.ietf.org/html/rfc4880#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)]
21 pub struct Trust {
22     pub(crate) common: packet::Common,
23     value: Vec<u8>,
24 }
25 
26 impl From<Vec<u8>> for Trust {
from(u: Vec<u8>) -> Self27     fn from(u: Vec<u8>) -> Self {
28         Trust {
29             common: Default::default(),
30             value: u,
31         }
32     }
33 }
34 
35 impl fmt::Display for Trust {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result36     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37         let trust = String::from_utf8_lossy(&self.value[..]);
38         write!(f, "{}", trust)
39     }
40 }
41 
42 impl fmt::Debug for Trust {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result43     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44         f.debug_struct("Trust")
45             .field("value", &crate::fmt::hex::encode(&self.value))
46             .finish()
47     }
48 }
49 
50 impl Trust {
51     /// Gets the trust packet's value.
value(&self) -> &[u8]52     pub fn value(&self) -> &[u8] {
53         self.value.as_slice()
54     }
55 }
56 
57 impl From<Trust> for Packet {
from(s: Trust) -> Self58     fn from(s: Trust) -> Self {
59         Packet::Trust(s)
60     }
61 }
62 
63 #[cfg(any(test, feature = "quickcheck"))]
64 impl Arbitrary for Trust {
arbitrary<G: Gen>(g: &mut G) -> Self65     fn arbitrary<G: Gen>(g: &mut G) -> Self {
66         Vec::<u8>::arbitrary(g).into()
67     }
68 }
69 
70 #[cfg(test)]
71 mod tests {
72     use super::*;
73     use crate::parse::Parse;
74     use crate::serialize::MarshalInto;
75 
76     quickcheck! {
77         fn roundtrip(p: Trust) -> bool {
78             let q = Trust::from_bytes(&p.to_vec().unwrap()).unwrap();
79             assert_eq!(p, q);
80             true
81         }
82     }
83 }
84