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]]
name = "torn-api"
version = "1.0.2"
version = "1.0.3"
dependencies = [
"bon",
"bytes",
@ -2290,7 +2290,7 @@ dependencies = [
[[package]]
name = "torn-api-codegen"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"heck",
"indexmap",

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,12 +1,7 @@
use bon::Builder;
use bytes::Bytes;
use http::StatusCode;
use crate::{
executor::Executor,
models::{FactionChainsResponse, FactionId},
};
#[cfg(feature = "requests")]
pub mod models;
#[derive(Default)]
@ -40,65 +35,5 @@ pub trait IntoRequest: Send {
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)]
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();
}
}
mod test {}