From c2d0a66d840f16da6d624be2cab8b3550c944150 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 26 Sep 2024 00:24:19 +0100 Subject: [PATCH] Start moving to action enum --- Cargo.lock | 239 +++++++++++++++++++++++++------------ Cargo.toml | 3 + src/client/mod.rs | 15 +-- src/lib.rs | 1 + src/macros.rs | 105 +++++++++------- src/{client => }/params.rs | 11 ++ 6 files changed, 246 insertions(+), 128 deletions(-) rename src/{client => }/params.rs (54%) diff --git a/Cargo.lock b/Cargo.lock index e9e010a..b4c4340 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,6 +249,7 @@ dependencies = [ "ctor", "custom_debug", "eyre", + "form_urlencoded", "futures", "http", "indexmap", @@ -260,6 +261,7 @@ dependencies = [ "netdev", "num_enum", "once_cell", + "paste", "rand", "reqwest", "sailfish", @@ -268,6 +270,7 @@ dependencies = [ "serde_json", "serde_plain", "serde_repr", + "serdebug", "serial_test", "socket2 0.5.7", "thiserror", @@ -435,8 +438,8 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -470,8 +473,8 @@ version = "0.1.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -637,8 +640,8 @@ dependencies = [ "log", "peeking_take_while", "prettyplease", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "regex", "rustc-hash", "shlex", @@ -769,8 +772,8 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cc8b54b395f2fcfbb3d90c47b01c7f444d94d05bdeb775811dec868ac3bbc26" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -1093,7 +1096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d375883580a668c7481ea6631fc1a8863e33cc335bf56bfad8d7e6d4b04b13a5" dependencies = [ "com_macros_support", - "proc-macro2", + "proc-macro2 1.0.86", "syn 1.0.109", ] @@ -1103,8 +1106,8 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad899a1087a9296d5644792d7cb72b8e34c1bec8e7d4fbc002230169a6e8710c" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] @@ -1321,7 +1324,7 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" dependencies = [ - "quote", + "quote 1.0.37", "syn 2.0.76", ] @@ -1348,8 +1351,8 @@ checksum = "f731440b39c73910e253cb465ec1fac97732b3c7af215639881ec0c2a38f4f69" dependencies = [ "darling", "itertools 0.12.1", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", "synstructure", ] @@ -1372,8 +1375,8 @@ checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" dependencies = [ "fnv", "ident_case", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "strsim", "syn 2.0.76", ] @@ -1385,7 +1388,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", - "quote", + "quote 1.0.37", "syn 2.0.76", ] @@ -1404,8 +1407,8 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] @@ -1621,8 +1624,8 @@ version = "0.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -1802,8 +1805,8 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -1910,8 +1913,8 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -2772,7 +2775,7 @@ dependencies = [ "spirv", "termcolor", "thiserror", - "unicode-xid", + "unicode-xid 0.2.5", ] [[package]] @@ -2847,8 +2850,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da93265ae87c37493239e475c4cef98ff7a6b9f3f845bbd8957c714250614c6d" dependencies = [ "proc-macro-error", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] @@ -3067,8 +3070,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ "proc-macro-crate 3.2.0", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -3355,8 +3358,8 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -3503,7 +3506,7 @@ version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.86", "syn 2.0.76", ] @@ -3533,8 +3536,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", "version_check", ] @@ -3545,11 +3548,20 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "version_check", ] +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid 0.1.0", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -3580,13 +3592,22 @@ dependencies = [ "memchr", ] +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2 0.4.30", +] + [[package]] name = "quote" version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.86", ] [[package]] @@ -3784,6 +3805,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.37.27" @@ -3844,8 +3874,8 @@ dependencies = [ "filetime", "home", "memchr", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "serde", "syn 2.0.76", "toml", @@ -3857,7 +3887,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e47e31910c5f9230e99992568d05a5968fe4f42a635c3f912c993e9f66a619a5" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.86", "sailfish-compiler", ] @@ -3910,6 +3940,21 @@ version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0495e4577c672de8254beb68d01a9b62d0e8a13c099edecdbedccce3223cd29f" +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.209" @@ -3935,8 +3980,8 @@ version = "1.0.209" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -3977,8 +4022,8 @@ version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4003,6 +4048,27 @@ dependencies = [ "serde", ] +[[package]] +name = "serdebug" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "195540c83d8d0a4ff60bf4318a7bfe259de8131ccee0bddb737638683dd80a16" +dependencies = [ + "rustc_version", + "serde", + "serdebug_derive", +] + +[[package]] +name = "serdebug_derive" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e82acb3dd791085c6091f48c9175456485c48c10cf37d91a041f4606bf45b341" +dependencies = [ + "quote 0.6.13", + "syn 0.14.9", +] + [[package]] name = "serial_test" version = "3.1.1" @@ -4023,8 +4089,8 @@ version = "3.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82fe9db325bcef1fbcde82e078a5cc4efdf787e96b3b9cf45b50b529f2083d67" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4219,14 +4285,25 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "syn" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" +dependencies = [ + "proc-macro2 0.4.30", + "quote 0.6.13", + "unicode-xid 0.1.0", +] + [[package]] name = "syn" version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "unicode-ident", ] @@ -4236,8 +4313,8 @@ version = "2.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "unicode-ident", ] @@ -4262,8 +4339,8 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4325,8 +4402,8 @@ version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4444,8 +4521,8 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4540,8 +4617,8 @@ version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -4686,6 +4763,12 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + [[package]] name = "unicode-xid" version = "0.2.5" @@ -4786,8 +4869,8 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", "wasm-bindgen-shared", ] @@ -4810,7 +4893,7 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ - "quote", + "quote 1.0.37", "wasm-bindgen-macro-support", ] @@ -4820,8 +4903,8 @@ version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", "wasm-bindgen-backend", "wasm-bindgen-shared", @@ -4950,9 +5033,9 @@ version = "0.31.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7b56f89937f1cf2ee1f1259cf2936a17a1f45d8f0aa1019fae6d470d304cfa6" dependencies = [ - "proc-macro2", + "proc-macro2 1.0.86", "quick-xml", - "quote", + "quote 1.0.37", ] [[package]] @@ -5209,8 +5292,8 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e2ee588991b9e7e6c8338edf3333fbe4da35dc72092643958ebb43f0ab2c49c" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] @@ -5220,8 +5303,8 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6fb8df20c9bcaa8ad6ab513f7b40104840c8867d5751126e4df3b08388d0cc7" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] @@ -5657,8 +5740,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7131497b0f887e8061b430c530240063d33bf9455fa34438f388a245da69e0a5" dependencies = [ "proc-macro-crate 1.3.1", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "regex", "syn 1.0.109", "zvariant_utils", @@ -5691,8 +5774,8 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 2.0.76", ] @@ -5717,8 +5800,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c24dc0bed72f5f90d1f8bb5b07228cbf63b3c6e9f82d82559d4bae666e7ed9" dependencies = [ "proc-macro-crate 1.3.1", - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", "zvariant_utils", ] @@ -5729,7 +5812,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7234f0d811589db492d16893e3f21e8e2fd282e6d01b0cddee310322062cc200" dependencies = [ - "proc-macro2", - "quote", + "proc-macro2 1.0.86", + "quote 1.0.37", "syn 1.0.109", ] diff --git a/Cargo.toml b/Cargo.toml index 5936b92..18b3d42 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,9 @@ time = { version = "0.3.36", features = ["macros"], optional = true } tokio = { workspace = true, features = ["net", "rt", "io-util"] } tracing = { workspace = true } tracing-futures = { version = "0.2.5", features = ["futures-03"], optional = true } +paste = "1.0.15" +form_urlencoded = "1.2.1" +serdebug = "1.0.5" [target.'cfg(windows)'.dependencies] windows-sys = { version = "0.59.0", features = ["Win32_Networking_WinSock"] } diff --git a/src/client/mod.rs b/src/client/mod.rs index c9b829e..ed4e309 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -9,13 +9,11 @@ pub use discovery::{BoundClient as BoundDiscoveryClient, Client as DiscoveryClie mod transaction; pub(crate) use transaction::*; -mod params; -pub(crate) use params::{ActionParams, Method}; - mod response; pub(crate) use response::Response; use crate::api::{ConfiguredDevice, DevicePath, FallibleDeviceType, ServerInfo, TypedDevice}; +use crate::params::{Action, ActionParams, Method}; use crate::response::ValueResponse; use crate::{ASCOMError, ASCOMResult}; use eyre::ContextCompat; @@ -38,15 +36,12 @@ pub(crate) struct RawDeviceClient { } impl RawDeviceClient { - pub(crate) async fn exec_action( - &self, - action_params: ActionParams, - ) -> ASCOMResult + pub(crate) async fn exec_action(&self, action: impl Action) -> ASCOMResult where ASCOMResult: Response, { self.inner - .request::>(action_params) + .request::>(action.into_parts()) .await .unwrap_or_else(|err| Err(ASCOMError::unspecified(err))) } @@ -85,7 +80,7 @@ impl RawClient { action, method, params, - }: ActionParams, + }: ActionParams, ) -> eyre::Result { let request_transaction = RequestTransaction::new(self.client_id); @@ -97,7 +92,7 @@ impl RawClient { ); async move { - tracing::debug!(?method, ?params, base_url = %self.base_url, "Sending request"); + tracing::debug!(?method, params = ?serdebug::debug(¶ms), base_url = %self.base_url, "Sending request"); let mut request = REQWEST.request(method.into(), self.base_url.join(action)?); diff --git a/src/lib.rs b/src/lib.rs index 39e2bed..c338a91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,6 +371,7 @@ mod either; pub mod discovery; mod errors; +mod params; mod response; /// Utilities for testing Alpaca client and server implementations. diff --git a/src/macros.rs b/src/macros.rs index 523e194..a16a894 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -38,7 +38,57 @@ macro_rules! rpc_trait { ) -> $return_type:ty $default_body:block )* } - ) => { + ) => (paste::paste! { + #[cfg_attr(feature = "client", derive(serde::Serialize), serde(untagged))] + #[allow(non_camel_case_types)] + pub(crate) enum [<$trait_name Action>] { + $( + $method_name { + $( + #[cfg_attr(feature = "client", serde(rename = $param_query))] + $param: $param_ty, + )* + }, + )* + } + + impl $crate::params::Action for [<$trait_name Action>] { + #[cfg(feature = "server")] + fn from_parts(action: &str, params: $crate::server::ActionParams) -> $crate::server::Result> { + Ok(Ok(match (action, params) { + $( + ($method_path, $crate::server::ActionParams::$http_method(params)) => { + #[allow(unused)] + let mut params = params; + $( + let $param = + params.extract($param_query) + $(.map(<$param_via>::into))? + ?; + )* + params.finish_extraction(); + + Self::$method_name { $($param),* } + } + )* + (_, params) => return Ok(Err(params)), + })) + } + + #[cfg(feature = "client")] + fn into_parts(self) -> $crate::params::ActionParams { + let (method, action) = match self { + $(Self::$method_name { .. } => ($crate::params::Method::$http_method, $method_path),)* + }; + + $crate::params::ActionParams { + action, + method, + params: self, + } + } + } + $(# $attr)* #[async_trait::async_trait] #[allow(unused_variables)] @@ -93,31 +143,16 @@ macro_rules! rpc_trait { $($method_name($method_name),)* } - let value = match (action, params) { - $( - ($method_path, $crate::server::ActionParams::$http_method(params)) => { - #[allow(unused_mut)] - let mut params = params; - $( - let $param = - params.extract($param_query) - $(.map(<$param_via>::into))? - ?; - )* - params.finish_extraction(); - - #[allow(deprecated)] - let value = - device - .$method_name($($param),*) - .await - $(.map(<$via>::from))? - ?; - - ResponseRepr::$method_name(value) - } - )* - (action, params) => rpc_trait!(@if_specific $trait_name { + let value = match $crate::params::Action::from_parts(action, params)? { + Ok(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 { return match ::handle_action(device, action, params).await { Ok(value) => Ok($crate::either::Either::Right(value)), Err(mut err) => { @@ -157,28 +192,18 @@ macro_rules! rpc_trait { $( #[allow(non_camel_case_types)] async fn $method_name(& $self $(, $param: $param_ty)*) -> $return_type { - #[derive(Debug, Serialize)] - struct OpaqueParams<$($param),*> { + $self + .exec_action([<$trait_name Action>]::$method_name { $( - #[serde(rename = $param_query)] - $param: $param, + $param, )* - } - - $self - .exec_action($crate::client::ActionParams { - action: $method_path, - method: $crate::client::Method::$http_method, - params: OpaqueParams { - $($param $(: <$param_via>::from($param))?),* - } }) .await $(.map(<$via>::into))? } )* } - }; + }); } pub(crate) use rpc_trait; diff --git a/src/client/params.rs b/src/params.rs similarity index 54% rename from src/client/params.rs rename to src/params.rs index 95515c9..6457a11 100644 --- a/src/client/params.rs +++ b/src/params.rs @@ -18,3 +18,14 @@ pub(crate) struct ActionParams { pub(crate) method: Method, pub(crate) params: T, } + +pub(crate) trait Action: Sized + Send { + #[cfg(feature = "server")] + fn from_parts( + action: &str, + params: crate::server::ActionParams, + ) -> crate::server::Result>; + + #[cfg(feature = "client")] + fn into_parts(self) -> ActionParams; +}