lib.rs: create re-export macro

This commit is contained in:
hinto.janai 2024-06-03 21:20:36 -04:00
parent a4ad660daa
commit 8c24f13e8c
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 155 additions and 62 deletions

View file

@ -3,7 +3,7 @@
//! <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::{macros::define_monero_rpc_struct, misc::Status}; use crate::macros::define_monero_rpc_struct;
//---------------------------------------------------------------------------------------------------- Struct definitions //---------------------------------------------------------------------------------------------------- Struct definitions
define_monero_rpc_struct! { define_monero_rpc_struct! {
@ -12,14 +12,12 @@ define_monero_rpc_struct! {
// The `$file.$extension` in which this type is defined in the Monero // The `$file.$extension` in which this type is defined in the Monero
// codebase in the `rpc/` directory, followed by the specific lines. // codebase in the `rpc/` directory, followed by the specific lines.
core_rpc_server_commands_defs.h => 919..=933, 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. // The actual type definitions.
// If there are any additional attributes (`/// docs` or `#[derive]`s) // If there are any additional attributes (`/// docs` or `#[derive]`s)
// for the struct, they go here, e.g.: // for the struct, they go here, e.g.:
// #[derive(MyCustomDerive)] // #[derive(MyCustomDerive)]
GetBlockCount /* <- The type name */ { GetBlockCount, // <- The type name.
Request /* <- The request type */ {
// Within the `{}` is an infinite matching pattern of: // Within the `{}` is an infinite matching pattern of:
// ``` // ```
// $ATTRIBUTES // $ATTRIBUTES
@ -30,24 +28,32 @@ define_monero_rpc_struct! {
/// How many blocks are in the longest chain known to the node. /// How many blocks are in the longest chain known to the node.
count: u64, count: u64,
/// General RPC error code. "OK" means everything looks good. /// 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). /// Whether the node is untrusted (see Monero docs).
untrusted: bool, untrusted: bool,
} }
} }
define_monero_rpc_struct! { // define_monero_rpc_struct! {
on_get_block_hash, // on_get_block_hash,
core_rpc_server_commands_defs.h => 919..=933, // core_rpc_server_commands_defs.h => 919..=933,
OnGetBlockHash { height: [123] } => // OnGetBlockHash { height: [123] } =>
r#"[123]"#, // r#"[123]"#,
#[repr(transparent)] // #[repr(transparent)]
#[cfg_attr(feature = "serde", serde(transparent))] // #[cfg_attr(feature = "serde", serde(transparent))]
OnGetBlockHash { // OnGetBlockHash {
/// A block's height. // /// A block's height.
height: [u64; 1], // height: [u64; 1],
} // }
} // }
//---------------------------------------------------------------------------------------------------- Tests //---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)] #[cfg(test)]

View file

@ -38,7 +38,6 @@
overlapping_range_endpoints, overlapping_range_endpoints,
semicolon_in_expressions_from_macros, semicolon_in_expressions_from_macros,
noop_method_call, noop_method_call,
unreachable_pub,
)] )]
// Deny lints. // Deny lints.
// Some of these are `#[allow]`'ed on a per-case basis. // Some of these are `#[allow]`'ed on a per-case basis.
@ -57,7 +56,8 @@
missing_docs, missing_docs,
deprecated, deprecated,
unused_comparisons, unused_comparisons,
nonstandard_style nonstandard_style,
unreachable_pub
)] )]
#![allow( #![allow(
// FIXME: this lint affects crates outside of // FIXME: this lint affects crates outside of
@ -98,9 +98,90 @@
)] )]
//---------------------------------------------------------------------------------------------------- Use //---------------------------------------------------------------------------------------------------- Use
pub mod binary; mod binary;
pub mod data; mod data;
pub mod json; mod json;
pub mod macros; mod macros;
pub mod misc; 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::[<Request $json_type>] as $json_type;
)*
}
/// TODO
pub mod binary {
$(
pub use $crate::binary::[<Request $binary_type>] as $binary_type;
)*
}
/// TODO
pub mod other {
$(
pub use $crate::other::[<Request $other_type>] as $other_type;
)*
}
}
/// TODO
pub mod resp {
/// TODO
pub mod json {
$(
pub use $crate::json::[<Response $json_type>] as $json_type;
)*
}
/// TODO
pub mod binary {
$(
pub use $crate::binary::[<Response $binary_type>] as $binary_type;
)*
}
/// TODO
pub mod other {
$(
pub use $crate::other::[<Response $other_type>] as $other_type;
)*
}
}
}};
}
re_export_request_and_response_types! {
json {
GetBlockCount,
}
binary {
}
other {
}
}

View file

@ -20,23 +20,30 @@ macro_rules! define_monero_rpc_struct {
$monero_code_filename:ident.$monero_code_filename_extension:ident => $monero_code_filename:ident.$monero_code_filename_extension:ident =>
$monero_code_line_start:literal..=$monero_code_line_end:literal, $monero_code_line_start:literal..=$monero_code_line_end:literal,
// A real literal type expression, and its JSON string form. // The actual request `struct` name and any doc comments, derives, etc.
// Used in example doc-test. $( #[$request_type_attr:meta] )*
$type_literal:expr => $type_as_json:literal, $type_name:ident,
Request {
// The actual `struct` name and any doc comments, derives, etc.
$( #[$type_attr:meta] )*
$type_name:ident {
// And any fields. // And any fields.
$( $(
$( #[$field_attr:meta] )* $( #[$request_field_attr:meta] )*
$field:ident: $field_type:ty, $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)] #[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
$( #[$type_attr] )* $( #[$request_type_attr] )*
#[doc = concat!( #[doc = concat!(
"", "",
"Definition: [`", "Definition: [`",
@ -61,26 +68,25 @@ macro_rules! define_monero_rpc_struct {
"`](https://www.getmonero.org/resources/developer-guides/daemon-rpc.html", "`](https://www.getmonero.org/resources/developer-guides/daemon-rpc.html",
"#", "#",
stringify!($monero_daemon_rpc_doc_link), stringify!($monero_daemon_rpc_doc_link),
").", ")."
)] )]
/// #[allow(dead_code)]
/// # `serde` example pub struct [<Request $type_name>] {
/// ```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 {
$( $(
$( #[$field_attr] )* $( #[$request_field_attr] )*
pub $field: $field_type, pub $request_field: $request_field_type,
)* )*
} }
};
#[allow(dead_code)]
/// TODO
pub struct [<Response $type_name>] {
$(
$( #[$response_field_attr] )*
pub $response_field: $response_field_type,
)*
}
}};
} }
pub(crate) use define_monero_rpc_struct; pub(crate) use define_monero_rpc_struct;

View file

@ -3,18 +3,18 @@
//! <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::macros::define_monero_rpc_struct; // use crate::macros::define_monero_rpc_struct;
//---------------------------------------------------------------------------------------------------- TODO //---------------------------------------------------------------------------------------------------- TODO
define_monero_rpc_struct! { // define_monero_rpc_struct! {
get_height, // get_height,
daemon_messages.h => 81..=87, // daemon_messages.h => 81..=87,
GetHeight { height: 123 } => r#"{"height":123}"#, // GetHeight { height: 123 } => r#"{"height":123}"#,
GetHeight { // GetHeight {
/// A block's height. // /// A block's height.
height: u64, // height: u64,
} // }
} // }
//---------------------------------------------------------------------------------------------------- Tests //---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)] #[cfg(test)]