enhance: typed request, thanks to crabtime

This commit is contained in:
mokurin000
2025-07-31 21:42:38 +08:00
parent b408d1ba51
commit b0b8cea00e
4 changed files with 98 additions and 1 deletions

54
Cargo.lock generated
View File

@@ -457,6 +457,26 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "crabtime"
version = "1.1.3"
source = "git+https://github.com/wdanilo/crabtime.git?rev=2ed856f5#2ed856f5f8b85a1e81598d64a36305a1d3f17d01"
dependencies = [
"crabtime-internal",
]
[[package]]
name = "crabtime-internal"
version = "1.1.3"
source = "git+https://github.com/wdanilo/crabtime.git?rev=2ed856f5#2ed856f5f8b85a1e81598d64a36305a1d3f17d01"
dependencies = [
"proc-macro2",
"quote",
"rustc_version",
"syn 2.0.104",
"toml",
]
[[package]] [[package]]
name = "crc32fast" name = "crc32fast"
version = "1.5.0" version = "1.5.0"
@@ -1433,6 +1453,7 @@ dependencies = [
"chrono", "chrono",
"cipher", "cipher",
"compio", "compio",
"crabtime",
"digest", "digest",
"flate2", "flate2",
"hmac-sha256", "hmac-sha256",
@@ -1503,6 +1524,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_spanned"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"
@@ -1755,11 +1785,26 @@ dependencies = [
"syn 2.0.104", "syn 2.0.104",
] ]
[[package]]
name = "toml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]] [[package]]
name = "toml_datetime" name = "toml_datetime"
version = "0.6.11" version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "toml_edit" name = "toml_edit"
@@ -1768,10 +1813,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"serde",
"serde_spanned",
"toml_datetime", "toml_datetime",
"toml_write",
"winnow", "winnow",
] ]
[[package]]
name = "toml_write"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.41" version = "0.1.41"

View File

@@ -39,3 +39,6 @@ cbc = { version = "0.1.2", features = ["alloc"] }
aes = "0.8.4" aes = "0.8.4"
cipher = { version = "0.4.4", features = ["block-padding"] } cipher = { version = "0.4.4", features = ["block-padding"] }
bincode = { version = "2.0.1", optional = true } bincode = { version = "2.0.1", optional = true }
# magic macro
crabtime = { git = "https://github.com/wdanilo/crabtime.git", rev = "2ed856f5" }

View File

@@ -1,3 +1,5 @@
use serde::{Deserialize, Serialize};
#[derive(strum::IntoStaticStr)] #[derive(strum::IntoStaticStr)]
pub enum APIMethod { pub enum APIMethod {
GetGameChargeApi, GetGameChargeApi,
@@ -45,6 +47,36 @@ pub enum APIMethod {
UserLogoutApi, UserLogoutApi,
} }
pub trait APIExt {
const METHOD: APIMethod;
type Payload: Serialize + Send + 'static;
type Output: for<'de> Deserialize<'de>;
}
#[crabtime::function]
fn api_implement(api_names: Vec<String>) {
for api_name in api_names {
let api_name = api_name;
crabtime::output!(
pub struct {{api_name}};
impl APIExt for {{api_name}} {
const METHOD: APIMethod = APIMethod::{{api_name}};
type Payload = crate::title::model::{{api_name}};
type Output = crate::title::model::{{api_name}}Resp;
}
);
}
}
api_implement!([
"Ping",
"UserLoginApi",
"UserLogoutApi",
"GetUserDataApi",
"GetUserPreviewApi",
]);
#[cfg(test)] #[cfg(test)]
mod _test { mod _test {
use crate::title::{MaiVersionExt, Sdgb1_50, methods::APIMethod}; use crate::title::{MaiVersionExt, Sdgb1_50, methods::APIMethod};

View File

@@ -1,6 +1,6 @@
use std::fmt::Display; use std::fmt::Display;
use crate::title::methods::APIMethod; use crate::title::methods::{APIExt, APIMethod};
pub mod encryption; pub mod encryption;
pub mod methods; pub mod methods;
@@ -107,6 +107,14 @@ pub trait MaiVersionExt: MaiVersion {
Ok(serde_json::from_slice(&raw_data)?) Ok(serde_json::from_slice(&raw_data)?)
} }
} }
fn request_ext<M: APIExt>(
client: &AsyncClient,
data: M::Payload,
agent_extra: impl Display + Send + 'static,
) -> impl Future<Output = Result<M::Output, ApiError>> {
Self::request(client, M::METHOD, agent_extra, data)
}
} }
pub struct Sdgb1_40; pub struct Sdgb1_40;