feat(core): allow optionally disabling expensive codegen

This commit is contained in:
TotallyNot 2025-04-27 15:23:52 +02:00
parent 26043ac318
commit 4dd4fd37d4
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
7 changed files with 32 additions and 84 deletions

4
Cargo.lock generated
View file

@ -2271,7 +2271,7 @@ dependencies = [
[[package]] [[package]]
name = "torn-api" name = "torn-api"
version = "1.0.2" version = "1.0.3"
dependencies = [ dependencies = [
"bon", "bon",
"bytes", "bytes",
@ -2290,7 +2290,7 @@ dependencies = [
[[package]] [[package]]
name = "torn-api-codegen" name = "torn-api-codegen"
version = "0.1.4" version = "0.1.5"
dependencies = [ dependencies = [
"heck", "heck",
"indexmap", "indexmap",

View file

@ -1,7 +1,7 @@
[package] [package]
name = "torn-api-codegen" name = "torn-api-codegen"
authors = ["Pyrit [2111649]"] authors = ["Pyrit [2111649]"]
version = "0.1.4" version = "0.1.5"
edition = "2021" edition = "2021"
description = "Contains the v2 torn API model descriptions and codegen for the bindings" description = "Contains the v2 torn API model descriptions and codegen for the bindings"
license-file = { workspace = true } license-file = { workspace = true }

View file

@ -166,14 +166,14 @@ impl Path {
quote! { quote! {
crate::request::models::#path::#ty_name crate::request::models::#path::#ty_name
}, },
Some(quote! { #[builder(into)] }), Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
) )
} else { } else {
( (
quote! { quote! {
crate::parameters::#ty_name crate::parameters::#ty_name
}, },
Some(quote! { #[builder(into)]}), Some(quote! { #[cfg_attr(feature = "builder", builder(into))]}),
) )
} }
} }
@ -197,7 +197,7 @@ impl Path {
quote! { quote! {
crate::request::models::#path::#ty_name crate::request::models::#path::#ty_name
}, },
Some(quote! { #[builder(into)] }), Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
) )
} }
}; };
@ -210,7 +210,7 @@ impl Path {
discriminant_val.push(quote! { self.#name }); discriminant_val.push(quote! { self.#name });
let path_name = format_ident!("{}", param.value); let path_name = format_ident!("{}", param.value);
start_fields.push(quote! { start_fields.push(quote! {
#[builder(start_fn)] #[cfg_attr(feature = "builder", builder(start_fn))]
#builder_param #builder_param
pub #name: #ty pub #name: #ty
}); });
@ -273,8 +273,9 @@ impl Path {
Some(quote! { Some(quote! {
#ns #ns
#[derive(Debug, Clone, bon::Builder)] #[cfg_attr(feature = "builder", derive(bon::Builder))]
#[builder(state_mod(vis = "pub(crate)"), on(String, into))] #[derive(Debug, Clone)]
#[cfg_attr(feature = "builder", builder(state_mod(vis = "pub(crate)"), on(String, into)))]
pub struct #name { pub struct #name {
#(#start_fields),* #(#start_fields),*
} }

View file

@ -1,17 +1,24 @@
[package] [package]
name = "torn-api" name = "torn-api"
version = "1.0.2" version = "1.0.3"
edition = "2021" edition = "2021"
description = "Auto-generated bindings for the v2 torn api" description = "Auto-generated bindings for the v2 torn api"
license-file = { workspace = true } license-file = { workspace = true }
repository = { workspace = true } repository = { workspace = true }
homepage = { workspace = true } homepage = { workspace = true }
[features]
default = ["scopes", "requests", "builder", "models"]
scopes = ["builder"]
builder = ["requests", "dep:bon"]
requests = ["models"]
models = ["dep:serde_repr"]
[dependencies] [dependencies]
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
serde_repr = "0.1" serde_repr = { version = "0.1", optional = true }
serde_json = { workspace = true } serde_json = { workspace = true }
bon = "3.6" bon = { version = "3.6", optional = true }
bytes = "1" bytes = "1"
http = "1" http = "1"
reqwest = { version = "0.12", default-features = false, features = [ reqwest = { version = "0.12", default-features = false, features = [
@ -25,7 +32,7 @@ thiserror = "2"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
[build-dependencies] [build-dependencies]
torn-api-codegen = { path = "../torn-api-codegen", version = "0.1.1" } torn-api-codegen = { path = "../torn-api-codegen", version = "0.1.5" }
syn = { workspace = true, features = ["parsing"] } syn = { workspace = true, features = ["parsing"] }
proc-macro2 = { workspace = true } proc-macro2 = { workspace = true }
prettyplease = "0.2" prettyplease = "0.2"

View file

@ -3,10 +3,9 @@ use std::future::Future;
use http::{header::AUTHORIZATION, HeaderMap, HeaderValue}; use http::{header::AUTHORIZATION, HeaderMap, HeaderValue};
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::request::{ApiResponse, IntoRequest};
request::{ApiResponse, IntoRequest}, #[cfg(feature = "scopes")]
scopes::{FactionScope, ForumScope, MarketScope, RacingScope, TornScope, UserScope}, use crate::scopes::{FactionScope, ForumScope, MarketScope, RacingScope, TornScope, UserScope};
};
pub trait Executor { pub trait Executor {
type Error: From<serde_json::Error> + From<crate::ApiError> + Send; type Error: From<serde_json::Error> + From<crate::ApiError> + Send;
@ -73,6 +72,7 @@ impl ReqwestClient {
} }
} }
#[cfg(feature = "scopes")]
pub trait ExecutorExt: Executor + Sized { pub trait ExecutorExt: Executor + Sized {
fn user(&self) -> UserScope<'_, Self>; fn user(&self) -> UserScope<'_, Self>;
@ -87,6 +87,7 @@ pub trait ExecutorExt: Executor + Sized {
fn forum(&self) -> ForumScope<'_, Self>; fn forum(&self) -> ForumScope<'_, Self>;
} }
#[cfg(feature = "scopes")]
impl<T> ExecutorExt for T impl<T> ExecutorExt for T
where where
T: Executor + Sized, T: Executor + Sized,
@ -144,6 +145,7 @@ mod test {
use super::*; use super::*;
#[cfg(feature = "scopes")]
#[tokio::test] #[tokio::test]
async fn api_error() { async fn api_error() {
let client = test_client().await; let client = test_client().await;

View file

@ -1,9 +1,12 @@
use thiserror::Error; use thiserror::Error;
pub mod executor; pub mod executor;
#[cfg(feature = "models")]
pub mod models; pub mod models;
#[cfg(feature = "requests")]
pub mod parameters; pub mod parameters;
pub mod request; pub mod request;
#[cfg(feature = "scopes")]
pub mod scopes; pub mod scopes;
#[derive(Debug, Error, Clone, PartialEq, Eq)] #[derive(Debug, Error, Clone, PartialEq, Eq)]

View file

@ -1,12 +1,7 @@
use bon::Builder;
use bytes::Bytes; use bytes::Bytes;
use http::StatusCode; use http::StatusCode;
use crate::{ #[cfg(feature = "requests")]
executor::Executor,
models::{FactionChainsResponse, FactionId},
};
pub mod models; pub mod models;
#[derive(Default)] #[derive(Default)]
@ -40,65 +35,5 @@ pub trait IntoRequest: Send {
fn into_request(self) -> ApiRequest<Self::Discriminant>; fn into_request(self) -> ApiRequest<Self::Discriminant>;
} }
pub struct FactionScope<'e, E>(&'e E)
where
E: Executor;
impl<E> FactionScope<'_, E>
where
E: Executor,
{
pub async fn chains_for_id<S>(
&self,
id: FactionId,
builder: impl FnOnce(
FactionChainsRequestBuilder<faction_chains_request_builder::Empty>,
) -> FactionChainsRequestBuilder<S>,
) -> Result<FactionChainsResponse, E::Error>
where
S: faction_chains_request_builder::IsComplete,
{
let r = builder(FactionChainsRequest::with_id(id)).build();
self.0.fetch(r).await
}
}
#[derive(Builder)]
#[builder(start_fn = with_id)]
pub struct FactionChainsRequest {
#[builder(start_fn)]
pub id: FactionId,
pub limit: Option<usize>,
}
impl IntoRequest for FactionChainsRequest {
type Discriminant = FactionId;
type Response = FactionChainsResponse;
fn into_request(self) -> ApiRequest<Self::Discriminant> {
ApiRequest {
disriminant: self.id,
path: format!("/faction/{}/chains", self.id),
parameters: self
.limit
.into_iter()
.map(|l| ("limit", l.to_string()))
.collect(),
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {}
use crate::executor::ReqwestClient;
use super::*;
#[tokio::test]
async fn test_request() {
let client = ReqwestClient::new("nAYRXaoqzBAGalWt");
let r = models::TornItemsForIdsRequest::builder("1".to_owned()).build();
client.fetch(r).await.unwrap();
}
}