cuprate_types/rpc/
key_image_spent_status.rs
1#[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#[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 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 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}