From 8c24f13e8cc6d88409f7e6bcf29fbff35ade20ae Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 3 Jun 2024 21:20:36 -0400 Subject: [PATCH] lib.rs: create re-export macro --- rpc/monero-rpc-types/src/json.rs | 42 +++++++------ rpc/monero-rpc-types/src/lib.rs | 95 +++++++++++++++++++++++++++--- rpc/monero-rpc-types/src/macros.rs | 60 ++++++++++--------- rpc/monero-rpc-types/src/other.rs | 20 +++---- 4 files changed, 155 insertions(+), 62 deletions(-) diff --git a/rpc/monero-rpc-types/src/json.rs b/rpc/monero-rpc-types/src/json.rs index d4e60d2..a217450 100644 --- a/rpc/monero-rpc-types/src/json.rs +++ b/rpc/monero-rpc-types/src/json.rs @@ -3,7 +3,7 @@ //! . //---------------------------------------------------------------------------------------------------- Import -use crate::{macros::define_monero_rpc_struct, misc::Status}; +use crate::macros::define_monero_rpc_struct; //---------------------------------------------------------------------------------------------------- Struct definitions define_monero_rpc_struct! { @@ -12,14 +12,12 @@ define_monero_rpc_struct! { // The `$file.$extension` in which this type is defined in the Monero // codebase in the `rpc/` directory, followed by the specific lines. core_rpc_server_commands_defs.h => 919..=933, - // The type and its compacted JSON string form, used in example doc-test. - GetBlockCount { count: 123, status: Status::Ok, untrusted: false } => - r#"{"count":123,"status":"OK","untrusted":false}"#, // The actual type definitions. // If there are any additional attributes (`/// docs` or `#[derive]`s) // for the struct, they go here, e.g.: // #[derive(MyCustomDerive)] - GetBlockCount /* <- The type name */ { + GetBlockCount, // <- The type name. + Request /* <- The request type */ { // Within the `{}` is an infinite matching pattern of: // ``` // $ATTRIBUTES @@ -30,24 +28,32 @@ define_monero_rpc_struct! { /// How many blocks are in the longest chain known to the node. count: u64, /// General RPC error code. "OK" means everything looks good. - status: Status, + status: crate::misc::Status, + /// Whether the node is untrusted (see Monero docs). + untrusted: bool, + }, + Response /* <- The response type */ { + /// How many blocks are in the longest chain known to the node. + count: u64, + /// General RPC error code. "OK" means everything looks good. + status: crate::misc::Status, /// Whether the node is untrusted (see Monero docs). untrusted: bool, } } -define_monero_rpc_struct! { - on_get_block_hash, - core_rpc_server_commands_defs.h => 919..=933, - OnGetBlockHash { height: [123] } => - r#"[123]"#, - #[repr(transparent)] - #[cfg_attr(feature = "serde", serde(transparent))] - OnGetBlockHash { - /// A block's height. - height: [u64; 1], - } -} +// define_monero_rpc_struct! { +// on_get_block_hash, +// core_rpc_server_commands_defs.h => 919..=933, +// OnGetBlockHash { height: [123] } => +// r#"[123]"#, +// #[repr(transparent)] +// #[cfg_attr(feature = "serde", serde(transparent))] +// OnGetBlockHash { +// /// A block's height. +// height: [u64; 1], +// } +// } //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] diff --git a/rpc/monero-rpc-types/src/lib.rs b/rpc/monero-rpc-types/src/lib.rs index 996a186..07a5d07 100644 --- a/rpc/monero-rpc-types/src/lib.rs +++ b/rpc/monero-rpc-types/src/lib.rs @@ -38,7 +38,6 @@ overlapping_range_endpoints, semicolon_in_expressions_from_macros, noop_method_call, - unreachable_pub, )] // Deny lints. // Some of these are `#[allow]`'ed on a per-case basis. @@ -57,7 +56,8 @@ missing_docs, deprecated, unused_comparisons, - nonstandard_style + nonstandard_style, + unreachable_pub )] #![allow( // FIXME: this lint affects crates outside of @@ -98,9 +98,90 @@ )] //---------------------------------------------------------------------------------------------------- Use -pub mod binary; -pub mod data; -pub mod json; -pub mod macros; +mod binary; +mod data; +mod json; +mod macros; pub mod misc; -pub mod other; +mod other; + +/// TODO +macro_rules! re_export_request_and_response_types { + ( + json { + $( + $json_type:ident, + )* + } + binary { + $( + $binary_type:ident, + )* + } + other { + $( + $other_type:ident, + )* + } + ) => { paste::paste! { + /// TODO + pub mod req { + /// TODO + pub mod json { + $( + pub use $crate::json::[] as $json_type; + )* + } + + /// TODO + pub mod binary { + $( + pub use $crate::binary::[] as $binary_type; + )* + } + + /// TODO + pub mod other { + $( + pub use $crate::other::[] as $other_type; + )* + } + } + + /// TODO + pub mod resp { + /// TODO + pub mod json { + $( + pub use $crate::json::[] as $json_type; + )* + } + + /// TODO + pub mod binary { + $( + pub use $crate::binary::[] as $binary_type; + )* + } + + /// TODO + pub mod other { + $( + pub use $crate::other::[] as $other_type; + )* + } + } + }}; +} + +re_export_request_and_response_types! { + json { + GetBlockCount, + } + + binary { + } + + other { + } +} diff --git a/rpc/monero-rpc-types/src/macros.rs b/rpc/monero-rpc-types/src/macros.rs index f00467c..e42ab9a 100644 --- a/rpc/monero-rpc-types/src/macros.rs +++ b/rpc/monero-rpc-types/src/macros.rs @@ -20,23 +20,30 @@ macro_rules! define_monero_rpc_struct { $monero_code_filename:ident.$monero_code_filename_extension:ident => $monero_code_line_start:literal..=$monero_code_line_end:literal, - // A real literal type expression, and its JSON string form. - // Used in example doc-test. - $type_literal:expr => $type_as_json:literal, - - // The actual `struct` name and any doc comments, derives, etc. - $( #[$type_attr:meta] )* - $type_name:ident { + // The actual request `struct` name and any doc comments, derives, etc. + $( #[$request_type_attr:meta] )* + $type_name:ident, + Request { // And any fields. $( - $( #[$field_attr:meta] )* - $field:ident: $field_type:ty, + $( #[$request_field_attr:meta] )* + $request_field:ident: $request_field_type:ty, + )* + }, + + // The actual `struct` name and any doc comments, derives, etc. + $( #[$response_type_attr:meta] )* + Response { + // And any fields. + $( + $( #[$response_field_attr:meta] )* + $response_field:ident: $response_field_type:ty, )* } - ) => { + ) => { paste::paste! { #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] - $( #[$type_attr] )* + $( #[$request_type_attr] )* #[doc = concat!( "", "Definition: [`", @@ -61,26 +68,25 @@ macro_rules! define_monero_rpc_struct { "`](https://www.getmonero.org/resources/developer-guides/daemon-rpc.html", "#", stringify!($monero_daemon_rpc_doc_link), - ").", + ")." )] - /// - /// # `serde` example - /// ```rust - #[doc = "# use monero_rpc_types::{json::*, binary::*, data::*, misc::*, other::*};"] - #[doc = concat!("let t = ", stringify!($type_literal), ";")] - #[doc = "let string = serde_json::to_string(&t).unwrap();"] - #[doc = concat!("assert_eq!(string, ", stringify!($type_as_json), ");")] - #[doc = ""] - #[doc = "let t2 = serde_json::from_str(&string).unwrap();"] - #[doc = "assert_eq!(t, t2);"] - /// ``` - pub struct $type_name { + #[allow(dead_code)] + pub struct [] { $( - $( #[$field_attr] )* - pub $field: $field_type, + $( #[$request_field_attr] )* + pub $request_field: $request_field_type, )* } - }; + + #[allow(dead_code)] + /// TODO + pub struct [] { + $( + $( #[$response_field_attr] )* + pub $response_field: $response_field_type, + )* + } + }}; } pub(crate) use define_monero_rpc_struct; diff --git a/rpc/monero-rpc-types/src/other.rs b/rpc/monero-rpc-types/src/other.rs index 742feb8..fc6d073 100644 --- a/rpc/monero-rpc-types/src/other.rs +++ b/rpc/monero-rpc-types/src/other.rs @@ -3,18 +3,18 @@ //! . //---------------------------------------------------------------------------------------------------- Import -use crate::macros::define_monero_rpc_struct; +// use crate::macros::define_monero_rpc_struct; //---------------------------------------------------------------------------------------------------- TODO -define_monero_rpc_struct! { - get_height, - daemon_messages.h => 81..=87, - GetHeight { height: 123 } => r#"{"height":123}"#, - GetHeight { - /// A block's height. - height: u64, - } -} +// define_monero_rpc_struct! { +// get_height, +// daemon_messages.h => 81..=87, +// GetHeight { height: 123 } => r#"{"height":123}"#, +// GetHeight { +// /// A block's height. +// height: u64, +// } +// } //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)]