diff --git a/Cargo.lock b/Cargo.lock index cb68853..1bd2c78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/torn-api-codegen/Cargo.toml b/torn-api-codegen/Cargo.toml index 9421818..a57803e 100644 --- a/torn-api-codegen/Cargo.toml +++ b/torn-api-codegen/Cargo.toml @@ -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 } diff --git a/torn-api-codegen/src/model/path.rs b/torn-api-codegen/src/model/path.rs index a188314..922622d 100644 --- a/torn-api-codegen/src/model/path.rs +++ b/torn-api-codegen/src/model/path.rs @@ -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),* } diff --git a/torn-api/Cargo.toml b/torn-api/Cargo.toml index b8f0c20..42c80f1 100644 --- a/torn-api/Cargo.toml +++ b/torn-api/Cargo.toml @@ -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" diff --git a/torn-api/src/executor.rs b/torn-api/src/executor.rs index 8b7b2ab..dfd75dd 100644 --- a/torn-api/src/executor.rs +++ b/torn-api/src/executor.rs @@ -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 + From + 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 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; diff --git a/torn-api/src/lib.rs b/torn-api/src/lib.rs index f9aa757..2c37a0f 100644 --- a/torn-api/src/lib.rs +++ b/torn-api/src/lib.rs @@ -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)] diff --git a/torn-api/src/request/mod.rs b/torn-api/src/request/mod.rs index 6334bbb..3f7bfde 100644 --- a/torn-api/src/request/mod.rs +++ b/torn-api/src/request/mod.rs @@ -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; } -pub struct FactionScope<'e, E>(&'e E) -where - E: Executor; - -impl FactionScope<'_, E> -where - E: Executor, -{ - pub async fn chains_for_id( - &self, - id: FactionId, - builder: impl FnOnce( - FactionChainsRequestBuilder, - ) -> FactionChainsRequestBuilder, - ) -> Result - 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, -} - -impl IntoRequest for FactionChainsRequest { - type Discriminant = FactionId; - type Response = FactionChainsResponse; - fn into_request(self) -> ApiRequest { - 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 {}