cuprate_types/rpc/
key_image_spent_status.rs

1//! TODO
2
3//---------------------------------------------------------------------------------------------------- Use
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7#[cfg(feature = "epee")]
8use cuprate_epee_encoding::{
9    error,
10    macros::bytes::{Buf, BufMut},
11    EpeeValue, Marker,
12};
13
14//---------------------------------------------------------------------------------------------------- KeyImageSpentStatus
15/// Used in RPC's `/is_key_image_spent`.
16#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
17#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
18#[cfg_attr(feature = "serde", serde(try_from = "u8", into = "u8"))]
19#[repr(u8)]
20pub enum KeyImageSpentStatus {
21    Unspent = 0,
22    SpentInBlockchain = 1,
23    SpentInPool = 2,
24}
25
26impl KeyImageSpentStatus {
27    /// Convert [`Self`] to a [`u8`].
28    ///
29    /// ```rust
30    /// use cuprate_types::rpc::KeyImageSpentStatus as K;
31    ///
32    /// assert_eq!(K::Unspent.to_u8(), 0);
33    /// assert_eq!(K::SpentInBlockchain.to_u8(), 1);
34    /// assert_eq!(K::SpentInPool.to_u8(), 2);
35    /// ```
36    pub const fn to_u8(self) -> u8 {
37        match self {
38            Self::Unspent => 0,
39            Self::SpentInBlockchain => 1,
40            Self::SpentInPool => 2,
41        }
42    }
43
44    /// Convert a [`u8`] to a [`Self`].
45    ///
46    /// # Errors
47    /// This returns [`None`] if `u > 2`.
48    ///
49    /// ```rust
50    /// use cuprate_types::rpc::KeyImageSpentStatus as K;
51    ///
52    /// assert_eq!(K::from_u8(0), Some(K::Unspent));
53    /// assert_eq!(K::from_u8(1), Some(K::SpentInBlockchain));
54    /// assert_eq!(K::from_u8(2), Some(K::SpentInPool));
55    /// assert_eq!(K::from_u8(3), None);
56    /// ```
57    pub const fn from_u8(u: u8) -> Option<Self> {
58        Some(match u {
59            0 => Self::Unspent,
60            1 => Self::SpentInBlockchain,
61            2 => Self::SpentInPool,
62            _ => return None,
63        })
64    }
65}
66
67impl From<KeyImageSpentStatus> for u8 {
68    fn from(value: KeyImageSpentStatus) -> Self {
69        value.to_u8()
70    }
71}
72
73impl TryFrom<u8> for KeyImageSpentStatus {
74    type Error = u8;
75    fn try_from(value: u8) -> Result<Self, Self::Error> {
76        Self::from_u8(value).ok_or(value)
77    }
78}
79
80#[cfg(feature = "epee")]
81impl EpeeValue for KeyImageSpentStatus {
82    const MARKER: Marker = u8::MARKER;
83
84    fn read<B: Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> {
85        let u = u8::read(r, marker)?;
86        Self::from_u8(u).ok_or(error::Error::Format("u8 was greater than 2"))
87    }
88
89    fn write<B: BufMut>(self, w: &mut B) -> error::Result<()> {
90        let u = self.to_u8();
91        u8::write(u, w)?;
92        Ok(())
93    }
94}