support serde/epee default values

This commit is contained in:
hinto.janai 2024-07-03 16:40:21 -04:00
parent 72bd1673d6
commit d0981da311
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 119 additions and 14 deletions

16
rpc/types/src/defaults.rs Normal file
View file

@ -0,0 +1,16 @@
//! TODO
//---------------------------------------------------------------------------------------------------- Import
//---------------------------------------------------------------------------------------------------- TODO
/// TODO
#[inline]
pub(crate) const fn bool() -> bool {
false
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -3,7 +3,12 @@
//! <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/daemon_messages.h>. //! <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/daemon_messages.h>.
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use crate::{base::ResponseBase, macros::define_request_and_response}; use crate::{
base::{AccessResponseBase, ResponseBase},
defaults::bool,
macros::define_request_and_response,
misc::BlockHeader,
};
//---------------------------------------------------------------------------------------------------- Struct definitions //---------------------------------------------------------------------------------------------------- Struct definitions
// This generates 2 structs: // This generates 2 structs:
@ -109,12 +114,32 @@ define_request_and_response! {
core_rpc_server_commands_defs.h => 935..=939, core_rpc_server_commands_defs.h => 935..=939,
OnGetBlockHash, OnGetBlockHash,
#[derive(Copy)] #[derive(Copy)]
/// ```rust
/// use serde_json::*;
/// use cuprate_rpc_types::json::*;
///
/// let x = OnGetBlockHashRequest { block_height: [3] };
/// let x = to_string(&x).unwrap();
/// assert_eq!(x, "[3]");
/// ```
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
Request { Request {
#[cfg_attr(feature = "serde", serde(flatten))] // This is `std::vector<uint64_t>` in `monerod` but
block_height: u64, // it must be a 1 length array or else it will error.
block_height: [u64; 1],
}, },
/// ```rust
/// use serde_json::*;
/// use cuprate_rpc_types::json::*;
///
/// let x = OnGetBlockHashResponse { block_hash: String::from("asdf") };
/// let x = to_string(&x).unwrap();
/// assert_eq!(x, "\"asdf\"");
/// ```
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
Response { Response {
#[cfg_attr(feature = "serde", serde(flatten))]
block_hash: String, block_hash: String,
} }
} }
@ -152,6 +177,69 @@ define_request_and_response! {
} }
} }
define_request_and_response! {
generateblocks,
cc73fe71162d564ffda8e549b79a350bca53c454 =>
core_rpc_server_commands_defs.h => 1130..=1161,
GenerateBlocks,
Request {
amount_of_blocks: u64,
wallet_address: String,
prev_block: String,
starting_nonce: u32,
},
ResponseBase {
height: u64,
blocks: Vec<String>,
}
}
define_request_and_response! {
get_last_block_header,
cc73fe71162d564ffda8e549b79a350bca53c454 =>
core_rpc_server_commands_defs.h => 1214..=1238,
GetLastBlockHeader,
Request {
#[cfg_attr(feature = "serde", serde(default = "bool"))]
fill_pow_hash: bool = bool(),
},
AccessResponseBase {
block_header: BlockHeader,
}
}
define_request_and_response! {
get_block_header_by_hash,
cc73fe71162d564ffda8e549b79a350bca53c454 =>
core_rpc_server_commands_defs.h => 1240..=1269,
GetBlockHeaderByHash,
Request {
hash: String,
hashes: Vec<String>,
#[cfg_attr(feature = "serde", serde(default = "bool"))]
fill_pow_hash: bool = bool(),
},
AccessResponseBase {
block_header: BlockHeader,
block_headers: Vec<BlockHeader>,
}
}
define_request_and_response! {
get_block_header_by_height,
cc73fe71162d564ffda8e549b79a350bca53c454 =>
core_rpc_server_commands_defs.h => 1271..=1296,
GetBlockHeaderByHeight,
Request {
height: u64,
#[cfg_attr(feature = "serde", serde(default = "bool"))]
fill_pow_hash: bool = bool(),
},
AccessResponseBase {
block_header: BlockHeader,
}
}
//---------------------------------------------------------------------------------------------------- Tests //---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View file

@ -99,6 +99,7 @@
//---------------------------------------------------------------------------------------------------- Use //---------------------------------------------------------------------------------------------------- Use
mod binary_string; mod binary_string;
mod constants; mod constants;
mod defaults;
mod macros; mod macros;
mod status; mod status;

View file

@ -63,7 +63,7 @@ macro_rules! define_request_and_response {
// And any fields. // And any fields.
$( $(
$( #[$request_field_attr:meta] )* $( #[$request_field_attr:meta] )*
$request_field:ident: $request_field_type:ty, $request_field:ident: $request_field_type:ty $(= $request_field_type_default:expr)?,
)* )*
}, },
@ -73,7 +73,7 @@ macro_rules! define_request_and_response {
// And any fields. // And any fields.
$( $(
$( #[$response_field_attr:meta] )* $( #[$response_field_attr:meta] )*
$response_field:ident: $response_field_type:ty, $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?,
)* )*
} }
) => { paste::paste! { ) => { paste::paste! {
@ -91,7 +91,7 @@ macro_rules! define_request_and_response {
[<$type_name Request>] { [<$type_name Request>] {
$( $(
$( #[$request_field_attr] )* $( #[$request_field_attr] )*
$request_field: $request_field_type, $request_field: $request_field_type $(= $request_field_type_default)?,
)* )*
} }
} }
@ -114,7 +114,7 @@ macro_rules! define_request_and_response {
$response_base_type => [<$type_name Response>] { $response_base_type => [<$type_name Response>] {
$( $(
$( #[$response_field_attr] )* $( #[$response_field_attr] )*
$response_field: $response_field_type, $response_field: $response_field_type $(= $response_field_type_default)?,
)* )*
} }
} }
@ -145,7 +145,7 @@ macro_rules! define_request {
// And any fields. // And any fields.
$( $(
$( #[$request_field_attr:meta] )* $( #[$request_field_attr:meta] )*
$request_field:ident: $request_field_type:ty, $request_field:ident: $request_field_type:ty $(= $request_field_type_default:expr)?,
)* )*
} }
) => { ) => {
@ -164,7 +164,7 @@ macro_rules! define_request {
::cuprate_epee_encoding::epee_object! { ::cuprate_epee_encoding::epee_object! {
$request_type_name, $request_type_name,
$( $(
$request_field: $request_field_type, $request_field: $request_field_type $(= $request_field_type_default)?,
)* )*
} }
}; };
@ -181,7 +181,7 @@ macro_rules! define_response {
// And any fields. // And any fields.
$( $(
$( #[$response_field_attr:meta] )* $( #[$response_field_attr:meta] )*
$response_field:ident: $response_field_type:ty, $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?,
)* )*
} }
) => { ) => {
@ -197,7 +197,7 @@ macro_rules! define_response {
::cuprate_epee_encoding::epee_object! { ::cuprate_epee_encoding::epee_object! {
$response_type_name, $response_type_name,
$( $(
$response_field: $response_field_type, $response_field: $response_field_type $($response_field_type_default)?,
)* )*
} }
}; };
@ -211,7 +211,7 @@ macro_rules! define_response {
// And any fields. // And any fields.
$( $(
$( #[$response_field_attr:meta] )* $( #[$response_field_attr:meta] )*
$response_field:ident: $response_field_type:ty, $response_field:ident: $response_field_type:ty $(= $response_field_type_default:expr)?,
)* )*
} }
) => { ) => {
@ -230,7 +230,7 @@ macro_rules! define_response {
::cuprate_epee_encoding::epee_object! { ::cuprate_epee_encoding::epee_object! {
$response_type_name, $response_type_name,
$( $(
$response_field: $response_field_type, $response_field: $response_field_type $(= $response_field_type_default)?,
)* )*
!flatten: base: $response_base_type, !flatten: base: $response_base_type,
} }