chore: updated schemas

This commit is contained in:
TotallyNot 2025-05-19 20:09:38 +02:00
parent b4ce0c764e
commit 73358b70cc
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
11 changed files with 1628 additions and 100 deletions

4
Cargo.lock generated
View file

@ -2271,7 +2271,7 @@ dependencies = [
[[package]] [[package]]
name = "torn-api" name = "torn-api"
version = "1.1.2" version = "1.2.0"
dependencies = [ dependencies = [
"bon", "bon",
"bytes", "bytes",
@ -2291,7 +2291,7 @@ dependencies = [
[[package]] [[package]]
name = "torn-api-codegen" name = "torn-api-codegen"
version = "0.2.2" version = "0.2.3"
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.2.2" version = "0.2.3"
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 }

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,7 @@
use heck::{ToSnakeCase, ToUpperCamelCase}; use heck::{ToSnakeCase, ToUpperCamelCase};
use indexmap::IndexMap; use indexmap::IndexMap;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote}; use quote::{format_ident, quote, ToTokens};
use syn::Ident; use syn::Ident;
use crate::openapi::r#type::OpenApiType; use crate::openapi::r#type::OpenApiType;
@ -82,6 +82,7 @@ pub struct Property {
pub required: bool, pub required: bool,
pub nullable: bool, pub nullable: bool,
pub r#type: PropertyType, pub r#type: PropertyType,
pub deprecated: bool,
} }
impl Property { impl Property {
@ -105,30 +106,25 @@ impl Property {
name, name,
description, description,
required, required,
deprecated: schema.deprecated,
nullable: false, nullable: false,
}), }),
OpenApiType { OpenApiType {
one_of: Some(types), one_of: Some(types),
.. ..
} => match types.as_slice() { } => match types.as_slice() {
[ [left, OpenApiType {
left,
OpenApiType {
r#type: Some("null"), r#type: Some("null"),
.. ..
}, }] => {
] => {
let mut inner = Self::from_schema(&name, required, left, schemas)?; let mut inner = Self::from_schema(&name, required, left, schemas)?;
inner.nullable = true; inner.nullable = true;
Some(inner) Some(inner)
} }
[ [left @ .., OpenApiType {
left @ ..,
OpenApiType {
r#type: Some("null"), r#type: Some("null"),
.. ..
}, }] => {
] => {
let rest = OpenApiType { let rest = OpenApiType {
one_of: Some(left.to_owned()), one_of: Some(left.to_owned()),
..schema.clone() ..schema.clone()
@ -141,9 +137,10 @@ impl Property {
let r#enum = Enum::from_one_of(&name.to_upper_camel_case(), cases)?; let r#enum = Enum::from_one_of(&name.to_upper_camel_case(), cases)?;
Some(Self { Some(Self {
name, name,
description: None, description,
required, required,
nullable: false, nullable: false,
deprecated: schema.deprecated,
r#type: PropertyType::Enum(r#enum), r#type: PropertyType::Enum(r#enum),
}) })
} }
@ -155,9 +152,10 @@ impl Property {
let composite = Object::from_all_of(&name.to_upper_camel_case(), types, schemas)?; let composite = Object::from_all_of(&name.to_upper_camel_case(), types, schemas)?;
Some(Self { Some(Self {
name, name,
description: None, description,
required, required,
nullable: false, nullable: false,
deprecated: schema.deprecated,
r#type: PropertyType::Nested(Box::new(composite)), r#type: PropertyType::Nested(Box::new(composite)),
}) })
} }
@ -173,6 +171,7 @@ impl Property {
name, name,
description, description,
required, required,
deprecated: schema.deprecated,
nullable: false, nullable: false,
}), }),
OpenApiType { OpenApiType {
@ -183,6 +182,7 @@ impl Property {
description, description,
r#type: PropertyType::Ref((*path).to_owned()), r#type: PropertyType::Ref((*path).to_owned()),
required, required,
deprecated: schema.deprecated,
nullable: false, nullable: false,
}), }),
OpenApiType { OpenApiType {
@ -197,6 +197,7 @@ impl Property {
description, description,
required, required,
nullable: false, nullable: false,
deprecated: schema.deprecated,
r#type: PropertyType::Array(Box::new(inner.r#type)), r#type: PropertyType::Array(Box::new(inner.r#type)),
}) })
} }
@ -217,6 +218,7 @@ impl Property {
description, description,
required, required,
nullable: false, nullable: false,
deprecated: schema.deprecated,
r#type: PropertyType::Primitive(prim), r#type: PropertyType::Primitive(prim),
}) })
} }
@ -245,8 +247,17 @@ impl Property {
ty_inner ty_inner
}; };
let deprecated = self.deprecated.then(|| {
let note = self.description.as_ref().map(|d| quote! { note = #d });
quote! {
#[deprecated(#note)]
}
});
Some(quote! { Some(quote! {
#desc #desc
#deprecated
#serde_attr #serde_attr
pub #name: #ty pub #name: #ty
}) })

View file

@ -1,4 +1,4 @@
use std::{fmt::Write, ops::Deref}; use std::fmt::Write;
use heck::{ToSnakeCase, ToUpperCamelCase}; use heck::{ToSnakeCase, ToUpperCamelCase};
use indexmap::IndexMap; use indexmap::IndexMap;
@ -40,7 +40,7 @@ pub struct Path {
pub segments: Vec<PathSegment>, pub segments: Vec<PathSegment>,
pub name: String, pub name: String,
pub summary: Option<String>, pub summary: Option<String>,
pub description: String, pub description: Option<String>,
pub parameters: Vec<PathParameter>, pub parameters: Vec<PathParameter>,
pub response: PathResponse, pub response: PathResponse,
} }
@ -63,7 +63,7 @@ impl Path {
} }
let summary = schema.get.summary.as_deref().map(ToOwned::to_owned); let summary = schema.get.summary.as_deref().map(ToOwned::to_owned);
let description = schema.get.description.deref().to_owned(); let description = schema.get.description.as_deref().map(ToOwned::to_owned);
let mut params = Vec::with_capacity(schema.get.parameters.len()); let mut params = Vec::with_capacity(schema.get.parameters.len());
for parameter in &schema.get.parameters { for parameter in &schema.get.parameters {
@ -377,7 +377,23 @@ impl Path {
} }
}; };
let doc = match (&self.summary, &self.description) {
(Some(summary), Some(description)) => {
Some(format!("{summary}\n\n# Description\n{description}"))
}
(Some(summary), None) => Some(summary.clone()),
(None, Some(description)) => Some(format!("# Description\n{description}")),
(None, None) => None,
};
let doc = doc.map(|d| {
quote! {
#[doc = #d]
}
});
Some(quote! { Some(quote! {
#doc
pub async fn #fn_name<S>( pub async fn #fn_name<S>(
self, self,
#(#extra_args)* #(#extra_args)*
@ -487,7 +503,23 @@ impl Path {
quote! { #(#disc_ty),* } quote! { #(#disc_ty),* }
}; };
let doc = match (&self.summary, &self.description) {
(Some(summary), Some(description)) => {
Some(format!("{summary}\n\n# Description\n{description}"))
}
(Some(summary), None) => Some(summary.clone()),
(None, Some(description)) => Some(format!("# Description\n{description}")),
(None, None) => None,
};
let doc = doc.map(|d| {
quote! {
#[doc = #d]
}
});
Some(quote! { Some(quote! {
#doc
pub fn #fn_name<S, I, B>( pub fn #fn_name<S, I, B>(
self, self,
ids: I, ids: I,

View file

@ -50,6 +50,7 @@ impl Scope {
} }
Some(quote! { Some(quote! {
#[allow(dead_code)]
pub struct #name<E>(E) pub struct #name<E>(E)
where where
E: crate::executor::Executor; E: crate::executor::Executor;
@ -65,6 +66,7 @@ impl Scope {
#(#functions)* #(#functions)*
} }
#[allow(dead_code)]
pub struct #bulk_name<E> where pub struct #bulk_name<E> where
E: crate::executor::BulkExecutor, E: crate::executor::BulkExecutor,
{ {

View file

@ -63,7 +63,7 @@ where
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct OpenApiPathBody<'a> { pub struct OpenApiPathBody<'a> {
pub summary: Option<Cow<'a, str>>, pub summary: Option<Cow<'a, str>>,
pub description: Cow<'a, str>, pub description: Option<Cow<'a, str>>,
#[serde(borrow, default)] #[serde(borrow, default)]
pub parameters: Vec<OpenApiPathParameter<'a>>, pub parameters: Vec<OpenApiPathParameter<'a>>,
#[serde( #[serde(

View file

@ -1,6 +1,6 @@
[package] [package]
name = "torn-api" name = "torn-api"
version = "1.1.2" version = "1.2.0"
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 }
@ -36,7 +36,7 @@ futures = { version = "0.3", default-features = false, features = [
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
[build-dependencies] [build-dependencies]
torn-api-codegen = { path = "../torn-api-codegen", version = "0.2.1" } torn-api-codegen = { path = "../torn-api-codegen", version = "0.2.3" }
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

@ -62,14 +62,14 @@ pub trait BulkExecutor: Sized {
fn execute<R>( fn execute<R>(
self, self,
requests: impl IntoIterator<Item = R>, requests: impl IntoIterator<Item = R>,
) -> impl Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> ) -> impl Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
where where
R: IntoRequest; R: IntoRequest;
fn fetch_many<R>( fn fetch_many<R>(
self, self,
requests: impl IntoIterator<Item = R>, requests: impl IntoIterator<Item = R>,
) -> impl Stream<Item = (R::Discriminant, Result<R::Response, Self::Error>)> ) -> impl Stream<Item = (R::Discriminant, Result<R::Response, Self::Error>)> + Unpin
where where
R: IntoRequest, R: IntoRequest,
{ {
@ -173,7 +173,7 @@ pub trait BulkExecutorExt: BulkExecutor + Sized {
} }
#[cfg(feature = "scopes")] #[cfg(feature = "scopes")]
impl<'e, T> BulkExecutorExt for T impl<T> BulkExecutorExt for T
where where
T: BulkExecutor + Sized, T: BulkExecutor + Sized,
{ {

View file

@ -28,7 +28,7 @@ pub struct ApiResponse {
} }
pub trait IntoRequest: Send { pub trait IntoRequest: Send {
type Discriminant: Send; type Discriminant: Send + 'static;
type Response: for<'de> serde::Deserialize<'de> + Send; type Response: for<'de> serde::Deserialize<'de> + Send;
fn into_request(self) -> (Self::Discriminant, ApiRequest); fn into_request(self) -> (Self::Discriminant, ApiRequest);
} }

View file

@ -569,17 +569,16 @@ where
fn execute<R>( fn execute<R>(
self, self,
requests: impl IntoIterator<Item = R>, requests: impl IntoIterator<Item = R>,
) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> ) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
where where
R: torn_api::request::IntoRequest, R: torn_api::request::IntoRequest,
{ {
let requests: Vec<_> = requests.into_iter().map(|r| r.into_request()).collect();
self.pool self.pool
.execute_bulk_requests( .execute_bulk_requests(self.selector.clone(), requests)
self.selector.clone(),
requests.into_iter().map(|r| r.into_request()),
)
.into_stream() .into_stream()
.flatten() .flatten()
.boxed()
} }
} }
@ -640,22 +639,23 @@ where
fn execute<R>( fn execute<R>(
self, self,
requests: impl IntoIterator<Item = R>, requests: impl IntoIterator<Item = R>,
) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> ) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
where where
R: torn_api::request::IntoRequest, R: torn_api::request::IntoRequest,
{ {
let requests: Vec<_> = requests.into_iter().map(|r| r.into_request()).collect();
StreamExt::map( StreamExt::map(
futures::stream::iter(requests).throttle(self.distance), futures::stream::iter(requests).throttle(self.distance),
move |r| { move |(d, request)| {
let this = self.clone(); let this = self.clone();
async move { async move {
let (d, request) = r.into_request();
let result = this.execute_request(request).await; let result = this.execute_request(request).await;
(d, result) (d, result)
} }
}, },
) )
.buffer_unordered(25) .buffer_unordered(25)
.boxed()
} }
} }