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

View File

@@ -39,3 +39,6 @@ cbc = { version = "0.1.2", features = ["alloc"] }
aes = "0.8.4"
cipher = { version = "0.4.4", features = ["block-padding"] }
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)]
pub enum APIMethod {
GetGameChargeApi,
@@ -45,6 +47,36 @@ pub enum APIMethod {
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)]
mod _test {
use crate::title::{MaiVersionExt, Sdgb1_50, methods::APIMethod};

View File

@@ -1,6 +1,6 @@
use std::fmt::Display;
use crate::title::methods::APIMethod;
use crate::title::methods::{APIExt, APIMethod};
pub mod encryption;
pub mod methods;
@@ -107,6 +107,14 @@ pub trait MaiVersionExt: MaiVersion {
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;