From dc68ab9413fd8c4d2fefda2941654473fc48f101 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 21 Jun 2026 14:42:56 +0900 Subject: [PATCH] commands: add `::new` constructors for macro generated items --- src/commands.rs | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 2c4bd1a..c55c49b 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -231,7 +231,15 @@ macro_rules! single_item_command_request { ($name:ident, $command_name:expr, $item_type:ty) => { paste::paste! { #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] - pub struct [<$name Request>] ($item_type); + pub struct [<$name Request>] (pub $item_type); + } + + impl paste::paste! { [<$name Request>] } { + pub fn new(item: $item_type) -> Self { + paste::paste! { + crate::commands::[<$name Request>](item) + } + } } impl crate::commands::CommandRequest for paste::paste! { [<$name Request>] } { @@ -268,7 +276,15 @@ macro_rules! single_optional_item_command_request { ($name:ident, $command_name:expr, $item_type:ty) => { paste::paste! { #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] - pub struct [<$name Request>] (Option<$item_type>); + pub struct [<$name Request>] (pub Option<$item_type>); + } + + impl paste::paste! { [<$name Request>] } { + pub fn new(item: Option<$item_type>) -> Self { + paste::paste! { + crate::commands::[<$name Request>](item) + } + } } impl crate::commands::CommandRequest for paste::paste! { [<$name Request>] } { @@ -311,7 +327,15 @@ macro_rules! single_item_command_response { ($name:ident, $item_name:expr, $item_type:ty) => { paste::paste! { #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] - pub struct [<$name Response>] ( $item_type ); + pub struct [<$name Response>] (pub $item_type); + } + + impl paste::paste! { [<$name Response>] } { + pub fn new(item: $item_type) -> Self { + paste::paste! { + crate::commands::[<$name Response>](item) + } + } } impl crate::commands::CommandResponse for paste::paste! { [<$name Response>] } { @@ -347,7 +371,15 @@ macro_rules! multi_item_command_response { ($name:ident, $item_name:expr, $item_type:ty) => { paste::paste! { #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] - pub struct [<$name Response>] ( Vec<$item_type> ); + pub struct [<$name Response>] (pub Vec<$item_type>); + } + + impl paste::paste! { [<$name Response>] } { + pub fn new(items: Vec<$item_type>) -> Self { + paste::paste! { + crate::commands::[<$name Response>](items) + } + } } impl crate::commands::CommandResponse for paste::paste! { [<$name Response>] } {