rpc: use feature flags in misc types

This commit is contained in:
hinto.janai 2024-07-01 20:53:16 -04:00
parent a85d4b845a
commit 1a90c75f42
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 35 additions and 26 deletions

View file

@ -12,8 +12,10 @@
//! - <https://github.com/monero-project/monero/pull/8843> //! - <https://github.com/monero-project/monero/pull/8843>
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "epee")]
use cuprate_epee_encoding::epee_object; use cuprate_epee_encoding::epee_object;
use crate::Status; use crate::Status;
@ -36,25 +38,27 @@ macro_rules! monero_rpc_base_link {
/// The most common base for responses (nothing). /// The most common base for responses (nothing).
/// ///
#[doc = monero_rpc_base_link!(95..=99)] #[doc = monero_rpc_base_link!(95..=99)]
#[derive( #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
)]
pub struct EmptyRequestBase; pub struct EmptyRequestBase;
cuprate_epee_encoding::epee_object! { #[cfg(feature = "epee")]
epee_object! {
EmptyRequestBase, EmptyRequestBase,
} }
/// A base for RPC request types that support RPC payment. /// A base for RPC request types that support RPC payment.
/// ///
#[doc = monero_rpc_base_link!(114..=122)] #[doc = monero_rpc_base_link!(114..=122)]
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccessRequestBase { pub struct AccessRequestBase {
/// The RPC payment client. /// The RPC payment client.
pub client: String, pub client: String,
} }
cuprate_epee_encoding::epee_object! { #[cfg(feature = "epee")]
epee_object! {
AccessRequestBase, AccessRequestBase,
client: String, client: String,
} }
@ -65,21 +69,20 @@ cuprate_epee_encoding::epee_object! {
/// This is for response types that do not contain /// This is for response types that do not contain
/// any extra fields, e.g. TODO. /// any extra fields, e.g. TODO.
// [`CalcPowResponse`](crate::json::CalcPowResponse). // [`CalcPowResponse`](crate::json::CalcPowResponse).
#[derive( #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
)]
pub struct EmptyResponseBase; pub struct EmptyResponseBase;
cuprate_epee_encoding::epee_object! { #[cfg(feature = "epee")]
epee_object! {
EmptyResponseBase, EmptyResponseBase,
} }
/// The most common base for responses. /// The most common base for responses.
/// ///
#[doc = monero_rpc_base_link!(101..=112)] #[doc = monero_rpc_base_link!(101..=112)]
#[derive( #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
)]
pub struct ResponseBase { pub struct ResponseBase {
/// General RPC error code. [`Status::Ok`] means everything looks good. /// General RPC error code. [`Status::Ok`] means everything looks good.
pub status: Status, pub status: Status,
@ -89,6 +92,7 @@ pub struct ResponseBase {
pub untrusted: bool, pub untrusted: bool,
} }
#[cfg(feature = "epee")]
epee_object! { epee_object! {
ResponseBase, ResponseBase,
status: Status, status: Status,
@ -98,10 +102,11 @@ epee_object! {
/// A base for RPC response types that support RPC payment. /// A base for RPC response types that support RPC payment.
/// ///
#[doc = monero_rpc_base_link!(124..=136)] #[doc = monero_rpc_base_link!(124..=136)]
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccessResponseBase { pub struct AccessResponseBase {
/// A flattened [`ResponseBase`]. /// A flattened [`ResponseBase`].
#[serde(flatten)] #[cfg_attr(feature = "serde", serde(flatten))]
pub response_base: ResponseBase, pub response_base: ResponseBase,
/// If payment for RPC is enabled, the number of credits /// If payment for RPC is enabled, the number of credits
/// available to the requesting client. Otherwise, `0`. /// available to the requesting client. Otherwise, `0`.
@ -111,6 +116,7 @@ pub struct AccessResponseBase {
pub top_hash: String, pub top_hash: String,
} }
#[cfg(feature = "epee")]
epee_object! { epee_object! {
AccessResponseBase, AccessResponseBase,
credits: u64, credits: u64,

View file

@ -113,11 +113,11 @@ define_request_and_response! {
OnGetBlockHash, OnGetBlockHash,
#[derive(Copy)] #[derive(Copy)]
EmptyRequestBase { EmptyRequestBase {
#[serde(flatten)] #[cfg_attr(feature = "serde", serde(flatten))]
block_height: u64, block_height: u64,
}, },
EmptyResponseBase { EmptyResponseBase {
#[serde(flatten)] #[cfg_attr(feature = "serde", serde(flatten))]
block_hash: String, block_hash: String,
} }
} }

View file

@ -3,8 +3,10 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use std::fmt::Display; use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "epee")]
use cuprate_epee_encoding::{ use cuprate_epee_encoding::{
macros::bytes::{Buf, BufMut}, macros::bytes::{Buf, BufMut},
EpeeValue, Marker, EpeeValue, Marker,
@ -59,28 +61,27 @@ use crate::constants::{
/// assert_eq!(format!("{:?}", Status::PaymentRequired), "PaymentRequired"); /// assert_eq!(format!("{:?}", Status::PaymentRequired), "PaymentRequired");
/// assert_eq!(format!("{:?}", unknown), "Unknown"); /// assert_eq!(format!("{:?}", unknown), "Unknown");
/// ``` /// ```
#[derive( #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
)]
pub enum Status { pub enum Status {
// FIXME: // FIXME:
// `#[serde(rename = "")]` only takes raw string literals? // `#[serde(rename = "")]` only takes raw string literals?
// We have to re-type the constants here... // We have to re-type the constants here...
/// Successful RPC response, everything is OK; [`CORE_RPC_STATUS_OK`]. /// Successful RPC response, everything is OK; [`CORE_RPC_STATUS_OK`].
#[serde(rename = "OK")] #[cfg_attr(feature = "serde", serde(rename = "OK"))]
#[default] #[default]
Ok, Ok,
/// The daemon is busy, try later; [`CORE_RPC_STATUS_BUSY`]. /// The daemon is busy, try later; [`CORE_RPC_STATUS_BUSY`].
#[serde(rename = "BUSY")] #[cfg_attr(feature = "serde", serde(rename = "BUSY"))]
Busy, Busy,
/// The daemon is not mining; [`CORE_RPC_STATUS_NOT_MINING`]. /// The daemon is not mining; [`CORE_RPC_STATUS_NOT_MINING`].
#[serde(rename = "NOT MINING")] #[cfg_attr(feature = "serde", serde(rename = "NOT MINING"))]
NotMining, NotMining,
/// Payment is required for RPC; [`CORE_RPC_STATUS_PAYMENT_REQUIRED`]. /// Payment is required for RPC; [`CORE_RPC_STATUS_PAYMENT_REQUIRED`].
#[serde(rename = "PAYMENT REQUIRED")] #[cfg_attr(feature = "serde", serde(rename = "PAYMENT REQUIRED"))]
PaymentRequired, PaymentRequired,
/// Some unknown other string; [`CORE_RPC_STATUS_UNKNOWN`]. /// Some unknown other string; [`CORE_RPC_STATUS_UNKNOWN`].
@ -91,8 +92,8 @@ pub enum Status {
/// The reason this isn't `Unknown(String)` is because that /// The reason this isn't `Unknown(String)` is because that
/// disallows [`Status`] to be [`Copy`], and thus other types /// disallows [`Status`] to be [`Copy`], and thus other types
/// that contain it. /// that contain it.
#[serde(other)] #[cfg_attr(feature = "serde", serde(other))]
#[serde(rename = "UNKNOWN")] #[cfg_attr(feature = "serde", serde(rename = "UNKNOWN"))]
Unknown, Unknown,
} }
@ -132,6 +133,7 @@ impl Display for Status {
// //
// See below for more impl info: // See below for more impl info:
// <https://github.com/Cuprate/cuprate/blob/bef2a2cbd4e1194991751d1fbc96603cba8c7a51/net/epee-encoding/src/value.rs#L366-L392>. // <https://github.com/Cuprate/cuprate/blob/bef2a2cbd4e1194991751d1fbc96603cba8c7a51/net/epee-encoding/src/value.rs#L366-L392>.
#[cfg(feature = "epee")]
impl EpeeValue for Status { impl EpeeValue for Status {
const MARKER: Marker = <String as EpeeValue>::MARKER; const MARKER: Marker = <String as EpeeValue>::MARKER;
@ -161,6 +163,7 @@ mod test {
// Test epee (de)serialization works. // Test epee (de)serialization works.
#[test] #[test]
#[cfg(feature = "epee")]
fn epee() { fn epee() {
for status in [ for status in [
Status::Ok, Status::Ok,