chore(codegen): updated spec and improved builder ergonomics
This commit is contained in:
parent
84d7c7586b
commit
e5e5d327a2
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -1228,7 +1228,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "torn-api-codegen"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"indexmap",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "torn-api-codegen"
|
||||
authors = ["Pyrit [2111649]"]
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2024"
|
||||
description = "Contains the v2 torn API model descriptions and codegen for the bindings"
|
||||
license-file = { workspace = true }
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -15,26 +15,51 @@ pub enum EnumRepr {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EnumVariantTupleValue {
|
||||
Ref(String),
|
||||
Ref { ty_name: String },
|
||||
ArrayOfRefs { ty_name: String },
|
||||
}
|
||||
|
||||
impl EnumVariantTupleValue {
|
||||
pub fn from_schema(schema: &OpenApiType) -> Option<Self> {
|
||||
if let OpenApiType {
|
||||
ref_path: Some(path),
|
||||
..
|
||||
} = schema
|
||||
{
|
||||
Some(Self::Ref((*path).to_owned()))
|
||||
} else {
|
||||
None
|
||||
match schema {
|
||||
OpenApiType {
|
||||
ref_path: Some(path),
|
||||
..
|
||||
} => Some(Self::Ref {
|
||||
ty_name: path.strip_prefix("#/components/schemas/")?.to_owned(),
|
||||
}),
|
||||
OpenApiType {
|
||||
r#type: Some("array"),
|
||||
items: Some(items),
|
||||
..
|
||||
} => {
|
||||
let OpenApiType {
|
||||
ref_path: Some(path),
|
||||
..
|
||||
} = items.as_ref()
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
Some(Self::ArrayOfRefs {
|
||||
ty_name: path.strip_prefix("#/components/schemas/")?.to_owned(),
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> Option<&str> {
|
||||
let Self::Ref(path) = self;
|
||||
pub fn type_name(&self) -> &str {
|
||||
match self {
|
||||
Self::Ref { ty_name } => ty_name,
|
||||
Self::ArrayOfRefs { ty_name } => ty_name,
|
||||
}
|
||||
}
|
||||
|
||||
path.strip_prefix("#/components/schemas/")
|
||||
pub fn name(&self) -> String {
|
||||
match self {
|
||||
Self::Ref { ty_name } => ty_name.clone(),
|
||||
Self::ArrayOfRefs { ty_name } => format!("{ty_name}s"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,7 +128,7 @@ impl EnumVariant {
|
|||
let mut val_tys = Vec::with_capacity(values.len());
|
||||
|
||||
for value in values {
|
||||
let ty_name = value.name()?;
|
||||
let ty_name = value.type_name();
|
||||
let ty_name = format_ident!("{ty_name}");
|
||||
|
||||
val_tys.push(quote! {
|
||||
|
@ -216,7 +241,7 @@ impl Enum {
|
|||
|
||||
for schema in schemas {
|
||||
let value = EnumVariantTupleValue::from_schema(schema)?;
|
||||
let name = value.name()?.to_owned();
|
||||
let name = value.name();
|
||||
|
||||
result.variants.push(EnumVariant {
|
||||
name,
|
||||
|
@ -253,11 +278,15 @@ impl Enum {
|
|||
|
||||
let mut derives = vec![];
|
||||
|
||||
if self.copy {
|
||||
derives.extend_from_slice(&["Copy", "Hash"]);
|
||||
if self.repr.is_some() {
|
||||
derives.push(quote! { serde_repr::Deserialize_repr });
|
||||
} else {
|
||||
derives.push(quote! { serde::Deserialize });
|
||||
}
|
||||
|
||||
let derives = derives.into_iter().map(|d| format_ident!("{d}"));
|
||||
if self.copy {
|
||||
derives.push(quote! { Copy, Hash });
|
||||
}
|
||||
|
||||
let serde_attr = self.untagged.then(|| {
|
||||
quote! {
|
||||
|
@ -279,7 +308,7 @@ impl Enum {
|
|||
|
||||
Some(quote! {
|
||||
#desc
|
||||
#[derive(Debug, Clone, PartialEq, serde::Deserialize, #(#derives),*)]
|
||||
#[derive(Debug, Clone, PartialEq, #(#derives),*)]
|
||||
#serde_attr
|
||||
pub enum #name {
|
||||
#(#variants),*
|
||||
|
|
|
@ -280,7 +280,7 @@ impl Object {
|
|||
|
||||
for (prop_name, prop) in props {
|
||||
// HACK: This will cause a duplicate key otherwise
|
||||
if *prop_name == "itemDetails" {
|
||||
if ["itemDetails", "sci-fi", "non-attackers", "co-leader_id"].contains(prop_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -217,6 +217,12 @@ impl Parameter {
|
|||
Self(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i32> for #name {
|
||||
fn from(inner: i32) -> Self {
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -317,6 +323,14 @@ The default value [Self::{}](self::{}#variant.{})"#,
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for #name where T: IntoIterator<Item = #inner_ty> {
|
||||
fn from(value: T) -> #name {
|
||||
let items = value.into_iter().collect();
|
||||
|
||||
Self(items)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Some(code)
|
||||
|
|
|
@ -154,7 +154,7 @@ impl Path {
|
|||
PathParameter::Component(param) => (false, param),
|
||||
};
|
||||
|
||||
let ty = match ¶m.r#type {
|
||||
let (ty, builder_param) = match ¶m.r#type {
|
||||
ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
|
||||
let ty_name = format_ident!("{}", param.name);
|
||||
|
||||
|
@ -162,31 +162,43 @@ impl Path {
|
|||
ns.push_element(param.codegen()?);
|
||||
let path = ns.get_ident();
|
||||
|
||||
quote! {
|
||||
crate::request::models::#path::#ty_name
|
||||
}
|
||||
(
|
||||
quote! {
|
||||
crate::request::models::#path::#ty_name
|
||||
},
|
||||
Some(quote! { #[builder(into)] }),
|
||||
)
|
||||
} else {
|
||||
quote! {
|
||||
crate::parameters::#ty_name
|
||||
}
|
||||
(
|
||||
quote! {
|
||||
crate::parameters::#ty_name
|
||||
},
|
||||
Some(quote! { #[builder(into)]}),
|
||||
)
|
||||
}
|
||||
}
|
||||
ParameterType::String => quote! { String },
|
||||
ParameterType::Boolean => quote! { bool },
|
||||
ParameterType::String => (quote! { String }, None),
|
||||
ParameterType::Boolean => (quote! { bool }, None),
|
||||
ParameterType::Schema { type_name } => {
|
||||
let ty_name = format_ident!("{}", type_name);
|
||||
|
||||
quote! {
|
||||
crate::models::#ty_name
|
||||
}
|
||||
(
|
||||
quote! {
|
||||
crate::models::#ty_name
|
||||
},
|
||||
None,
|
||||
)
|
||||
}
|
||||
ParameterType::Array { .. } => {
|
||||
ns.push_element(param.codegen()?);
|
||||
let ty_name = param.r#type.codegen_type_name(¶m.name);
|
||||
let path = ns.get_ident();
|
||||
quote! {
|
||||
crate::request::models::#path::#ty_name
|
||||
}
|
||||
(
|
||||
quote! {
|
||||
crate::request::models::#path::#ty_name
|
||||
},
|
||||
Some(quote! { #[builder(into)] }),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -199,6 +211,7 @@ impl Path {
|
|||
let path_name = format_ident!("{}", param.value);
|
||||
start_fields.push(quote! {
|
||||
#[builder(start_fn)]
|
||||
#builder_param
|
||||
pub #name: #ty
|
||||
});
|
||||
fmt_val.push(quote! {
|
||||
|
@ -218,6 +231,7 @@ impl Path {
|
|||
};
|
||||
|
||||
fields.push(quote! {
|
||||
#builder_param
|
||||
pub #name: #ty
|
||||
});
|
||||
}
|
||||
|
@ -260,7 +274,7 @@ impl Path {
|
|||
#ns
|
||||
|
||||
#[derive(Debug, Clone, bon::Builder)]
|
||||
#[builder(state_mod(vis = "pub(crate)"))]
|
||||
#[builder(state_mod(vis = "pub(crate)"), on(String, into))]
|
||||
pub struct #name {
|
||||
#(#start_fields),*
|
||||
}
|
||||
|
|
|
@ -6,7 +6,13 @@ pub(super) mod test {
|
|||
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use crate::executor::ReqwestClient;
|
||||
use crate::{
|
||||
executor::{ExecutorExt, ReqwestClient},
|
||||
models::{
|
||||
AttackCode, FactionSelectionName, PersonalStatsCategoryEnum, PersonalStatsStatName,
|
||||
UserListEnum,
|
||||
},
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
@ -54,6 +60,21 @@ pub(super) mod test {
|
|||
rx.recv().await.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn faction() {
|
||||
let client = test_client().await;
|
||||
|
||||
let r = client
|
||||
.faction()
|
||||
.for_selections(|b| {
|
||||
b.selections([FactionSelectionName::Basic, FactionSelectionName::Balance])
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
r.faction_basic_response().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn faction_applications() {
|
||||
let client = test_client().await;
|
||||
|
@ -193,7 +214,7 @@ pub(super) mod test {
|
|||
let faction_scope = FactionScope(&client);
|
||||
|
||||
faction_scope
|
||||
.crime_for_id(468347.into(), |b| b)
|
||||
.crime_for_crime_id(468347.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -277,7 +298,7 @@ pub(super) mod test {
|
|||
let faction_scope = FactionScope(&client);
|
||||
|
||||
faction_scope
|
||||
.rankedwarreport_for_id(24424.into(), |b| b)
|
||||
.rankedwarreport_for_ranked_war_id(24424.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -337,7 +358,7 @@ pub(super) mod test {
|
|||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn lookup() {
|
||||
async fn faction_lookup() {
|
||||
let client = test_client().await;
|
||||
|
||||
let faction_scope = FactionScope(&client);
|
||||
|
@ -520,7 +541,7 @@ pub(super) mod test {
|
|||
let racing_scope = TornScope(&client);
|
||||
|
||||
racing_scope
|
||||
.attacklog(|b| b.log("ec987a60a22155cbfb7c1625cbb2092f".to_owned()))
|
||||
.attacklog(|b| b.log(AttackCode("ec987a60a22155cbfb7c1625cbb2092f".to_owned())))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -661,7 +682,7 @@ pub(super) mod test {
|
|||
let torn_scope = TornScope(&client);
|
||||
|
||||
torn_scope
|
||||
.subcrimes_for_crime_id("3".into(), |b| b)
|
||||
.subcrimes_for_crime_id(3.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -669,7 +690,6 @@ pub(super) mod test {
|
|||
#[tokio::test]
|
||||
async fn torn_lookup() {
|
||||
let client = test_client().await;
|
||||
|
||||
let torn_scope = TornScope(&client);
|
||||
|
||||
torn_scope.lookup(|b| b).await.unwrap();
|
||||
|
@ -683,4 +703,255 @@ pub(super) mod test {
|
|||
|
||||
torn_scope.timestamp(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_attacks() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().attacks(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_attacksfull() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().attacksfull(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_bounties() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().bounties(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_bounties_for_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.bounties_for_id(986228.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_calendar() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().calendar(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_crimes_for_crime_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.crimes_for_crime_id(10.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_enlisted_cars() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().enlistedcars(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_factionbalance() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().factionbalance(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumfeed() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().forumfeed(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumfriends() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().forumfriends(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumposts() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().forumposts(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumposts_for_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.forumposts_for_id(1.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumsubscribedthreads() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().forumsubscribedthreads(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumthreads() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().forumthreads(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_forumthreads_for_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.forumthreads_for_id(1.into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_hof() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().hof(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_hof_for_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().hof_for_id(1.into(), |b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_itemmarket() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().itemmarket(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_jobranks() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().jobranks(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_list() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.list(|b| b.cat(UserListEnum::Friends))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_organizedcrime() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().organizedcrime(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_personalstats() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats(|b| {
|
||||
b.stat([PersonalStatsStatName::Piercinghits])
|
||||
.timestamp(1737661955)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats(|b| b.cat(PersonalStatsCategoryEnum::All))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats(|b| b.cat(PersonalStatsCategoryEnum::Popular))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats(|b| b.cat(PersonalStatsCategoryEnum::Drugs))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats(|b| b.stat([PersonalStatsStatName::Piercinghits]))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_for_id() {
|
||||
let client = test_client().await;
|
||||
|
||||
client
|
||||
.user()
|
||||
.personalstats_for_id(1.into(), |b| b.cat(PersonalStatsCategoryEnum::All))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_races() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().races(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_revives() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().revives(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_revivesfull() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().revives_full(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_lookup() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().lookup(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_timestamp() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().attacks(|b| b).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue