1
//! [`IptLocalId`]
2

            
3
use crate::internal_prelude::*;
4

            
5
/// Persistent local identifier for an introduction point
6
///
7
/// Changes when the IPT relay changes, or the IPT key material changes.
8
/// (Different for different `.onion` services, obviously)
9
///
10
/// Is a randomly-generated byte string, currently 32 long.
11
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Deftly)]
12
#[derive_deftly(SerdeStringOrTransparent)]
13
#[cfg_attr(test, derive(derive_more::From))]
14
pub(crate) struct IptLocalId([u8; 32]);
15

            
16
impl_debug_hex!(IptLocalId.0);
17

            
18
impl Display for IptLocalId {
19
544
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20
17952
        for v in self.0 {
21
17408
            write!(f, "{v:02x}")?;
22
        }
23
544
        Ok(())
24
544
    }
25
}
26

            
27
/// Invalid [`IptLocalId`] - for example bad string representation
28
#[derive(Debug, Error, Clone, Eq, PartialEq)]
29
#[error("invalid IptLocalId")]
30
#[non_exhaustive]
31
pub(crate) struct InvalidIptLocalId {}
32

            
33
impl FromStr for IptLocalId {
34
    type Err = InvalidIptLocalId;
35
282
    fn from_str(s: &str) -> Result<Self, Self::Err> {
36
282
        let mut b = [0; 32];
37
282
        hex::decode_to_slice(s, &mut b).map_err(|_: hex::FromHexError| InvalidIptLocalId {})?;
38
282
        Ok(IptLocalId(b))
39
282
    }
40
}
41

            
42
impl KeySpecifierComponentViaDisplayFromStr for IptLocalId {}
43

            
44
impl IptLocalId {
45
    /// Return a fixed dummy `IptLocalId`, for testing etc.
46
    ///
47
    /// The id is made by repeating `which` 32 times.
48
    #[cfg(test)]
49
18
    pub(crate) fn dummy(which: u8) -> Self {
50
18
        IptLocalId([which; 32]) // I can't think of a good way not to specify 32 again here
51
18
    }
52
}
53

            
54
impl rand::distr::Distribution<IptLocalId> for rand::distr::StandardUniform {
55
28
    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> IptLocalId {
56
28
        IptLocalId(rng.random())
57
28
    }
58
}
59

            
60
#[cfg(test)]
61
pub(crate) mod test {
62
    // @@ begin test lint list maintained by maint/add_warning @@
63
    #![allow(clippy::bool_assert_comparison)]
64
    #![allow(clippy::clone_on_copy)]
65
    #![allow(clippy::dbg_macro)]
66
    #![allow(clippy::mixed_attributes_style)]
67
    #![allow(clippy::print_stderr)]
68
    #![allow(clippy::print_stdout)]
69
    #![allow(clippy::single_char_pattern)]
70
    #![allow(clippy::unwrap_used)]
71
    #![allow(clippy::unchecked_duration_subtraction)]
72
    #![allow(clippy::useless_vec)]
73
    #![allow(clippy::needless_pass_by_value)]
74
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
75
    use super::*;
76
    use itertools::{chain, Itertools};
77

            
78
    #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
79
    struct IptLidTest {
80
        lid: IptLocalId,
81
    }
82

            
83
    #[test]
84
    fn lid_serde() {
85
        let t = IptLidTest {
86
            lid: IptLocalId::dummy(7),
87
        };
88
        let json = serde_json::to_string(&t).unwrap();
89
        assert_eq!(
90
            json,
91
            // This also tests <IptLocalId as Display> since that's how we serialise it
92
            r#"{"lid":"0707070707070707070707070707070707070707070707070707070707070707"}"#,
93
        );
94
        let u: IptLidTest = serde_json::from_str(&json).unwrap();
95
        assert_eq!(t, u);
96

            
97
        let mpack = rmp_serde::to_vec_named(&t).unwrap();
98
        assert_eq!(
99
            mpack,
100
            chain!(&[129, 163], b"lid", &[220, 0, 32], &[0x07; 32],)
101
                .cloned()
102
                .collect_vec()
103
        );
104
        let u: IptLidTest = rmp_serde::from_slice(&mpack).unwrap();
105
        assert_eq!(t, u);
106
    }
107
}