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>.
//---------------------------------------------------------------------------------------------------- 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)]

View file

@ -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::[<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_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 [<Request $type_name>] {
$(
$( #[$field_attr] )*
pub $field: $field_type,
$( #[$request_field_attr] )*
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;

View file

@ -3,18 +3,18 @@
//! <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/daemon_messages.h>.
//---------------------------------------------------------------------------------------------------- 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)]