From 1a90c75f42dc4e1a710afe6a8cda895bb92bd90e Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 1 Jul 2024 20:53:16 -0400 Subject: [PATCH] rpc: use feature flags in misc types --- rpc/types/src/base.rs | 36 +++++++++++++++++++++--------------- rpc/types/src/json.rs | 4 ++-- rpc/types/src/status.rs | 21 ++++++++++++--------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/rpc/types/src/base.rs b/rpc/types/src/base.rs index 6a29367..f04a005 100644 --- a/rpc/types/src/base.rs +++ b/rpc/types/src/base.rs @@ -12,8 +12,10 @@ //! - //---------------------------------------------------------------------------------------------------- Import +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "epee")] use cuprate_epee_encoding::epee_object; use crate::Status; @@ -36,25 +38,27 @@ macro_rules! monero_rpc_base_link { /// The most common base for responses (nothing). /// #[doc = monero_rpc_base_link!(95..=99)] -#[derive( - Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, -)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct EmptyRequestBase; -cuprate_epee_encoding::epee_object! { +#[cfg(feature = "epee")] +epee_object! { EmptyRequestBase, } /// A base for RPC request types that support RPC payment. /// #[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 { /// The RPC payment client. pub client: String, } -cuprate_epee_encoding::epee_object! { +#[cfg(feature = "epee")] +epee_object! { AccessRequestBase, client: String, } @@ -65,21 +69,20 @@ cuprate_epee_encoding::epee_object! { /// This is for response types that do not contain /// any extra fields, e.g. TODO. // [`CalcPowResponse`](crate::json::CalcPowResponse). -#[derive( - Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, -)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct EmptyResponseBase; -cuprate_epee_encoding::epee_object! { +#[cfg(feature = "epee")] +epee_object! { EmptyResponseBase, } /// The most common base for responses. /// #[doc = monero_rpc_base_link!(101..=112)] -#[derive( - Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, -)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ResponseBase { /// General RPC error code. [`Status::Ok`] means everything looks good. pub status: Status, @@ -89,6 +92,7 @@ pub struct ResponseBase { pub untrusted: bool, } +#[cfg(feature = "epee")] epee_object! { ResponseBase, status: Status, @@ -98,10 +102,11 @@ epee_object! { /// A base for RPC response types that support RPC payment. /// #[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 { /// A flattened [`ResponseBase`]. - #[serde(flatten)] + #[cfg_attr(feature = "serde", serde(flatten))] pub response_base: ResponseBase, /// If payment for RPC is enabled, the number of credits /// available to the requesting client. Otherwise, `0`. @@ -111,6 +116,7 @@ pub struct AccessResponseBase { pub top_hash: String, } +#[cfg(feature = "epee")] epee_object! { AccessResponseBase, credits: u64, diff --git a/rpc/types/src/json.rs b/rpc/types/src/json.rs index 5f5f8ff..1dddc7f 100644 --- a/rpc/types/src/json.rs +++ b/rpc/types/src/json.rs @@ -113,11 +113,11 @@ define_request_and_response! { OnGetBlockHash, #[derive(Copy)] EmptyRequestBase { - #[serde(flatten)] + #[cfg_attr(feature = "serde", serde(flatten))] block_height: u64, }, EmptyResponseBase { - #[serde(flatten)] + #[cfg_attr(feature = "serde", serde(flatten))] block_hash: String, } } diff --git a/rpc/types/src/status.rs b/rpc/types/src/status.rs index e8ac6ce..2322a56 100644 --- a/rpc/types/src/status.rs +++ b/rpc/types/src/status.rs @@ -3,8 +3,10 @@ //---------------------------------------------------------------------------------------------------- Import use std::fmt::Display; +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[cfg(feature = "epee")] use cuprate_epee_encoding::{ macros::bytes::{Buf, BufMut}, EpeeValue, Marker, @@ -59,28 +61,27 @@ use crate::constants::{ /// assert_eq!(format!("{:?}", Status::PaymentRequired), "PaymentRequired"); /// assert_eq!(format!("{:?}", unknown), "Unknown"); /// ``` -#[derive( - Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, -)] +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Status { // FIXME: // `#[serde(rename = "")]` only takes raw string literals? // We have to re-type the constants here... /// Successful RPC response, everything is OK; [`CORE_RPC_STATUS_OK`]. - #[serde(rename = "OK")] + #[cfg_attr(feature = "serde", serde(rename = "OK"))] #[default] Ok, /// The daemon is busy, try later; [`CORE_RPC_STATUS_BUSY`]. - #[serde(rename = "BUSY")] + #[cfg_attr(feature = "serde", serde(rename = "BUSY"))] Busy, /// The daemon is not mining; [`CORE_RPC_STATUS_NOT_MINING`]. - #[serde(rename = "NOT MINING")] + #[cfg_attr(feature = "serde", serde(rename = "NOT MINING"))] NotMining, /// Payment is required for RPC; [`CORE_RPC_STATUS_PAYMENT_REQUIRED`]. - #[serde(rename = "PAYMENT REQUIRED")] + #[cfg_attr(feature = "serde", serde(rename = "PAYMENT REQUIRED"))] PaymentRequired, /// 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 /// disallows [`Status`] to be [`Copy`], and thus other types /// that contain it. - #[serde(other)] - #[serde(rename = "UNKNOWN")] + #[cfg_attr(feature = "serde", serde(other))] + #[cfg_attr(feature = "serde", serde(rename = "UNKNOWN"))] Unknown, } @@ -132,6 +133,7 @@ impl Display for Status { // // See below for more impl info: // . +#[cfg(feature = "epee")] impl EpeeValue for Status { const MARKER: Marker = ::MARKER; @@ -161,6 +163,7 @@ mod test { // Test epee (de)serialization works. #[test] + #[cfg(feature = "epee")] fn epee() { for status in [ Status::Ok,