cuprate_types/
address_type.rs

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
//! Types of network addresses; used in P2P.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "epee")]
use cuprate_epee_encoding::{
    error,
    macros::bytes::{Buf, BufMut},
    EpeeValue, Marker,
};

use strum::{
    AsRefStr, Display, EnumCount, EnumIs, EnumString, FromRepr, IntoStaticStr, VariantArray,
};

/// An enumeration of address types.
///
/// Used in `cuprate_p2p` and `cuprate_types`
///
/// Original definition:
/// - <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/epee/include/net/enums.h/#L49>
///
/// # Serde
/// This type's `serde` implementation (de)serializes from a [`u8`].
///
/// ```rust
/// use cuprate_types::AddressType as A;
/// use serde_json::{to_string, from_str};
///
/// assert_eq!(from_str::<A>(&"0").unwrap(), A::Invalid);
/// assert_eq!(from_str::<A>(&"1").unwrap(), A::Ipv4);
/// assert_eq!(from_str::<A>(&"2").unwrap(), A::Ipv6);
/// assert_eq!(from_str::<A>(&"3").unwrap(), A::I2p);
/// assert_eq!(from_str::<A>(&"4").unwrap(), A::Tor);
///
/// assert_eq!(to_string(&A::Invalid).unwrap(), "0");
/// assert_eq!(to_string(&A::Ipv4).unwrap(), "1");
/// assert_eq!(to_string(&A::Ipv6).unwrap(), "2");
/// assert_eq!(to_string(&A::I2p).unwrap(), "3");
/// assert_eq!(to_string(&A::Tor).unwrap(), "4");
/// ```
#[derive(
    Copy,
    Clone,
    Default,
    Debug,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
    AsRefStr,
    Display,
    EnumCount,
    EnumIs,
    EnumString,
    FromRepr,
    IntoStaticStr,
    VariantArray,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged, try_from = "u8", into = "u8"))]
#[repr(u8)]
pub enum AddressType {
    #[default]
    Invalid,
    Ipv4,
    Ipv6,
    I2p,
    Tor,
}

impl AddressType {
    /// Convert [`Self`] to a [`u8`].
    ///
    /// ```rust
    /// use cuprate_types::AddressType as A;
    ///
    /// assert_eq!(A::Invalid.to_u8(), 0);
    /// assert_eq!(A::Ipv4.to_u8(), 1);
    /// assert_eq!(A::Ipv6.to_u8(), 2);
    /// assert_eq!(A::I2p.to_u8(), 3);
    /// assert_eq!(A::Tor.to_u8(), 4);
    /// ```
    pub const fn to_u8(self) -> u8 {
        self as u8
    }

    /// Convert a [`u8`] to a [`Self`].
    ///
    /// # Errors
    /// This returns [`None`] if `u > 4`.
    ///
    /// ```rust
    /// use cuprate_types::AddressType as A;
    ///
    /// assert_eq!(A::from_u8(0), Some(A::Invalid));
    /// assert_eq!(A::from_u8(1), Some(A::Ipv4));
    /// assert_eq!(A::from_u8(2), Some(A::Ipv6));
    /// assert_eq!(A::from_u8(3), Some(A::I2p));
    /// assert_eq!(A::from_u8(4), Some(A::Tor));
    /// assert_eq!(A::from_u8(5), None);
    /// ```
    pub const fn from_u8(u: u8) -> Option<Self> {
        Some(match u {
            0 => Self::Invalid,
            1 => Self::Ipv4,
            2 => Self::Ipv6,
            3 => Self::I2p,
            4 => Self::Tor,
            _ => return None,
        })
    }
}

impl From<AddressType> for u8 {
    fn from(value: AddressType) -> Self {
        value.to_u8()
    }
}

impl TryFrom<u8> for AddressType {
    type Error = u8;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match Self::from_u8(value) {
            Some(s) => Ok(s),
            None => Err(value),
        }
    }
}

#[cfg(feature = "epee")]
impl EpeeValue for AddressType {
    const MARKER: Marker = u8::MARKER;

    fn read<B: Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> {
        let u = u8::read(r, marker)?;
        Self::from_u8(u).ok_or(error::Error::Format("u8 was greater than 4"))
    }

    fn write<B: BufMut>(self, w: &mut B) -> error::Result<()> {
        let u = self.to_u8();
        u8::write(u, w)?;
        Ok(())
    }
}