cuprate_rpc_types/misc/
pool_info_extent.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//---------------------------------------------------------------------------------------------------- PoolInfoExtent
15#[doc = crate::macros::monero_definition_link!(
16    cc73fe71162d564ffda8e549b79a350bca53c454,
17    "rpc/core_rpc_server_commands_defs.h",
18    223..=228
19)]
20/// Used in [`crate::bin::GetBlocksResponse`].
21#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
22#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23#[repr(u8)]
24pub enum PoolInfoExtent {
25    #[default]
26    None = 0,
27    Incremental = 1,
28    Full = 2,
29}
30
31impl PoolInfoExtent {
32    /// Convert [`Self`] to a [`u8`].
33    ///
34    /// ```rust
35    /// use cuprate_rpc_types::misc::PoolInfoExtent as P;
36    ///
37    /// assert_eq!(P::None.to_u8(), 0);
38    /// assert_eq!(P::Incremental.to_u8(), 1);
39    /// assert_eq!(P::Full.to_u8(), 2);
40    /// ```
41    pub const fn to_u8(self) -> u8 {
42        match self {
43            Self::None => 0,
44            Self::Incremental => 1,
45            Self::Full => 2,
46        }
47    }
48
49    /// Convert a [`u8`] to a [`Self`].
50    ///
51    /// # Errors
52    /// This returns [`None`] if `u > 2`.
53    ///
54    /// ```rust
55    /// use cuprate_rpc_types::misc::PoolInfoExtent as P;
56    ///
57    /// assert_eq!(P::from_u8(0), Some(P::None));
58    /// assert_eq!(P::from_u8(1), Some(P::Incremental));
59    /// assert_eq!(P::from_u8(2), Some(P::Full));
60    /// assert_eq!(P::from_u8(3), None);
61    /// ```
62    pub const fn from_u8(u: u8) -> Option<Self> {
63        Some(match u {
64            0 => Self::None,
65            1 => Self::Incremental,
66            2 => Self::Full,
67            _ => return None,
68        })
69    }
70}
71
72#[cfg(feature = "epee")]
73impl EpeeValue for PoolInfoExtent {
74    const MARKER: Marker = <u8 as EpeeValue>::MARKER;
75
76    fn read<B: Buf>(r: &mut B, marker: &Marker) -> error::Result<Self> {
77        let u = u8::read(r, marker)?;
78        Self::from_u8(u).ok_or(error::Error::Format("u8 was greater than 2"))
79    }
80
81    fn write<B: BufMut>(self, w: &mut B) -> error::Result<()> {
82        let u = self.to_u8();
83        u8::write(u, w)?;
84        Ok(())
85    }
86}