Skip to content

Commit

Permalink
Take params by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 25, 2024
1 parent c2d0a66 commit 68b94de
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ macro_rules! rpc_trait {

impl $crate::params::Action for [<$trait_name Action>] {
#[cfg(feature = "server")]
fn from_parts(action: &str, params: $crate::server::ActionParams) -> $crate::server::Result<Result<Self, $crate::server::ActionParams>> {
Ok(Ok(match (action, params) {
fn from_parts(action: &str, params: &mut $crate::server::ActionParams) -> $crate::server::Result<Option<Self>> {
Ok(Some(match (action, params) {
$(
($method_path, $crate::server::ActionParams::$http_method(params)) => {
#[allow(unused)]
Expand All @@ -71,7 +71,7 @@ macro_rules! rpc_trait {
Self::$method_name { $($param),* }
}
)*
(_, params) => return Ok(Err(params)),
_ => return Ok(None),
}))
}

Expand Down Expand Up @@ -135,24 +135,24 @@ macro_rules! rpc_trait {
/// Private inherent method for handling actions.
/// This method could live on the trait itself, but then it wouldn't be possible to make it private.
#[allow(non_camel_case_types)]
async fn handle_action(device: &(impl ?Sized + $trait_name), action: &str, params: $crate::server::ActionParams) -> $crate::server::Result<impl Serialize> {
async fn handle_action(device: &(impl ?Sized + $trait_name), action: &str, mut params: $crate::server::ActionParams) -> $crate::server::Result<impl Serialize> {
#[derive(Serialize)]
#[serde(untagged)]
#[allow(non_camel_case_types)]
enum ResponseRepr<$($method_name),*> {
$($method_name($method_name),)*
}

let value = match $crate::params::Action::from_parts(action, params)? {
Ok(action) => match action {
let value = match $crate::params::Action::from_parts(action, &mut params)? {
Some(action) => match action {
$(
[<$trait_name Action>]::$method_name { $($param),* } => {
#[allow(deprecated)]
device.$method_name($($param),*).await.map(ResponseRepr::$method_name)
}
)*
}?,
Err(params) => rpc_trait!(@if_specific $trait_name {
None=> rpc_trait!(@if_specific $trait_name {
return match <dyn Device>::handle_action(device, action, params).await {
Ok(value) => Ok($crate::either::Either::Right(value)),
Err(mut err) => {
Expand Down
4 changes: 2 additions & 2 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub(crate) trait Action: Sized + Send {
#[cfg(feature = "server")]
fn from_parts(
action: &str,
params: crate::server::ActionParams,
) -> crate::server::Result<Result<Self, crate::server::ActionParams>>;
params: &mut crate::server::ActionParams,
) -> crate::server::Result<Option<Self>>;

#[cfg(feature = "client")]
fn into_parts(self) -> ActionParams<impl serde::Serialize + Send>;
Expand Down
2 changes: 1 addition & 1 deletion src/server/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
.ok_or(Error::MissingParameter { name })
}

pub(crate) fn finish_extraction(self) {
pub(crate) fn finish_extraction(&self) {
if !self.0.is_empty() {
tracing::warn!("Unused parameters: {:?}", self.0.keys());
}
Expand Down

0 comments on commit 68b94de

Please sign in to comment.