Compare commits
No commits in common. "main" and "key-pool/1.0.1" have entirely different histories.
main
...
key-pool/1
113
Cargo.lock
generated
113
Cargo.lock
generated
|
@ -55,9 +55,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "async-compression"
|
||||
version = "0.4.23"
|
||||
version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b37fc50485c4f3f736a4fb14199f6d5f5ba008d7f28fe710306c92780f004c07"
|
||||
checksum = "59a194f9d963d8099596278594b3107448656ba73831c9d8c783e613ce86da64"
|
||||
dependencies = [
|
||||
"brotli",
|
||||
"futures-core",
|
||||
|
@ -159,9 +159,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "brotli"
|
||||
version = "8.0.0"
|
||||
version = "7.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf19e729cdbd51af9a397fb9ef8ac8378007b797f8273cfbfdf45dcaa316167b"
|
||||
checksum = "cc97b8f16f944bba54f0433f07e30be199b6dc2bd25937444bbad560bcea29bd"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib",
|
||||
"alloc-stdlib",
|
||||
|
@ -170,9 +170,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "brotli-decompressor"
|
||||
version = "5.0.0"
|
||||
version = "4.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "874bb8112abecc98cbd6d81ea4fa7e94fb9449648c93cc89aa40c81c24d7de03"
|
||||
checksum = "a334ef7c9e23abf0ce748e8cd309037da93e606ad52eb372e4ce327a0dcfbdfd"
|
||||
dependencies = [
|
||||
"alloc-no-stdlib",
|
||||
"alloc-stdlib",
|
||||
|
@ -198,9 +198,9 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
|||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.20"
|
||||
version = "1.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04da6a0d40b948dfc4fa8f5bbf402b0fc1a64a28dbf7d12ffd683550f2c1b63a"
|
||||
checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
|
@ -219,15 +219,14 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
|||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.41"
|
||||
version = "0.4.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
|
||||
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
@ -608,9 +607,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.16"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
|
||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
|
@ -660,9 +659,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.3"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
|
||||
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
|
@ -1253,9 +1252,9 @@ checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
|||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.108"
|
||||
version = "0.9.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e145e1651e858e820e4860f7b9c5e169bc1d8ce1c86043be79fa7b7634821847"
|
||||
checksum = "8288979acd84749c744a9014b4382d42b8f7b2592847b5afb2ed29e5d16ede07"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
|
@ -1367,9 +1366,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
|
||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
@ -1396,9 +1395,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "quinn-proto"
|
||||
version = "0.11.11"
|
||||
version = "0.11.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bcbafbbdbb0f638fe3f35f3c56739f77a8a1d070cb25603226c83339b391472b"
|
||||
checksum = "b820744eb4dc9b57a3398183639c511b5a26d2ed702cedd3febaa1393caa22cc"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"getrandom 0.3.2",
|
||||
|
@ -1416,9 +1415,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "quinn-udp"
|
||||
version = "0.5.12"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee4e529991f949c5e25755532370b8af5d114acae52326361d68d47af64aa842"
|
||||
checksum = "541d0f57c6ec747a90738a52741d3221f7960e8ac2f0ff4b1a63680e033b4ab5"
|
||||
dependencies = [
|
||||
"cfg_aliases",
|
||||
"libc",
|
||||
|
@ -1490,7 +1489,7 @@ version = "0.6.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom 0.2.16",
|
||||
"getrandom 0.2.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1570,7 +1569,7 @@ checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
|
|||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"getrandom 0.2.16",
|
||||
"getrandom 0.2.15",
|
||||
"libc",
|
||||
"untrusted",
|
||||
"windows-sys 0.52.0",
|
||||
|
@ -1610,9 +1609,9 @@ checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
|||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.0.7"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266"
|
||||
checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
|
@ -1782,9 +1781,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.10.9"
|
||||
version = "0.10.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
|
||||
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
|
@ -2081,28 +2080,6 @@ version = "0.11.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.27.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
|
||||
dependencies = [
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.27.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.1"
|
||||
|
@ -2111,9 +2088,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.101"
|
||||
version = "2.0.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf"
|
||||
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -2131,9 +2108,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.13.2"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
||||
checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -2281,9 +2258,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.15"
|
||||
version = "0.7.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df"
|
||||
checksum = "6b9590b93e6fcc1739458317cccd391ad3955e2bde8913edf6f95f9e65a8f034"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
|
@ -2294,12 +2271,10 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "torn-api"
|
||||
version = "1.7.0"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"bon",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"futures",
|
||||
"http",
|
||||
"prettyplease",
|
||||
"proc-macro2",
|
||||
|
@ -2307,7 +2282,6 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_json",
|
||||
"serde_repr",
|
||||
"strum",
|
||||
"syn",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
|
@ -2316,7 +2290,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "torn-api-codegen"
|
||||
version = "0.7.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"indexmap",
|
||||
|
@ -2329,7 +2303,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "torn-key-pool"
|
||||
version = "1.1.3"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
|
@ -2341,7 +2315,6 @@ dependencies = [
|
|||
"sqlx",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
"torn-api",
|
||||
]
|
||||
|
||||
|
@ -2607,9 +2580,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.26.10"
|
||||
version = "0.26.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37493cadf42a2a939ed404698ded7fb378bf301b5011f973361779a3a74f8c93"
|
||||
checksum = "2210b291f7ea53617fbafcc4939f10914214ec15aace5ba62293a668f322c5c9"
|
||||
dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
@ -2962,18 +2935,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.25"
|
||||
version = "0.8.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb"
|
||||
checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.25"
|
||||
version = "0.8.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef"
|
||||
checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
|
@ -4,8 +4,8 @@ members = ["torn-api", "torn-api-codegen", "torn-key-pool"]
|
|||
|
||||
[workspace.package]
|
||||
license-file = "./LICENSE"
|
||||
repository = "https://git.elimination.me/pyrite/torn-api.rs.git"
|
||||
homepage = "https://git.elimination.me/pyrite/torn-api.rs"
|
||||
repository = "https://github.com/TotallyNot/torn-api.rs.git"
|
||||
homepage = "https://github.com/TotallyNot/torn-api.rs.git"
|
||||
|
||||
[workspace.dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[package]
|
||||
name = "torn-api-codegen"
|
||||
authors = ["Pyrit [2111649]"]
|
||||
version = "0.7.0"
|
||||
edition = "2021"
|
||||
version = "0.1.2"
|
||||
edition = "2024"
|
||||
description = "Contains the v2 torn API model descriptions and codegen for the bindings"
|
||||
license-file = { workspace = true }
|
||||
repository = { workspace = true }
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,15 +1,12 @@
|
|||
use heck::{ToSnakeCase, ToUpperCamelCase};
|
||||
use heck::ToUpperCamelCase;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::Ident;
|
||||
|
||||
use crate::openapi::{
|
||||
parameter::OpenApiParameterSchema,
|
||||
r#type::{OpenApiType, OpenApiVariants},
|
||||
};
|
||||
|
||||
use super::{object::PrimitiveType, Model, ResolvedSchema};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum EnumRepr {
|
||||
U8,
|
||||
|
@ -20,12 +17,10 @@ pub enum EnumRepr {
|
|||
pub enum EnumVariantTupleValue {
|
||||
Ref { ty_name: String },
|
||||
ArrayOfRefs { ty_name: String },
|
||||
Primitive(PrimitiveType),
|
||||
Enum { name: String, inner: Enum },
|
||||
}
|
||||
|
||||
impl EnumVariantTupleValue {
|
||||
pub fn from_schema(name: &str, schema: &OpenApiType) -> Option<Self> {
|
||||
pub fn from_schema(schema: &OpenApiType) -> Option<Self> {
|
||||
match schema {
|
||||
OpenApiType {
|
||||
ref_path: Some(path),
|
||||
|
@ -49,66 +44,14 @@ impl EnumVariantTupleValue {
|
|||
ty_name: path.strip_prefix("#/components/schemas/")?.to_owned(),
|
||||
})
|
||||
}
|
||||
OpenApiType {
|
||||
r#type: Some("string"),
|
||||
format: None,
|
||||
r#enum: None,
|
||||
..
|
||||
} => Some(Self::Primitive(PrimitiveType::String)),
|
||||
OpenApiType {
|
||||
r#type: Some("string"),
|
||||
format: None,
|
||||
r#enum: Some(_),
|
||||
..
|
||||
} => {
|
||||
let name = format!("{name}Variant");
|
||||
Some(Self::Enum {
|
||||
inner: Enum::from_schema(&name, schema)?,
|
||||
name,
|
||||
})
|
||||
}
|
||||
OpenApiType {
|
||||
r#type: Some("integer"),
|
||||
format: Some("int64"),
|
||||
..
|
||||
} => Some(Self::Primitive(PrimitiveType::I64)),
|
||||
OpenApiType {
|
||||
r#type: Some("integer"),
|
||||
format: Some("int32"),
|
||||
..
|
||||
} => Some(Self::Primitive(PrimitiveType::I32)),
|
||||
OpenApiType {
|
||||
r#type: Some("number"),
|
||||
format: Some("float") | None,
|
||||
..
|
||||
} => Some(Self::Primitive(PrimitiveType::Float)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_name(&self, ns: &mut EnumNamespace) -> TokenStream {
|
||||
pub fn type_name(&self) -> &str {
|
||||
match self {
|
||||
Self::Ref { ty_name } => {
|
||||
let ty = format_ident!("{ty_name}");
|
||||
quote! { crate::models::#ty }
|
||||
}
|
||||
Self::ArrayOfRefs { ty_name } => {
|
||||
let ty = format_ident!("{ty_name}");
|
||||
quote! { Vec<crate::models::#ty> }
|
||||
}
|
||||
Self::Primitive(PrimitiveType::I64) => quote! { i64 },
|
||||
Self::Primitive(PrimitiveType::I32) => quote! { i32 },
|
||||
Self::Primitive(PrimitiveType::Float) => quote! { f32 },
|
||||
Self::Primitive(PrimitiveType::String) => quote! { String },
|
||||
Self::Primitive(PrimitiveType::DateTime) => quote! { chrono::DateTime<chrono::Utc> },
|
||||
Self::Primitive(PrimitiveType::Bool) => quote! { bool },
|
||||
Self::Enum { name, .. } => {
|
||||
let path = ns.get_ident();
|
||||
let ty_name = format_ident!("{name}");
|
||||
quote! {
|
||||
#path::#ty_name,
|
||||
}
|
||||
}
|
||||
Self::Ref { ty_name } => ty_name,
|
||||
Self::ArrayOfRefs { ty_name } => ty_name,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,49 +59,6 @@ impl EnumVariantTupleValue {
|
|||
match self {
|
||||
Self::Ref { ty_name } => ty_name.clone(),
|
||||
Self::ArrayOfRefs { ty_name } => format!("{ty_name}s"),
|
||||
Self::Primitive(PrimitiveType::I64) => "I64".to_owned(),
|
||||
Self::Primitive(PrimitiveType::I32) => "I32".to_owned(),
|
||||
Self::Primitive(PrimitiveType::Float) => "Float".to_owned(),
|
||||
Self::Primitive(PrimitiveType::String) => "String".to_owned(),
|
||||
Self::Primitive(PrimitiveType::DateTime) => "DateTime".to_owned(),
|
||||
Self::Primitive(PrimitiveType::Bool) => "Bool".to_owned(),
|
||||
Self::Enum { .. } => "Variant".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_display(&self, resolved: &ResolvedSchema) -> bool {
|
||||
match self {
|
||||
Self::Primitive(_) => true,
|
||||
Self::Ref { ty_name } | Self::ArrayOfRefs { ty_name } => resolved
|
||||
.models
|
||||
.get(ty_name)
|
||||
.map(|f| f.is_display(resolved))
|
||||
.unwrap_or_default(),
|
||||
Self::Enum { inner, .. } => inner.is_display(resolved),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codegen_display(&self) -> TokenStream {
|
||||
match self {
|
||||
Self::ArrayOfRefs { .. } => quote! {
|
||||
write!(f, "{}", value.iter().map(ToString::to_string).collect::<Vec<_>>().join(","))
|
||||
},
|
||||
_ => quote! {
|
||||
write!(f, "{}", value)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_comparable(&self, resolved: &ResolvedSchema) -> bool {
|
||||
match self {
|
||||
Self::Primitive(PrimitiveType::Float) => false,
|
||||
Self::Primitive(_) => true,
|
||||
Self::Enum { inner, .. } => inner.is_comparable(resolved),
|
||||
Self::Ref { ty_name } | Self::ArrayOfRefs { ty_name } => resolved
|
||||
.models
|
||||
.get(ty_name)
|
||||
.map(|m| matches!(m, Model::Newtype(_)))
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -177,39 +77,12 @@ impl Default for EnumVariantValue {
|
|||
}
|
||||
|
||||
impl EnumVariantValue {
|
||||
pub fn is_display(&self, resolved: &ResolvedSchema) -> bool {
|
||||
match self {
|
||||
Self::Repr(_) | Self::String { .. } => true,
|
||||
Self::Tuple(val) => {
|
||||
val.len() == 1
|
||||
&& val
|
||||
.iter()
|
||||
.next()
|
||||
.map(|v| v.is_display(resolved))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_comparable(&self, resolved: &ResolvedSchema) -> bool {
|
||||
match self {
|
||||
Self::Repr(_) | Self::String { .. } => true,
|
||||
Self::Tuple(values) => values.iter().all(|v| v.is_comparable(resolved)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codegen_display(&self, name: &str) -> Option<TokenStream> {
|
||||
let variant = format_ident!("{name}");
|
||||
|
||||
match self {
|
||||
Self::Repr(i) => Some(quote! { Self::#variant => write!(f, "{}", #i) }),
|
||||
Self::Repr(i) => Some(quote! { write!(f, "{}", #i) }),
|
||||
Self::String { rename } => {
|
||||
let name = rename.as_deref().unwrap_or(name);
|
||||
Some(quote! { Self::#variant => write!(f, #name) })
|
||||
}
|
||||
Self::Tuple(values) if values.len() == 1 => {
|
||||
let rhs = values.first().unwrap().codegen_display();
|
||||
Some(quote! { Self::#variant(value) => #rhs })
|
||||
Some(quote! { write!(f, #name) })
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
@ -223,61 +96,8 @@ pub struct EnumVariant {
|
|||
pub value: EnumVariantValue,
|
||||
}
|
||||
|
||||
pub struct EnumNamespace<'e> {
|
||||
r#enum: &'e Enum,
|
||||
ident: Option<Ident>,
|
||||
elements: Vec<TokenStream>,
|
||||
top_level_elements: Vec<TokenStream>,
|
||||
}
|
||||
|
||||
impl EnumNamespace<'_> {
|
||||
pub fn get_ident(&mut self) -> Ident {
|
||||
self.ident
|
||||
.get_or_insert_with(|| {
|
||||
let name = self.r#enum.name.to_snake_case();
|
||||
format_ident!("{name}")
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub fn push_element(&mut self, el: TokenStream) {
|
||||
self.elements.push(el);
|
||||
}
|
||||
|
||||
pub fn push_top_level(&mut self, el: TokenStream) {
|
||||
self.top_level_elements.push(el);
|
||||
}
|
||||
|
||||
pub fn codegen(mut self) -> Option<TokenStream> {
|
||||
if self.elements.is_empty() && self.top_level_elements.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let top_level = &self.top_level_elements;
|
||||
let mut output = quote! {
|
||||
#(#top_level)*
|
||||
};
|
||||
|
||||
if !self.elements.is_empty() {
|
||||
let ident = self.get_ident();
|
||||
let elements = self.elements;
|
||||
output.extend(quote! {
|
||||
pub mod #ident {
|
||||
#(#elements)*
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Some(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EnumVariant {
|
||||
pub fn codegen(
|
||||
&self,
|
||||
ns: &mut EnumNamespace,
|
||||
resolved: &ResolvedSchema,
|
||||
) -> Option<TokenStream> {
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
let doc = self.description.as_ref().map(|d| {
|
||||
quote! {
|
||||
#[doc = #d]
|
||||
|
@ -307,27 +127,13 @@ impl EnumVariant {
|
|||
EnumVariantValue::Tuple(values) => {
|
||||
let mut val_tys = Vec::with_capacity(values.len());
|
||||
|
||||
if let [value] = values.as_slice() {
|
||||
let enum_name = format_ident!("{}", ns.r#enum.name);
|
||||
let ty_name = value.type_name(ns);
|
||||
|
||||
ns.push_top_level(quote! {
|
||||
impl From<#ty_name> for #enum_name {
|
||||
fn from(value: #ty_name) -> Self {
|
||||
Self::#name(value)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for value in values {
|
||||
let ty_name = value.type_name(ns);
|
||||
let ty_name = value.type_name();
|
||||
let ty_name = format_ident!("{ty_name}");
|
||||
|
||||
if let EnumVariantTupleValue::Enum { inner, .. } = &value {
|
||||
ns.push_element(inner.codegen(resolved)?);
|
||||
}
|
||||
|
||||
val_tys.push(ty_name);
|
||||
val_tys.push(quote! {
|
||||
crate::models::#ty_name
|
||||
});
|
||||
}
|
||||
|
||||
Some(quote! {
|
||||
|
@ -338,7 +144,12 @@ impl EnumVariant {
|
|||
}
|
||||
|
||||
pub fn codegen_display(&self) -> Option<TokenStream> {
|
||||
self.value.codegen_display(&self.name)
|
||||
let rhs = self.value.codegen_display(&self.name)?;
|
||||
let name = format_ident!("{}", self.name);
|
||||
|
||||
Some(quote! {
|
||||
Self::#name => #rhs
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -348,6 +159,7 @@ pub struct Enum {
|
|||
pub description: Option<String>,
|
||||
pub repr: Option<EnumRepr>,
|
||||
pub copy: bool,
|
||||
pub display: bool,
|
||||
pub untagged: bool,
|
||||
pub variants: Vec<EnumVariant>,
|
||||
}
|
||||
|
@ -364,6 +176,7 @@ impl Enum {
|
|||
match &schema.r#enum {
|
||||
Some(OpenApiVariants::Int(int_variants)) => {
|
||||
result.repr = Some(EnumRepr::U32);
|
||||
result.display = true;
|
||||
result.variants = int_variants
|
||||
.iter()
|
||||
.copied()
|
||||
|
@ -375,6 +188,7 @@ impl Enum {
|
|||
.collect();
|
||||
}
|
||||
Some(OpenApiVariants::Str(str_variants)) => {
|
||||
result.display = true;
|
||||
result.variants = str_variants
|
||||
.iter()
|
||||
.copied()
|
||||
|
@ -400,6 +214,7 @@ impl Enum {
|
|||
let mut result = Self {
|
||||
name: name.to_owned(),
|
||||
copy: true,
|
||||
display: true,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -425,7 +240,7 @@ impl Enum {
|
|||
};
|
||||
|
||||
for schema in schemas {
|
||||
let value = EnumVariantTupleValue::from_schema(name, schema)?;
|
||||
let value = EnumVariantTupleValue::from_schema(schema)?;
|
||||
let name = value.name();
|
||||
|
||||
result.variants.push(EnumVariant {
|
||||
|
@ -435,42 +250,13 @@ impl Enum {
|
|||
});
|
||||
}
|
||||
|
||||
// HACK: idk
|
||||
let shared: Vec<_> = result
|
||||
.variants
|
||||
.iter_mut()
|
||||
.filter(|v| v.name == "Variant")
|
||||
.collect();
|
||||
if shared.len() >= 2 {
|
||||
for (idx, variant) in shared.into_iter().enumerate() {
|
||||
let label = idx + 1;
|
||||
variant.name = format!("Variant{}", label);
|
||||
if let EnumVariantValue::Tuple(values) = &mut variant.value {
|
||||
if let [EnumVariantTupleValue::Enum { name, inner, .. }] = values.as_mut_slice()
|
||||
{
|
||||
inner.name.push_str(&label.to_string());
|
||||
name.push_str(&label.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(result)
|
||||
}
|
||||
|
||||
pub fn is_display(&self, resolved: &ResolvedSchema) -> bool {
|
||||
self.variants.iter().all(|v| v.value.is_display(resolved))
|
||||
}
|
||||
|
||||
pub fn is_comparable(&self, resolved: &ResolvedSchema) -> bool {
|
||||
self.variants
|
||||
.iter()
|
||||
.all(|v| v.value.is_comparable(resolved))
|
||||
}
|
||||
|
||||
pub fn codegen(&self, resolved: &ResolvedSchema) -> Option<TokenStream> {
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
let repr = self.repr.map(|r| match r {
|
||||
EnumRepr::U8 => quote! { #[repr(u8)] },
|
||||
EnumRepr::U32 => quote! { #[repr(u32)] },
|
||||
EnumRepr::U8 => quote! { #[repr(u8)]},
|
||||
EnumRepr::U32 => quote! { #[repr(u32)]},
|
||||
});
|
||||
let name = format_ident!("{}", self.name);
|
||||
let desc = self.description.as_ref().map(|d| {
|
||||
|
@ -480,21 +266,12 @@ impl Enum {
|
|||
}
|
||||
});
|
||||
|
||||
let mut ns = EnumNamespace {
|
||||
r#enum: self,
|
||||
ident: None,
|
||||
elements: Default::default(),
|
||||
top_level_elements: Default::default(),
|
||||
};
|
||||
|
||||
let is_display = self.is_display(resolved);
|
||||
|
||||
let mut display = Vec::with_capacity(self.variants.len());
|
||||
let mut variants = Vec::with_capacity(self.variants.len());
|
||||
for variant in &self.variants {
|
||||
variants.push(variant.codegen(&mut ns, resolved)?);
|
||||
variants.push(variant.codegen()?);
|
||||
|
||||
if is_display {
|
||||
if self.display {
|
||||
display.push(variant.codegen_display()?);
|
||||
}
|
||||
}
|
||||
|
@ -508,11 +285,7 @@ impl Enum {
|
|||
}
|
||||
|
||||
if self.copy {
|
||||
derives.push(quote! { Copy });
|
||||
}
|
||||
|
||||
if self.is_comparable(resolved) {
|
||||
derives.push(quote! { Eq, Hash });
|
||||
derives.push(quote! { Copy, Hash });
|
||||
}
|
||||
|
||||
let serde_attr = self.untagged.then(|| {
|
||||
|
@ -521,7 +294,7 @@ impl Enum {
|
|||
}
|
||||
});
|
||||
|
||||
let display = is_display.then(|| {
|
||||
let display = self.display.then(|| {
|
||||
quote! {
|
||||
impl std::fmt::Display for #name {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
|
@ -533,35 +306,34 @@ impl Enum {
|
|||
}
|
||||
});
|
||||
|
||||
let module = ns.codegen();
|
||||
|
||||
Some(quote! {
|
||||
#desc
|
||||
#[derive(Debug, Clone, PartialEq, #(#derives),*)]
|
||||
#[cfg_attr(feature = "strum", derive(strum::EnumIs, strum::EnumTryAs))]
|
||||
#serde_attr
|
||||
pub enum #name {
|
||||
#(#variants),*
|
||||
}
|
||||
#display
|
||||
|
||||
#module
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::openapi::schema::OpenApiSchema;
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::openapi::schema::test::get_schema;
|
||||
|
||||
#[test]
|
||||
fn is_display() {
|
||||
let schema = get_schema();
|
||||
let resolved = ResolvedSchema::from_open_api(&schema);
|
||||
fn codegen() {
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let torn_selection_name = resolved.models.get("TornSelectionName").unwrap();
|
||||
assert!(torn_selection_name.is_display(&resolved));
|
||||
let revive_setting = schema.components.schemas.get("ReviveSetting").unwrap();
|
||||
|
||||
let r#enum = Enum::from_schema("ReviveSetting", revive_setting).unwrap();
|
||||
|
||||
let code = r#enum.codegen().unwrap();
|
||||
|
||||
panic!("{code}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,10 @@
|
|||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use r#enum::Enum;
|
||||
use indexmap::IndexMap;
|
||||
use newtype::Newtype;
|
||||
use object::Object;
|
||||
use parameter::Parameter;
|
||||
use path::{Path, PrettySegments};
|
||||
use proc_macro2::TokenStream;
|
||||
use r#enum::Enum;
|
||||
use scope::Scope;
|
||||
|
||||
use crate::openapi::{r#type::OpenApiType, schema::OpenApiSchema};
|
||||
use crate::openapi::r#type::OpenApiType;
|
||||
|
||||
pub mod r#enum;
|
||||
pub mod newtype;
|
||||
|
@ -27,169 +22,7 @@ pub enum Model {
|
|||
Unresolved,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn is_display(&self, resolved: &ResolvedSchema) -> bool {
|
||||
match self {
|
||||
Self::Enum(r#enum) => r#enum.is_display(resolved),
|
||||
Self::Newtype(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ResolvedSchema {
|
||||
pub models: IndexMap<String, Model>,
|
||||
pub paths: IndexMap<String, Path>,
|
||||
pub parameters: Vec<Parameter>,
|
||||
|
||||
pub warnings: WarningReporter,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Warning {
|
||||
pub message: String,
|
||||
pub path: Vec<String>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Warning {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "in {}: {}", self.path.join("."), self.message)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct WarningReporterState {
|
||||
warnings: Vec<Warning>,
|
||||
path: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct WarningReporter {
|
||||
state: Rc<RefCell<WarningReporterState>>,
|
||||
}
|
||||
|
||||
impl WarningReporter {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
fn push(&self, message: impl ToString) {
|
||||
let mut state = self.state.borrow_mut();
|
||||
let path = state.path.iter().map(ToString::to_string).collect();
|
||||
state.warnings.push(Warning {
|
||||
message: message.to_string(),
|
||||
path,
|
||||
});
|
||||
}
|
||||
|
||||
fn child(&self, name: impl ToString) -> WarningReporter {
|
||||
self.state.borrow_mut().path.push(name.to_string());
|
||||
|
||||
Self {
|
||||
state: self.state.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.state.borrow().warnings.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_warnings(&self) -> Vec<Warning> {
|
||||
self.state.borrow().warnings.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WarningReporter {
|
||||
fn drop(&mut self) {
|
||||
self.state.borrow_mut().path.pop();
|
||||
}
|
||||
}
|
||||
|
||||
impl ResolvedSchema {
|
||||
pub fn from_open_api(schema: &OpenApiSchema) -> Self {
|
||||
let mut result = Self::default();
|
||||
|
||||
for (name, r#type) in &schema.components.schemas {
|
||||
result.models.insert(
|
||||
name.to_string(),
|
||||
resolve(r#type, name, &schema.components.schemas, &result.warnings),
|
||||
);
|
||||
}
|
||||
|
||||
for (path, body) in &schema.paths {
|
||||
result.paths.insert(
|
||||
path.to_string(),
|
||||
Path::from_schema(
|
||||
path,
|
||||
body,
|
||||
&schema.components.parameters,
|
||||
result.warnings.child(path),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
for (name, param) in &schema.components.parameters {
|
||||
result
|
||||
.parameters
|
||||
.push(Parameter::from_schema(name, param).unwrap());
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn codegen_models(&self) -> TokenStream {
|
||||
let mut output = TokenStream::default();
|
||||
|
||||
for model in self.models.values() {
|
||||
output.extend(model.codegen(self));
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn codegen_requests(&self) -> TokenStream {
|
||||
let mut output = TokenStream::default();
|
||||
|
||||
for path in self.paths.values() {
|
||||
output.extend(
|
||||
path.codegen_request(self, self.warnings.child(PrettySegments(&path.segments))),
|
||||
);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn codegen_parameters(&self) -> TokenStream {
|
||||
let mut output = TokenStream::default();
|
||||
|
||||
for param in &self.parameters {
|
||||
output.extend(param.codegen(self));
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
pub fn codegen_scopes(&self) -> TokenStream {
|
||||
let mut output = TokenStream::default();
|
||||
|
||||
let scopes = Scope::from_paths(self.paths.values().cloned().collect());
|
||||
|
||||
for scope in scopes {
|
||||
output.extend(scope.codegen());
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(
|
||||
r#type: &OpenApiType,
|
||||
name: &str,
|
||||
schemas: &IndexMap<&str, OpenApiType>,
|
||||
warnings: &WarningReporter,
|
||||
) -> Model {
|
||||
pub fn resolve(r#type: &OpenApiType, name: &str, schemas: &IndexMap<&str, OpenApiType>) -> Model {
|
||||
match r#type {
|
||||
OpenApiType {
|
||||
r#enum: Some(_), ..
|
||||
|
@ -197,12 +30,8 @@ pub fn resolve(
|
|||
OpenApiType {
|
||||
r#type: Some("object"),
|
||||
..
|
||||
} => Model::Object(Object::from_schema_object(
|
||||
name,
|
||||
r#type,
|
||||
schemas,
|
||||
warnings.child(name),
|
||||
)),
|
||||
} => Object::from_schema_object(name, r#type, schemas)
|
||||
.map_or(Model::Unresolved, Model::Object),
|
||||
OpenApiType {
|
||||
r#type: Some(_), ..
|
||||
} => Newtype::from_schema(name, r#type).map_or(Model::Unresolved, Model::Newtype),
|
||||
|
@ -213,22 +42,17 @@ pub fn resolve(
|
|||
OpenApiType {
|
||||
all_of: Some(types),
|
||||
..
|
||||
} => Model::Object(Object::from_all_of(
|
||||
name,
|
||||
types,
|
||||
schemas,
|
||||
warnings.child(name),
|
||||
)),
|
||||
} => Object::from_all_of(name, types, schemas).map_or(Model::Unresolved, Model::Object),
|
||||
_ => Model::Unresolved,
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub fn codegen(&self, resolved: &ResolvedSchema) -> Option<TokenStream> {
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
match self {
|
||||
Self::Newtype(newtype) => newtype.codegen(),
|
||||
Self::Enum(r#enum) => r#enum.codegen(resolved),
|
||||
Self::Object(object) => object.codegen(resolved),
|
||||
Self::Enum(r#enum) => r#enum.codegen(),
|
||||
Self::Object(object) => object.codegen(),
|
||||
Self::Unresolved => None,
|
||||
}
|
||||
}
|
||||
|
@ -237,22 +61,18 @@ impl Model {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::openapi::schema::test::get_schema;
|
||||
use crate::{
|
||||
model::r#enum::{EnumRepr, EnumVariant},
|
||||
openapi::schema::OpenApiSchema,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn resolve_newtypes() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let user_id_schema = schema.components.schemas.get("UserId").unwrap();
|
||||
|
||||
let reporter = WarningReporter::new();
|
||||
let user_id = resolve(
|
||||
user_id_schema,
|
||||
"UserId",
|
||||
&schema.components.schemas,
|
||||
&reporter,
|
||||
);
|
||||
assert!(reporter.is_empty());
|
||||
let user_id = resolve(user_id_schema, "UserId", &schema.components.schemas);
|
||||
|
||||
assert_eq!(
|
||||
user_id,
|
||||
|
@ -267,13 +87,7 @@ mod test {
|
|||
|
||||
let attack_code_schema = schema.components.schemas.get("AttackCode").unwrap();
|
||||
|
||||
let attack_code = resolve(
|
||||
attack_code_schema,
|
||||
"AttackCode",
|
||||
&schema.components.schemas,
|
||||
&reporter,
|
||||
);
|
||||
assert!(reporter.is_empty());
|
||||
let attack_code = resolve(attack_code_schema, "AttackCode", &schema.components.schemas);
|
||||
|
||||
assert_eq!(
|
||||
attack_code,
|
||||
|
@ -287,18 +101,74 @@ mod test {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_enums() {
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let forum_feed_type_schema = schema.components.schemas.get("ForumFeedTypeEnum").unwrap();
|
||||
|
||||
let forum_feed_type = resolve(
|
||||
forum_feed_type_schema,
|
||||
"ForumFeedTypeEnum",
|
||||
&schema.components.schemas,
|
||||
);
|
||||
|
||||
assert_eq!(forum_feed_type, Model::Enum(Enum {
|
||||
name: "ForumFeedType".to_owned(),
|
||||
description: Some("This represents the type of the activity. Values range from 1 to 8 where:\n * 1 = 'X posted on a thread',\n * 2 = 'X created a thread',\n * 3 = 'X liked your thread',\n * 4 = 'X disliked your thread',\n * 5 = 'X liked your post',\n * 6 = 'X disliked your post',\n * 7 = 'X quoted your post'.".to_owned()),
|
||||
repr: Some(EnumRepr::U32),
|
||||
copy: true,
|
||||
untagged: false,
|
||||
display: true,
|
||||
variants: vec![
|
||||
EnumVariant {
|
||||
name: "Variant1".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(1),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant2".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(2),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant3".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(3),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant4".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(4),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant5".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(5),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant6".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(6),
|
||||
..Default::default()
|
||||
},
|
||||
EnumVariant {
|
||||
name: "Variant7".to_owned(),
|
||||
value: r#enum::EnumVariantValue::Repr(7),
|
||||
..Default::default()
|
||||
},
|
||||
]
|
||||
}))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_all() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut unresolved = vec![];
|
||||
let total = schema.components.schemas.len();
|
||||
|
||||
for (name, desc) in &schema.components.schemas {
|
||||
let reporter = WarningReporter::new();
|
||||
if resolve(desc, name, &schema.components.schemas, &reporter) == Model::Unresolved
|
||||
|| !reporter.is_empty()
|
||||
{
|
||||
if resolve(desc, name, &schema.components.schemas) == Model::Unresolved {
|
||||
unresolved.push(name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,3 +121,24 @@ impl Newtype {
|
|||
Some(body)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::openapi::schema::OpenApiSchema;
|
||||
|
||||
#[test]
|
||||
fn codegen() {
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let user_id = schema.components.schemas.get("UserId").unwrap();
|
||||
|
||||
let mut newtype = Newtype::from_schema("UserId", user_id).unwrap();
|
||||
|
||||
newtype.description = Some("Description goes here".to_owned());
|
||||
|
||||
let code = newtype.codegen().unwrap().to_string();
|
||||
|
||||
panic!("{code}");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use heck::{ToSnakeCase, ToUpperCamelCase};
|
||||
use indexmap::{map::Entry, IndexMap};
|
||||
use indexmap::IndexMap;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote, ToTokens};
|
||||
use quote::{ToTokens, format_ident, quote};
|
||||
use syn::Ident;
|
||||
|
||||
use crate::openapi::r#type::OpenApiType;
|
||||
|
||||
use super::{r#enum::Enum, ResolvedSchema, WarningReporter};
|
||||
use super::r#enum::Enum;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PrimitiveType {
|
||||
|
@ -15,7 +15,6 @@ pub enum PrimitiveType {
|
|||
I64,
|
||||
String,
|
||||
Float,
|
||||
DateTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
@ -28,11 +27,7 @@ pub enum PropertyType {
|
|||
}
|
||||
|
||||
impl PropertyType {
|
||||
pub fn codegen(
|
||||
&self,
|
||||
namespace: &mut ObjectNamespace,
|
||||
resolved: &ResolvedSchema,
|
||||
) -> Option<TokenStream> {
|
||||
pub fn codegen(&self, namespace: &mut ObjectNamespace) -> Option<TokenStream> {
|
||||
match self {
|
||||
Self::Primitive(PrimitiveType::Bool) => Some(format_ident!("bool").into_token_stream()),
|
||||
Self::Primitive(PrimitiveType::I32) => Some(format_ident!("i32").into_token_stream()),
|
||||
|
@ -40,9 +35,6 @@ impl PropertyType {
|
|||
Self::Primitive(PrimitiveType::String) => {
|
||||
Some(format_ident!("String").into_token_stream())
|
||||
}
|
||||
Self::Primitive(PrimitiveType::DateTime) => {
|
||||
Some(quote! { chrono::DateTime<chrono::Utc> })
|
||||
}
|
||||
Self::Primitive(PrimitiveType::Float) => Some(format_ident!("f64").into_token_stream()),
|
||||
Self::Ref(path) => {
|
||||
let name = path.strip_prefix("#/components/schemas/")?;
|
||||
|
@ -51,7 +43,7 @@ impl PropertyType {
|
|||
Some(quote! { crate::models::#name })
|
||||
}
|
||||
Self::Enum(r#enum) => {
|
||||
let code = r#enum.codegen(resolved)?;
|
||||
let code = r#enum.codegen()?;
|
||||
namespace.push_element(code);
|
||||
|
||||
let ns = namespace.get_ident();
|
||||
|
@ -62,14 +54,14 @@ impl PropertyType {
|
|||
})
|
||||
}
|
||||
Self::Array(array) => {
|
||||
let inner_ty = array.codegen(namespace, resolved)?;
|
||||
let inner_ty = array.codegen(namespace)?;
|
||||
|
||||
Some(quote! {
|
||||
Vec<#inner_ty>
|
||||
})
|
||||
}
|
||||
Self::Nested(nested) => {
|
||||
let code = nested.codegen(resolved)?;
|
||||
let code = nested.codegen()?;
|
||||
namespace.push_element(code);
|
||||
|
||||
let ns = namespace.get_ident();
|
||||
|
@ -85,13 +77,11 @@ impl PropertyType {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Property {
|
||||
pub field_name: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub required: bool,
|
||||
pub nullable: bool,
|
||||
pub r#type: PropertyType,
|
||||
pub deprecated: bool,
|
||||
}
|
||||
|
||||
impl Property {
|
||||
|
@ -100,68 +90,60 @@ impl Property {
|
|||
required: bool,
|
||||
schema: &OpenApiType,
|
||||
schemas: &IndexMap<&str, OpenApiType>,
|
||||
warnings: WarningReporter,
|
||||
) -> Option<Self> {
|
||||
let name = name.to_owned();
|
||||
let field_name = name.to_snake_case();
|
||||
let description = schema.description.as_deref().map(ToOwned::to_owned);
|
||||
|
||||
match schema {
|
||||
OpenApiType {
|
||||
r#enum: Some(_), ..
|
||||
} => {
|
||||
let Some(r#enum) = Enum::from_schema(&name.clone().to_upper_camel_case(), schema)
|
||||
else {
|
||||
warnings.push("Failed to create enum");
|
||||
return None;
|
||||
};
|
||||
Some(Self {
|
||||
r#type: PropertyType::Enum(r#enum),
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
required,
|
||||
deprecated: schema.deprecated,
|
||||
nullable: false,
|
||||
})
|
||||
}
|
||||
} => Some(Self {
|
||||
r#type: PropertyType::Enum(Enum::from_schema(
|
||||
&name.clone().to_upper_camel_case(),
|
||||
schema,
|
||||
)?),
|
||||
name,
|
||||
description,
|
||||
required,
|
||||
nullable: false,
|
||||
}),
|
||||
OpenApiType {
|
||||
one_of: Some(types),
|
||||
..
|
||||
} => match types.as_slice() {
|
||||
[left, OpenApiType {
|
||||
r#type: Some("null"),
|
||||
..
|
||||
}] => {
|
||||
let mut inner = Self::from_schema(&name, required, left, schemas, warnings)?;
|
||||
[
|
||||
left,
|
||||
OpenApiType {
|
||||
r#type: Some("null"),
|
||||
..
|
||||
},
|
||||
] => {
|
||||
let mut inner = Self::from_schema(&name, required, left, schemas)?;
|
||||
inner.nullable = true;
|
||||
Some(inner)
|
||||
}
|
||||
[left @ .., OpenApiType {
|
||||
r#type: Some("null"),
|
||||
..
|
||||
}] => {
|
||||
[
|
||||
left @ ..,
|
||||
OpenApiType {
|
||||
r#type: Some("null"),
|
||||
..
|
||||
},
|
||||
] => {
|
||||
let rest = OpenApiType {
|
||||
one_of: Some(left.to_owned()),
|
||||
..schema.clone()
|
||||
};
|
||||
let mut inner = Self::from_schema(&name, required, &rest, schemas, warnings)?;
|
||||
let mut inner = Self::from_schema(&name, required, &rest, schemas)?;
|
||||
inner.nullable = true;
|
||||
Some(inner)
|
||||
}
|
||||
cases => {
|
||||
let Some(r#enum) = Enum::from_one_of(&name.to_upper_camel_case(), cases) else {
|
||||
warnings.push("Failed to create oneOf enum");
|
||||
return None;
|
||||
};
|
||||
|
||||
let r#enum = Enum::from_one_of(&name.to_upper_camel_case(), cases)?;
|
||||
Some(Self {
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
description: None,
|
||||
required,
|
||||
nullable: false,
|
||||
deprecated: schema.deprecated,
|
||||
r#type: PropertyType::Enum(r#enum),
|
||||
})
|
||||
}
|
||||
|
@ -170,49 +152,37 @@ impl Property {
|
|||
all_of: Some(types),
|
||||
..
|
||||
} => {
|
||||
let obj_name = name.to_upper_camel_case();
|
||||
let composite =
|
||||
Object::from_all_of(&obj_name, types, schemas, warnings.child(&obj_name));
|
||||
let composite = Object::from_all_of(&name.to_upper_camel_case(), types, schemas)?;
|
||||
Some(Self {
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
description: None,
|
||||
required,
|
||||
nullable: false,
|
||||
deprecated: schema.deprecated,
|
||||
r#type: PropertyType::Nested(Box::new(composite)),
|
||||
})
|
||||
}
|
||||
OpenApiType {
|
||||
r#type: Some("object"),
|
||||
..
|
||||
} => {
|
||||
let obj_name = name.to_upper_camel_case();
|
||||
Some(Self {
|
||||
r#type: PropertyType::Nested(Box::new(Object::from_schema_object(
|
||||
&obj_name,
|
||||
schema,
|
||||
schemas,
|
||||
warnings.child(&obj_name),
|
||||
))),
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
required,
|
||||
deprecated: schema.deprecated,
|
||||
nullable: false,
|
||||
})
|
||||
}
|
||||
} => Some(Self {
|
||||
r#type: PropertyType::Nested(Box::new(Object::from_schema_object(
|
||||
&name.clone().to_upper_camel_case(),
|
||||
schema,
|
||||
schemas,
|
||||
)?)),
|
||||
name,
|
||||
description,
|
||||
required,
|
||||
nullable: false,
|
||||
}),
|
||||
OpenApiType {
|
||||
ref_path: Some(path),
|
||||
..
|
||||
} => Some(Self {
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
r#type: PropertyType::Ref((*path).to_owned()),
|
||||
required,
|
||||
deprecated: schema.deprecated,
|
||||
nullable: false,
|
||||
}),
|
||||
OpenApiType {
|
||||
|
@ -220,15 +190,13 @@ impl Property {
|
|||
items: Some(items),
|
||||
..
|
||||
} => {
|
||||
let inner = Self::from_schema(&name, required, items, schemas, warnings)?;
|
||||
let inner = Self::from_schema(&name, required, items, schemas)?;
|
||||
|
||||
Some(Self {
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
required,
|
||||
nullable: false,
|
||||
deprecated: schema.deprecated,
|
||||
r#type: PropertyType::Array(Box::new(inner.r#type)),
|
||||
})
|
||||
}
|
||||
|
@ -238,50 +206,38 @@ impl Property {
|
|||
let prim = match (schema.r#type, schema.format) {
|
||||
(Some("integer"), Some("int32")) => PrimitiveType::I32,
|
||||
(Some("integer"), Some("int64")) => PrimitiveType::I64,
|
||||
(Some("number"), /* Some("float") */ _) | (_, Some("float")) => {
|
||||
PrimitiveType::Float
|
||||
}
|
||||
(Some("number"), Some("float")) => PrimitiveType::Float,
|
||||
(Some("string"), None) => PrimitiveType::String,
|
||||
(Some("string"), Some("date")) => PrimitiveType::DateTime,
|
||||
(Some("boolean"), None) => PrimitiveType::Bool,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(Self {
|
||||
name,
|
||||
field_name,
|
||||
description,
|
||||
required,
|
||||
nullable: false,
|
||||
deprecated: schema.deprecated,
|
||||
r#type: PropertyType::Primitive(prim),
|
||||
})
|
||||
}
|
||||
_ => {
|
||||
warnings.push("Could not resolve property type");
|
||||
None
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codegen(
|
||||
&self,
|
||||
namespace: &mut ObjectNamespace,
|
||||
resolved: &ResolvedSchema,
|
||||
) -> Option<TokenStream> {
|
||||
pub fn codegen(&self, namespace: &mut ObjectNamespace) -> Option<TokenStream> {
|
||||
let desc = self.description.as_ref().map(|d| quote! { #[doc = #d]});
|
||||
|
||||
let name = &self.name;
|
||||
let (name, serde_attr) = match name.as_str() {
|
||||
"type" => (format_ident!("r#type"), None),
|
||||
name if name != self.field_name => (
|
||||
format_ident!("{}", self.field_name),
|
||||
name if name != name.to_snake_case() => (
|
||||
format_ident!("{}", name.to_snake_case()),
|
||||
Some(quote! { #[serde(rename = #name)]}),
|
||||
),
|
||||
_ => (format_ident!("{}", self.field_name), None),
|
||||
_ => (format_ident!("{name}"), None),
|
||||
};
|
||||
|
||||
let ty_inner = self.r#type.codegen(namespace, resolved)?;
|
||||
let ty_inner = self.r#type.codegen(namespace)?;
|
||||
|
||||
let ty = if !self.required || self.nullable {
|
||||
quote! { Option<#ty_inner> }
|
||||
|
@ -289,17 +245,8 @@ impl Property {
|
|||
ty_inner
|
||||
};
|
||||
|
||||
let deprecated = self.deprecated.then(|| {
|
||||
let note = self.description.as_ref().map(|d| quote! { note = #d });
|
||||
|
||||
quote! {
|
||||
#[deprecated(#note)]
|
||||
}
|
||||
});
|
||||
|
||||
Some(quote! {
|
||||
#desc
|
||||
#deprecated
|
||||
#serde_attr
|
||||
pub #name: #ty
|
||||
})
|
||||
|
@ -310,7 +257,7 @@ impl Property {
|
|||
pub struct Object {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub properties: IndexMap<String, Property>,
|
||||
pub properties: Vec<Property>,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
|
@ -318,8 +265,7 @@ impl Object {
|
|||
name: &str,
|
||||
schema: &OpenApiType,
|
||||
schemas: &IndexMap<&str, OpenApiType>,
|
||||
warnings: WarningReporter,
|
||||
) -> Self {
|
||||
) -> Option<Self> {
|
||||
let mut result = Object {
|
||||
name: name.to_owned(),
|
||||
description: schema.description.as_deref().map(ToOwned::to_owned),
|
||||
|
@ -327,54 +273,38 @@ impl Object {
|
|||
};
|
||||
|
||||
let Some(props) = &schema.properties else {
|
||||
warnings.push("Missing properties");
|
||||
return result;
|
||||
return None;
|
||||
};
|
||||
|
||||
let required = schema.required.clone().unwrap_or_default();
|
||||
|
||||
for (prop_name, prop) in props {
|
||||
let Some(prop) = Property::from_schema(
|
||||
// HACK: This will cause a duplicate key otherwise
|
||||
if ["itemDetails", "sci-fi", "non-attackers", "co-leader_id"].contains(prop_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: implement custom enum for this (depends on overrides being added)
|
||||
if *prop_name == "value" && name == "TornHof" {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.properties.push(Property::from_schema(
|
||||
prop_name,
|
||||
required.contains(prop_name),
|
||||
prop,
|
||||
schemas,
|
||||
warnings.child(prop_name),
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let field_name = prop.field_name.clone();
|
||||
|
||||
let entry = result.properties.entry(field_name.clone());
|
||||
if let Entry::Occupied(mut entry) = entry {
|
||||
let other_name = entry.get().name.clone();
|
||||
warnings.push(format!(
|
||||
"Property name collision: {other_name} and {field_name}"
|
||||
));
|
||||
// deprioritise kebab and camelcase
|
||||
if other_name.contains('-')
|
||||
|| other_name
|
||||
.chars()
|
||||
.filter(|c| c.is_alphabetic())
|
||||
.all(|c| c.is_ascii_lowercase())
|
||||
{
|
||||
entry.insert(prop);
|
||||
}
|
||||
} else {
|
||||
entry.insert_entry(prop);
|
||||
}
|
||||
)?);
|
||||
}
|
||||
|
||||
result
|
||||
Some(result)
|
||||
}
|
||||
|
||||
pub fn from_all_of(
|
||||
name: &str,
|
||||
types: &[OpenApiType],
|
||||
schemas: &IndexMap<&str, OpenApiType>,
|
||||
warnings: WarningReporter,
|
||||
) -> Self {
|
||||
) -> Option<Self> {
|
||||
let mut result = Self {
|
||||
name: name.to_owned(),
|
||||
..Default::default()
|
||||
|
@ -382,32 +312,25 @@ impl Object {
|
|||
|
||||
for r#type in types {
|
||||
let r#type = if let OpenApiType {
|
||||
ref_path: Some(ref_path),
|
||||
ref_path: Some(path),
|
||||
..
|
||||
} = r#type
|
||||
{
|
||||
let Some(name) = ref_path.strip_prefix("#/components/schemas/") else {
|
||||
warnings.push(format!("Malformed ref {ref_path}"));
|
||||
continue;
|
||||
};
|
||||
let Some(schema) = schemas.get(name) else {
|
||||
warnings.push(format!("Missing schema for ref {name}"));
|
||||
continue;
|
||||
};
|
||||
schema
|
||||
let name = path.strip_prefix("#/components/schemas/")?;
|
||||
schemas.get(name)?
|
||||
} else {
|
||||
r#type
|
||||
};
|
||||
let obj = Self::from_schema_object(name, r#type, schemas, warnings.child("variant"));
|
||||
let obj = Self::from_schema_object(name, r#type, schemas)?;
|
||||
|
||||
result.description = result.description.or(obj.description);
|
||||
result.properties.extend(obj.properties);
|
||||
}
|
||||
|
||||
result
|
||||
Some(result)
|
||||
}
|
||||
|
||||
pub fn codegen(&self, resolved: &ResolvedSchema) -> Option<TokenStream> {
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
let doc = self.description.as_ref().map(|d| {
|
||||
quote! {
|
||||
#[doc = #d]
|
||||
|
@ -421,8 +344,8 @@ impl Object {
|
|||
};
|
||||
|
||||
let mut props = Vec::with_capacity(self.properties.len());
|
||||
for (_, prop) in &self.properties {
|
||||
props.push(prop.codegen(&mut namespace, resolved)?);
|
||||
for prop in &self.properties {
|
||||
props.push(prop.codegen(&mut namespace)?);
|
||||
}
|
||||
|
||||
let name = format_ident!("{}", self.name);
|
||||
|
@ -479,11 +402,23 @@ impl ObjectNamespace<'_> {
|
|||
mod test {
|
||||
use super::*;
|
||||
|
||||
use crate::openapi::schema::test::get_schema;
|
||||
use crate::openapi::schema::OpenApiSchema;
|
||||
|
||||
#[test]
|
||||
fn resolve_object() {
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let attack = schema.components.schemas.get("FactionUpgrades").unwrap();
|
||||
|
||||
let resolved =
|
||||
Object::from_schema_object("FactionUpgrades", attack, &schema.components.schemas)
|
||||
.unwrap();
|
||||
let _code = resolved.codegen().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_objects() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut objects = 0;
|
||||
let mut unresolved = vec![];
|
||||
|
@ -491,14 +426,7 @@ mod test {
|
|||
for (name, desc) in &schema.components.schemas {
|
||||
if desc.r#type == Some("object") {
|
||||
objects += 1;
|
||||
let reporter = WarningReporter::new();
|
||||
Object::from_schema_object(
|
||||
name,
|
||||
desc,
|
||||
&schema.components.schemas,
|
||||
reporter.clone(),
|
||||
);
|
||||
if !reporter.is_empty() {
|
||||
if Object::from_schema_object(name, desc, &schema.components.schemas).is_none() {
|
||||
unresolved.push(name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,14 @@ use std::fmt::Write;
|
|||
|
||||
use heck::ToUpperCamelCase;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote, ToTokens};
|
||||
use quote::{ToTokens, format_ident, quote};
|
||||
|
||||
use crate::openapi::parameter::{
|
||||
OpenApiParameter, OpenApiParameterDefault, OpenApiParameterSchema,
|
||||
ParameterLocation as SchemaLocation,
|
||||
};
|
||||
|
||||
use super::{r#enum::Enum, ResolvedSchema};
|
||||
use super::r#enum::Enum;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParameterOptions<P> {
|
||||
|
@ -42,7 +42,9 @@ impl ParameterType {
|
|||
match schema {
|
||||
OpenApiParameterSchema {
|
||||
r#type: Some("integer"),
|
||||
format: Some("int32"),
|
||||
// BUG: missing for some types in the spec
|
||||
|
||||
// format: Some("int32"),
|
||||
..
|
||||
} => {
|
||||
let default = match schema.default {
|
||||
|
@ -88,17 +90,6 @@ impl ParameterType {
|
|||
r#type: Enum::from_parameter_schema(name, schema)?,
|
||||
})
|
||||
}
|
||||
OpenApiParameterSchema {
|
||||
one_of: Some(schemas),
|
||||
..
|
||||
} => Some(ParameterType::Enum {
|
||||
options: ParameterOptions {
|
||||
default: None,
|
||||
minimum: None,
|
||||
maximum: None,
|
||||
},
|
||||
r#type: Enum::from_one_of(name, schemas)?,
|
||||
}),
|
||||
OpenApiParameterSchema {
|
||||
r#type: Some("string"),
|
||||
..
|
||||
|
@ -179,7 +170,7 @@ impl Parameter {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn codegen(&self, resolved: &ResolvedSchema) -> Option<TokenStream> {
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
match &self.r#type {
|
||||
ParameterType::I32 { options } => {
|
||||
let name = format_ident!("{}", self.name);
|
||||
|
@ -283,7 +274,7 @@ The default value [Self::{}](self::{}#variant.{})"#,
|
|||
}
|
||||
|
||||
let doc = quote! { #[doc = #desc]};
|
||||
let inner = r#type.codegen(resolved)?;
|
||||
let inner = r#type.codegen()?;
|
||||
|
||||
Some(quote! {
|
||||
#doc
|
||||
|
@ -309,13 +300,13 @@ The default value [Self::{}](self::{}#variant.{})"#,
|
|||
..self.clone()
|
||||
};
|
||||
|
||||
let mut code = inner.codegen(resolved).unwrap_or_default();
|
||||
let mut code = inner.codegen().unwrap_or_default();
|
||||
|
||||
let name = format_ident!("{}", outer_name);
|
||||
let inner_ty = items.codegen_type_name(&inner_name);
|
||||
|
||||
code.extend(quote! {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct #name(pub Vec<#inner_ty>);
|
||||
|
||||
impl std::fmt::Display for #name {
|
||||
|
@ -333,9 +324,9 @@ The default value [Self::{}](self::{}#variant.{})"#,
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> From<T> for #name where T: IntoIterator, T::Item: Into<#inner_ty> {
|
||||
impl<T> From<T> for #name where T: IntoIterator<Item = #inner_ty> {
|
||||
fn from(value: T) -> #name {
|
||||
let items = value.into_iter().map(Into::into).collect();
|
||||
let items = value.into_iter().collect();
|
||||
|
||||
Self(items)
|
||||
}
|
||||
|
@ -351,13 +342,13 @@ The default value [Self::{}](self::{}#variant.{})"#,
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::openapi::{path::OpenApiPathParameter, schema::test::get_schema};
|
||||
use crate::openapi::{path::OpenApiPathParameter, schema::OpenApiSchema};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn resolve_components() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut parameters = 0;
|
||||
let mut unresolved = vec![];
|
||||
|
@ -385,7 +376,7 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn resolve_inline() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut params = 0;
|
||||
let mut unresolved = Vec::new();
|
||||
|
@ -413,8 +404,7 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn codegen_inline() {
|
||||
let schema = get_schema();
|
||||
let resolved = ResolvedSchema::from_open_api(&schema);
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut params = 0;
|
||||
let mut unresolved = Vec::new();
|
||||
|
@ -435,7 +425,7 @@ mod test {
|
|||
continue;
|
||||
}
|
||||
params += 1;
|
||||
if param.codegen(&resolved).is_none() {
|
||||
if param.codegen().is_none() {
|
||||
unresolved.push(format!("`{}.{}`", path, inline.name));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::fmt::Write;
|
||||
use std::{fmt::Write, ops::Deref};
|
||||
|
||||
use heck::{ToSnakeCase, ToUpperCamelCase};
|
||||
use indexmap::IndexMap;
|
||||
|
@ -14,7 +14,6 @@ use crate::openapi::{
|
|||
use super::{
|
||||
parameter::{Parameter, ParameterLocation, ParameterType},
|
||||
union::Union,
|
||||
ResolvedSchema, WarningReporter,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -23,21 +22,6 @@ pub enum PathSegment {
|
|||
Parameter { name: String },
|
||||
}
|
||||
|
||||
pub struct PrettySegments<'a>(pub &'a [PathSegment]);
|
||||
|
||||
impl std::fmt::Display for PrettySegments<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for segment in self.0 {
|
||||
match segment {
|
||||
PathSegment::Constant(c) => write!(f, "/{c}")?,
|
||||
PathSegment::Parameter { name } => write!(f, "/{{{name}}}")?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum PathParameter {
|
||||
Inline(Parameter),
|
||||
|
@ -56,7 +40,7 @@ pub struct Path {
|
|||
pub segments: Vec<PathSegment>,
|
||||
pub name: String,
|
||||
pub summary: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub description: String,
|
||||
pub parameters: Vec<PathParameter>,
|
||||
pub response: PathResponse,
|
||||
}
|
||||
|
@ -66,7 +50,6 @@ impl Path {
|
|||
path: &str,
|
||||
schema: &OpenApiPath,
|
||||
parameters: &IndexMap<&str, OpenApiParameter>,
|
||||
warnings: WarningReporter,
|
||||
) -> Option<Self> {
|
||||
let mut segments = Vec::new();
|
||||
for segment in path.strip_prefix('/')?.split('/') {
|
||||
|
@ -80,7 +63,7 @@ impl Path {
|
|||
}
|
||||
|
||||
let summary = schema.get.summary.as_deref().map(ToOwned::to_owned);
|
||||
let description = schema.get.description.as_deref().map(ToOwned::to_owned);
|
||||
let description = schema.get.description.deref().to_owned();
|
||||
|
||||
let mut params = Vec::with_capacity(schema.get.parameters.len());
|
||||
for parameter in &schema.get.parameters {
|
||||
|
@ -127,13 +110,9 @@ impl Path {
|
|||
.strip_prefix("#/components/schemas/")?
|
||||
.to_owned(),
|
||||
},
|
||||
OpenApiResponseBody::Union { any_of: _ } => {
|
||||
PathResponse::ArbitraryUnion(Union::from_schema(
|
||||
"Response",
|
||||
&schema.get.response_content,
|
||||
warnings.child("response"),
|
||||
)?)
|
||||
}
|
||||
OpenApiResponseBody::Union { any_of: _ } => PathResponse::ArbitraryUnion(
|
||||
Union::from_schema("Response", &schema.get.response_content)?,
|
||||
),
|
||||
};
|
||||
|
||||
Some(Self {
|
||||
|
@ -146,11 +125,7 @@ impl Path {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn codegen_request(
|
||||
&self,
|
||||
resolved: &ResolvedSchema,
|
||||
warnings: WarningReporter,
|
||||
) -> Option<TokenStream> {
|
||||
pub fn codegen_request(&self) -> Option<TokenStream> {
|
||||
let name = if self.segments.len() == 1 {
|
||||
let Some(PathSegment::Constant(first)) = self.segments.first() else {
|
||||
return None;
|
||||
|
@ -184,21 +159,21 @@ impl Path {
|
|||
let ty_name = format_ident!("{}", param.name);
|
||||
|
||||
if is_inline {
|
||||
ns.push_element(param.codegen(resolved)?);
|
||||
ns.push_element(param.codegen()?);
|
||||
let path = ns.get_ident();
|
||||
|
||||
(
|
||||
quote! {
|
||||
crate::request::models::#path::#ty_name
|
||||
},
|
||||
Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
|
||||
Some(quote! { #[builder(into)] }),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
quote! {
|
||||
crate::parameters::#ty_name
|
||||
},
|
||||
Some(quote! { #[cfg_attr(feature = "builder", builder(into))]}),
|
||||
Some(quote! { #[builder(into)]}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -215,14 +190,14 @@ impl Path {
|
|||
)
|
||||
}
|
||||
ParameterType::Array { .. } => {
|
||||
ns.push_element(param.codegen(resolved)?);
|
||||
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
|
||||
},
|
||||
Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
|
||||
Some(quote! { #[builder(into)] }),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
@ -231,30 +206,17 @@ impl Path {
|
|||
let query_val = ¶m.value;
|
||||
|
||||
if param.location == ParameterLocation::Path {
|
||||
if self.segments.iter().any(|s| {
|
||||
if let PathSegment::Parameter { name } = s {
|
||||
name == ¶m.value
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
discriminant.push(ty.clone());
|
||||
discriminant_val.push(quote! { self.#name });
|
||||
let path_name = format_ident!("{}", param.value);
|
||||
start_fields.push(quote! {
|
||||
#[cfg_attr(feature = "builder", builder(start_fn))]
|
||||
#builder_param
|
||||
pub #name: #ty
|
||||
});
|
||||
fmt_val.push(quote! {
|
||||
#path_name=self.#name
|
||||
});
|
||||
} else {
|
||||
warnings.push(format!(
|
||||
"Provided path parameter is not present in the url: {}",
|
||||
param.value
|
||||
));
|
||||
}
|
||||
discriminant.push(ty.clone());
|
||||
discriminant_val.push(quote! { self.#name });
|
||||
let path_name = format_ident!("{}", param.value);
|
||||
start_fields.push(quote! {
|
||||
#[builder(start_fn)]
|
||||
#builder_param
|
||||
pub #name: #ty
|
||||
});
|
||||
fmt_val.push(quote! {
|
||||
#path_name=self.#name
|
||||
});
|
||||
} else {
|
||||
let ty = if param.required {
|
||||
convert_field.push(quote! {
|
||||
|
@ -311,9 +273,8 @@ impl Path {
|
|||
Some(quote! {
|
||||
#ns
|
||||
|
||||
#[cfg_attr(feature = "builder", derive(bon::Builder))]
|
||||
#[derive(Debug, Clone)]
|
||||
#[cfg_attr(feature = "builder", builder(state_mod(vis = "pub(crate)"), on(String, into)))]
|
||||
#[derive(Debug, Clone, bon::Builder)]
|
||||
#[builder(state_mod(vis = "pub(crate)"), on(String, into))]
|
||||
pub struct #name {
|
||||
#(#start_fields),*
|
||||
}
|
||||
|
@ -322,18 +283,15 @@ impl Path {
|
|||
#[allow(unused_parens)]
|
||||
type Discriminant = (#(#discriminant),*);
|
||||
type Response = #response_ty;
|
||||
fn into_request(self) -> (Self::Discriminant, crate::request::ApiRequest) {
|
||||
let path = format!(#path_fmt_str, #(#fmt_val),*);
|
||||
fn into_request(self) -> crate::request::ApiRequest<Self::Discriminant> {
|
||||
#[allow(unused_parens)]
|
||||
(
|
||||
(#(#discriminant_val),*),
|
||||
crate::request::ApiRequest {
|
||||
path,
|
||||
parameters: std::iter::empty()
|
||||
#(#convert_field)*
|
||||
.collect(),
|
||||
}
|
||||
)
|
||||
crate::request::ApiRequest {
|
||||
path: format!(#path_fmt_str, #(#fmt_val),*),
|
||||
parameters: std::iter::empty()
|
||||
#(#convert_field)*
|
||||
.collect(),
|
||||
disriminant: (#(#discriminant_val),*),
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -366,15 +324,7 @@ impl Path {
|
|||
PathParameter::Component(param) => (param, false),
|
||||
};
|
||||
|
||||
if param.location == ParameterLocation::Path
|
||||
&& self.segments.iter().any(|s| {
|
||||
if let PathSegment::Parameter { name } = s {
|
||||
name == ¶m.value
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
{
|
||||
if param.location == ParameterLocation::Path {
|
||||
let ty = match ¶m.r#type {
|
||||
ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
|
||||
let ty_name = format_ident!("{}", param.name);
|
||||
|
@ -398,13 +348,7 @@ impl Path {
|
|||
crate::models::#ty_name
|
||||
}
|
||||
}
|
||||
ParameterType::Array { .. } => {
|
||||
let ty_name = param.r#type.codegen_type_name(¶m.name);
|
||||
|
||||
quote! {
|
||||
crate::request::models::#request_mod_name::#ty_name
|
||||
}
|
||||
}
|
||||
ParameterType::Array { .. } => param.r#type.codegen_type_name(¶m.name),
|
||||
};
|
||||
|
||||
let arg_name = format_ident!("{}", param.value.to_snake_case());
|
||||
|
@ -429,25 +373,9 @@ 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! {
|
||||
#doc
|
||||
pub async fn #fn_name<S>(
|
||||
self,
|
||||
&self,
|
||||
#(#extra_args)*
|
||||
builder: impl FnOnce(
|
||||
#builder_path<#builder_mod_path::Empty>
|
||||
|
@ -462,148 +390,6 @@ impl Path {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn codegen_bulk_scope_call(&self) -> Option<TokenStream> {
|
||||
let mut disc = Vec::new();
|
||||
let mut disc_ty = Vec::new();
|
||||
|
||||
let snake_name = self.name.to_snake_case();
|
||||
|
||||
let request_name = format_ident!("{}Request", self.name);
|
||||
let builder_name = format_ident!("{}RequestBuilder", self.name);
|
||||
let builder_mod_name = format_ident!("{}_request_builder", snake_name);
|
||||
let request_mod_name = format_ident!("{snake_name}");
|
||||
|
||||
let request_path = quote! { crate::request::models::#request_name };
|
||||
let builder_path = quote! { crate::request::models::#builder_name };
|
||||
let builder_mod_path = quote! { crate::request::models::#builder_mod_name };
|
||||
|
||||
let tail = snake_name
|
||||
.split_once('_')
|
||||
.map_or_else(|| "for_selections".to_owned(), |(_, tail)| tail.to_owned());
|
||||
|
||||
let fn_name = format_ident!("{tail}");
|
||||
|
||||
for param in &self.parameters {
|
||||
let (param, is_inline) = match param {
|
||||
PathParameter::Inline(param) => (param, true),
|
||||
PathParameter::Component(param) => (param, false),
|
||||
};
|
||||
if param.location == ParameterLocation::Path
|
||||
&& self.segments.iter().any(|s| {
|
||||
if let PathSegment::Parameter { name } = s {
|
||||
name == ¶m.value
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
{
|
||||
let ty = match ¶m.r#type {
|
||||
ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
|
||||
let ty_name = format_ident!("{}", param.name);
|
||||
|
||||
if is_inline {
|
||||
quote! {
|
||||
crate::request::models::#request_mod_name::#ty_name
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
crate::parameters::#ty_name
|
||||
}
|
||||
}
|
||||
}
|
||||
ParameterType::String => quote! { String },
|
||||
ParameterType::Boolean => quote! { bool },
|
||||
ParameterType::Schema { type_name } => {
|
||||
let ty_name = format_ident!("{}", type_name);
|
||||
|
||||
quote! {
|
||||
crate::models::#ty_name
|
||||
}
|
||||
}
|
||||
ParameterType::Array { .. } => {
|
||||
let name = param.r#type.codegen_type_name(¶m.name);
|
||||
quote! {
|
||||
crate::request::models::#request_mod_name::#name
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let arg_name = format_ident!("{}", param.value.to_snake_case());
|
||||
|
||||
disc_ty.push(ty);
|
||||
disc.push(arg_name);
|
||||
}
|
||||
}
|
||||
|
||||
if disc.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let response_ty = match &self.response {
|
||||
PathResponse::Component { name } => {
|
||||
let name = format_ident!("{name}");
|
||||
quote! {
|
||||
crate::models::#name
|
||||
}
|
||||
}
|
||||
PathResponse::ArbitraryUnion(union) => {
|
||||
let name = format_ident!("{}", union.name);
|
||||
quote! {
|
||||
crate::request::models::#request_mod_name::#name
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let disc = if disc.len() > 1 {
|
||||
quote! { (#(#disc),*) }
|
||||
} else {
|
||||
quote! { #(#disc),* }
|
||||
};
|
||||
|
||||
let disc_ty = if disc_ty.len() > 1 {
|
||||
quote! { (#(#disc_ty),*) }
|
||||
} else {
|
||||
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! {
|
||||
#doc
|
||||
pub fn #fn_name<S, I, B>(
|
||||
self,
|
||||
ids: I,
|
||||
builder: B
|
||||
) -> impl futures::Stream<Item = (#disc_ty, Result<#response_ty, E::Error>)>
|
||||
where
|
||||
I: IntoIterator<Item = #disc_ty>,
|
||||
S: #builder_mod_path::IsComplete,
|
||||
B: Fn(
|
||||
#builder_path<#builder_mod_path::Empty>
|
||||
) -> #builder_path<S>,
|
||||
{
|
||||
let requests = ids.into_iter()
|
||||
.map(move |#disc| builder(#request_path::builder(#disc)).build());
|
||||
|
||||
let executor = self.executor;
|
||||
executor.fetch_many(requests)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PathNamespace<'r> {
|
||||
|
@ -645,25 +431,18 @@ impl PathNamespace<'_> {
|
|||
mod test {
|
||||
use super::*;
|
||||
|
||||
use crate::openapi::schema::test::get_schema;
|
||||
use crate::openapi::schema::OpenApiSchema;
|
||||
|
||||
#[test]
|
||||
fn resolve_paths() {
|
||||
let schema = get_schema();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut paths = 0;
|
||||
let mut unresolved = vec![];
|
||||
|
||||
for (name, desc) in &schema.paths {
|
||||
paths += 1;
|
||||
if Path::from_schema(
|
||||
name,
|
||||
desc,
|
||||
&schema.components.parameters,
|
||||
WarningReporter::new(),
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
if Path::from_schema(name, desc, &schema.components.parameters).is_none() {
|
||||
unresolved.push(name);
|
||||
}
|
||||
}
|
||||
|
@ -684,25 +463,19 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn codegen_paths() {
|
||||
let schema = get_schema();
|
||||
let resolved = ResolvedSchema::from_open_api(&schema);
|
||||
let reporter = WarningReporter::new();
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let mut paths = 0;
|
||||
let mut unresolved = vec![];
|
||||
|
||||
for (name, desc) in &schema.paths {
|
||||
paths += 1;
|
||||
let Some(path) =
|
||||
Path::from_schema(name, desc, &schema.components.parameters, reporter.clone())
|
||||
else {
|
||||
let Some(path) = Path::from_schema(name, desc, &schema.components.parameters) else {
|
||||
unresolved.push(name);
|
||||
continue;
|
||||
};
|
||||
|
||||
if path.codegen_scope_call().is_none()
|
||||
|| path.codegen_request(&resolved, reporter.clone()).is_none()
|
||||
{
|
||||
if path.codegen_scope_call().is_none() || path.codegen_request().is_none() {
|
||||
unresolved.push(name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,56 +35,30 @@ impl Scope {
|
|||
|
||||
pub fn codegen(&self) -> Option<TokenStream> {
|
||||
let name = format_ident!("{}", self.name);
|
||||
let bulk_name = format_ident!("Bulk{}", self.name);
|
||||
|
||||
let mut functions = Vec::with_capacity(self.members.len());
|
||||
let mut bulk_functions = Vec::with_capacity(self.members.len());
|
||||
|
||||
for member in &self.members {
|
||||
if let Some(code) = member.codegen_scope_call() {
|
||||
functions.push(code);
|
||||
}
|
||||
if let Some(code) = member.codegen_bulk_scope_call() {
|
||||
bulk_functions.push(code);
|
||||
}
|
||||
}
|
||||
|
||||
Some(quote! {
|
||||
#[allow(dead_code)]
|
||||
pub struct #name<E>(E)
|
||||
pub struct #name<'e, E>(&'e E)
|
||||
where
|
||||
E: crate::executor::Executor;
|
||||
|
||||
impl<E> #name<E>
|
||||
impl<'e, E> #name<'e, E>
|
||||
where
|
||||
E: crate::executor::Executor
|
||||
{
|
||||
pub fn new(executor: E) -> Self {
|
||||
pub fn new(executor: &'e E) -> Self {
|
||||
Self(executor)
|
||||
}
|
||||
|
||||
#(#functions)*
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct #bulk_name<E> where
|
||||
E: crate::executor::BulkExecutor,
|
||||
{
|
||||
executor: E,
|
||||
}
|
||||
|
||||
impl<E> #bulk_name<E>
|
||||
where
|
||||
E: crate::executor::BulkExecutor
|
||||
{
|
||||
pub fn new(executor: E) -> Self {
|
||||
Self {
|
||||
executor,
|
||||
}
|
||||
}
|
||||
|
||||
#(#bulk_functions)*
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,6 @@ use quote::{format_ident, quote};
|
|||
|
||||
use crate::openapi::path::OpenApiResponseBody;
|
||||
|
||||
use super::WarningReporter;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Union {
|
||||
pub name: String,
|
||||
|
@ -13,23 +11,10 @@ pub struct Union {
|
|||
}
|
||||
|
||||
impl Union {
|
||||
pub fn from_schema(
|
||||
name: &str,
|
||||
schema: &OpenApiResponseBody,
|
||||
warnings: WarningReporter,
|
||||
) -> Option<Self> {
|
||||
pub fn from_schema(name: &str, schema: &OpenApiResponseBody) -> Option<Self> {
|
||||
let members = match schema {
|
||||
OpenApiResponseBody::Union { any_of } => {
|
||||
let mut members = Vec::with_capacity(any_of.len());
|
||||
for l in any_of {
|
||||
let path = l.ref_path.to_owned();
|
||||
if members.contains(&path) {
|
||||
warnings.push(format!("Duplicate member: {path}"));
|
||||
} else {
|
||||
members.push(path);
|
||||
}
|
||||
}
|
||||
members
|
||||
any_of.iter().map(|l| l.ref_path.to_owned()).collect()
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
|
@ -48,7 +33,7 @@ impl Union {
|
|||
let ty_name = format_ident!("{}", variant_name);
|
||||
variants.push(quote! {
|
||||
pub fn #accessor_name(&self) -> Result<crate::models::#ty_name, serde_json::Error> {
|
||||
self.deserialize()
|
||||
<crate::models::#ty_name as serde::Deserialize>::deserialize(&self.0)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -58,13 +43,6 @@ impl Union {
|
|||
pub struct #name(serde_json::Value);
|
||||
|
||||
impl #name {
|
||||
pub fn deserialize<'de, T>(&'de self) -> Result<T, serde_json::Error>
|
||||
where
|
||||
T: serde::Deserialize<'de>,
|
||||
{
|
||||
T::deserialize(&self.0)
|
||||
}
|
||||
|
||||
#(#variants)*
|
||||
}
|
||||
})
|
||||
|
|
|
@ -2,8 +2,6 @@ use std::borrow::Cow;
|
|||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::r#type::OpenApiType;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ParameterLocation {
|
||||
|
@ -11,15 +9,14 @@ pub enum ParameterLocation {
|
|||
Path,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OpenApiParameterDefault<'a> {
|
||||
Int(i32),
|
||||
Str(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct OpenApiParameterSchema<'a> {
|
||||
#[serde(rename = "$ref")]
|
||||
pub ref_path: Option<&'a str>,
|
||||
|
@ -30,10 +27,9 @@ pub struct OpenApiParameterSchema<'a> {
|
|||
pub maximum: Option<i32>,
|
||||
pub minimum: Option<i32>,
|
||||
pub items: Option<Box<OpenApiParameterSchema<'a>>>,
|
||||
pub one_of: Option<Vec<OpenApiType<'a>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct OpenApiParameter<'a> {
|
||||
pub name: &'a str,
|
||||
pub description: Option<Cow<'a, str>>,
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
use super::parameter::OpenApiParameter;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OpenApiPathParameter<'a> {
|
||||
Link {
|
||||
|
@ -14,13 +14,13 @@ pub enum OpenApiPathParameter<'a> {
|
|||
Inline(OpenApiParameter<'a>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct SchemaLink<'a> {
|
||||
#[serde(rename = "$ref")]
|
||||
pub ref_path: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum OpenApiResponseBody<'a> {
|
||||
Schema(SchemaLink<'a>),
|
||||
|
@ -30,9 +30,6 @@ pub enum OpenApiResponseBody<'a> {
|
|||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct OperationId(pub String);
|
||||
|
||||
fn deserialize_response_body<'de, D>(deserializer: D) -> Result<OpenApiResponseBody<'de>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
|
@ -63,11 +60,10 @@ where
|
|||
Ok(responses.ok.content.json.schema)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct OpenApiPathBody<'a> {
|
||||
pub summary: Option<Cow<'a, str>>,
|
||||
pub description: Option<Cow<'a, str>>,
|
||||
pub description: Cow<'a, str>,
|
||||
#[serde(borrow, default)]
|
||||
pub parameters: Vec<OpenApiPathParameter<'a>>,
|
||||
#[serde(
|
||||
|
@ -76,10 +72,9 @@ pub struct OpenApiPathBody<'a> {
|
|||
deserialize_with = "deserialize_response_body"
|
||||
)]
|
||||
pub response_content: OpenApiResponseBody<'a>,
|
||||
pub operation_id: Option<OperationId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct OpenApiPath<'a> {
|
||||
#[serde(borrow)]
|
||||
pub get: OpenApiPathBody<'a>,
|
||||
|
|
|
@ -3,7 +3,7 @@ use serde::Deserialize;
|
|||
|
||||
use super::{parameter::OpenApiParameter, path::OpenApiPath, r#type::OpenApiType};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Components<'a> {
|
||||
#[serde(borrow)]
|
||||
pub schemas: IndexMap<&'a str, OpenApiType<'a>>,
|
||||
|
@ -11,7 +11,7 @@ pub struct Components<'a> {
|
|||
pub parameters: IndexMap<&'a str, OpenApiParameter<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct OpenApiSchema<'a> {
|
||||
#[serde(borrow)]
|
||||
pub paths: IndexMap<&'a str, OpenApiPath<'a>>,
|
||||
|
@ -19,12 +19,20 @@ pub struct OpenApiSchema<'a> {
|
|||
pub components: Components<'a>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test {
|
||||
use super::*;
|
||||
impl OpenApiSchema<'_> {
|
||||
pub fn read() -> Result<Self, serde_json::Error> {
|
||||
let s = include_str!("../../openapi.json");
|
||||
|
||||
pub(crate) fn get_schema() -> OpenApiSchema<'static> {
|
||||
let s = include_str!("../../../torn-api/openapi.json");
|
||||
serde_json::from_str(s).unwrap()
|
||||
serde_json::from_str(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn read() {
|
||||
OpenApiSchema::read().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,17 @@
|
|||
[package]
|
||||
name = "torn-api"
|
||||
version = "1.7.0"
|
||||
edition = "2021"
|
||||
version = "1.0.1"
|
||||
edition = "2024"
|
||||
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"]
|
||||
strum = ["dep:strum"]
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_repr = { version = "0.1", optional = true }
|
||||
serde_repr = "0.1"
|
||||
serde_json = { workspace = true }
|
||||
bon = { version = "3.6", optional = true }
|
||||
bon = "3.6"
|
||||
bytes = "1"
|
||||
http = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = [
|
||||
|
@ -28,19 +20,12 @@ reqwest = { version = "0.12", default-features = false, features = [
|
|||
"brotli",
|
||||
] }
|
||||
thiserror = "2"
|
||||
futures = { version = "0.3", default-features = false, features = [
|
||||
"std",
|
||||
"async-await",
|
||||
] }
|
||||
chrono = { version = "0.4.41", features = ["serde"] }
|
||||
strum = { version = "0.27.1", features = ["derive"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
|
||||
[build-dependencies]
|
||||
torn-api-codegen = { path = "../torn-api-codegen", version = "0.7.0" }
|
||||
torn-api-codegen = { path = "../torn-api-codegen", version = "0.1.1" }
|
||||
syn = { workspace = true, features = ["parsing"] }
|
||||
proc-macro2 = { workspace = true }
|
||||
prettyplease = "0.2"
|
||||
serde_json = { workspace = true }
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
use std::{env, fs, path::Path};
|
||||
|
||||
use torn_api_codegen::{model::ResolvedSchema, openapi::schema::OpenApiSchema};
|
||||
use proc_macro2::TokenStream;
|
||||
use torn_api_codegen::{
|
||||
model::{parameter::Parameter, path::Path as ApiPath, resolve, scope::Scope},
|
||||
openapi::schema::OpenApiSchema,
|
||||
};
|
||||
|
||||
const DENY_LIST: &[&str] = &[];
|
||||
|
||||
fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
@ -9,27 +15,61 @@ fn main() {
|
|||
let requests_dest = Path::new(&out_dir).join("requests.rs");
|
||||
let scopes_dest = Path::new(&out_dir).join("scopes.rs");
|
||||
|
||||
let s = include_str!("./openapi.json");
|
||||
let schema: OpenApiSchema = serde_json::from_str(s).unwrap();
|
||||
let resolved = ResolvedSchema::from_open_api(&schema);
|
||||
let schema = OpenApiSchema::read().unwrap();
|
||||
|
||||
let models_file = syn::parse2(resolved.codegen_models()).unwrap();
|
||||
let mut models_code = TokenStream::new();
|
||||
|
||||
for (name, model) in &schema.components.schemas {
|
||||
if DENY_LIST.contains(name) {
|
||||
continue;
|
||||
}
|
||||
let model = resolve(model, name, &schema.components.schemas);
|
||||
if let Some(new_code) = model.codegen() {
|
||||
models_code.extend(new_code);
|
||||
}
|
||||
}
|
||||
|
||||
let models_file = syn::parse2(models_code).unwrap();
|
||||
let models_pretty = prettyplease::unparse(&models_file);
|
||||
fs::write(&model_dest, models_pretty).unwrap();
|
||||
|
||||
let params_file = syn::parse2(resolved.codegen_parameters()).unwrap();
|
||||
let mut params_code = TokenStream::new();
|
||||
|
||||
for (name, param) in &schema.components.parameters {
|
||||
if let Some(code) = Parameter::from_schema(name, param).unwrap().codegen() {
|
||||
params_code.extend(code);
|
||||
}
|
||||
}
|
||||
|
||||
let params_file = syn::parse2(params_code).unwrap();
|
||||
let params_pretty = prettyplease::unparse(¶ms_file);
|
||||
fs::write(¶ms_dest, params_pretty).unwrap();
|
||||
|
||||
let requests_file = syn::parse2(resolved.codegen_requests()).unwrap();
|
||||
let mut requests_code = TokenStream::new();
|
||||
let mut paths = Vec::new();
|
||||
for (name, path) in &schema.paths {
|
||||
let Some(path) = ApiPath::from_schema(name, path, &schema.components.parameters) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(code) = path.codegen_request() {
|
||||
requests_code.extend(code);
|
||||
}
|
||||
paths.push(path);
|
||||
}
|
||||
|
||||
let requests_file = syn::parse2(requests_code).unwrap();
|
||||
let requests_pretty = prettyplease::unparse(&requests_file);
|
||||
fs::write(&requests_dest, requests_pretty).unwrap();
|
||||
|
||||
let scopes_file = syn::parse2(resolved.codegen_scopes()).unwrap();
|
||||
let mut scope_code = TokenStream::new();
|
||||
let scopes = Scope::from_paths(paths);
|
||||
for scope in scopes {
|
||||
if let Some(code) = scope.codegen() {
|
||||
scope_code.extend(code);
|
||||
}
|
||||
}
|
||||
|
||||
let scopes_file = syn::parse2(scope_code).unwrap();
|
||||
let scopes_pretty = prettyplease::unparse(&scopes_file);
|
||||
fs::write(&scopes_dest, scopes_pretty).unwrap();
|
||||
|
||||
for warning in resolved.warnings.get_warnings() {
|
||||
println!("cargo:warning={}", warning);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,22 @@
|
|||
use std::future::Future;
|
||||
|
||||
use futures::{Stream, StreamExt};
|
||||
use http::{header::AUTHORIZATION, HeaderMap, HeaderValue};
|
||||
use http::{HeaderMap, HeaderValue, header::AUTHORIZATION};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
use crate::scopes::{
|
||||
BulkFactionScope, BulkForumScope, BulkMarketScope, BulkRacingScope, BulkTornScope,
|
||||
BulkUserScope, FactionScope, ForumScope, MarketScope, RacingScope, TornScope, UserScope,
|
||||
};
|
||||
use crate::{
|
||||
request::{ApiRequest, ApiResponse, IntoRequest},
|
||||
scopes::{BulkKeyScope, KeyScope},
|
||||
request::{ApiResponse, IntoRequest},
|
||||
scopes::{FactionScope, ForumScope, MarketScope, RacingScope, TornScope, UserScope},
|
||||
};
|
||||
|
||||
pub trait Executor: Sized {
|
||||
pub trait Executor {
|
||||
type Error: From<serde_json::Error> + From<crate::ApiError> + Send;
|
||||
|
||||
fn execute<R>(
|
||||
self,
|
||||
&self,
|
||||
request: R,
|
||||
) -> impl Future<Output = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Send
|
||||
) -> impl Future<Output = Result<ApiResponse<R::Discriminant>, Self::Error>> + Send
|
||||
where
|
||||
R: IntoRequest;
|
||||
|
||||
fn fetch<R>(self, request: R) -> impl Future<Output = Result<R::Response, Self::Error>> + Send
|
||||
fn fetch<R>(&self, request: R) -> impl Future<Output = Result<R::Response, Self::Error>> + Send
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
|
@ -32,7 +24,7 @@ pub trait Executor: Sized {
|
|||
// The future is `Send` but `&self` might not be.
|
||||
let fut = self.execute(request);
|
||||
async {
|
||||
let resp = fut.await.1?;
|
||||
let resp = fut.await?;
|
||||
|
||||
let bytes = resp.body.unwrap();
|
||||
|
||||
|
@ -59,164 +51,6 @@ pub trait Executor: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait BulkExecutor: Sized {
|
||||
type Error: From<serde_json::Error> + From<crate::ApiError> + Send;
|
||||
|
||||
fn execute<R>(
|
||||
self,
|
||||
requests: impl IntoIterator<Item = R>,
|
||||
) -> impl Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
|
||||
where
|
||||
R: IntoRequest;
|
||||
|
||||
fn fetch_many<R>(
|
||||
self,
|
||||
requests: impl IntoIterator<Item = R>,
|
||||
) -> impl Stream<Item = (R::Discriminant, Result<R::Response, Self::Error>)> + Unpin
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
self.execute(requests).map(|(d, r)| {
|
||||
let r = match r {
|
||||
Ok(r) => r,
|
||||
Err(why) => return (d, Err(why)),
|
||||
};
|
||||
let bytes = r.body.unwrap();
|
||||
|
||||
if bytes.starts_with(br#"{"error":{"#) {
|
||||
#[derive(Deserialize)]
|
||||
struct ErrorBody<'a> {
|
||||
code: u16,
|
||||
error: &'a str,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct ErrorContainer<'a> {
|
||||
#[serde(borrow)]
|
||||
error: ErrorBody<'a>,
|
||||
}
|
||||
|
||||
let error: ErrorContainer = match serde_json::from_slice(&bytes) {
|
||||
Ok(error) => error,
|
||||
Err(why) => return (d, Err(why.into())),
|
||||
};
|
||||
return (
|
||||
d,
|
||||
Err(crate::ApiError::new(error.error.code, error.error.error).into()),
|
||||
);
|
||||
}
|
||||
|
||||
let resp = match serde_json::from_slice(&bytes) {
|
||||
Ok(resp) => resp,
|
||||
Err(why) => return (d, Err(why.into())),
|
||||
};
|
||||
|
||||
(d, Ok(resp))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
pub trait ExecutorExt: Executor + Sized {
|
||||
fn user(self) -> UserScope<Self>;
|
||||
|
||||
fn faction(self) -> FactionScope<Self>;
|
||||
|
||||
fn torn(self) -> TornScope<Self>;
|
||||
|
||||
fn market(self) -> MarketScope<Self>;
|
||||
|
||||
fn racing(self) -> RacingScope<Self>;
|
||||
|
||||
fn forum(self) -> ForumScope<Self>;
|
||||
|
||||
fn key(self) -> KeyScope<Self>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
impl<T> ExecutorExt for T
|
||||
where
|
||||
T: Executor + Sized,
|
||||
{
|
||||
fn user(self) -> UserScope<Self> {
|
||||
UserScope::new(self)
|
||||
}
|
||||
|
||||
fn faction(self) -> FactionScope<Self> {
|
||||
FactionScope::new(self)
|
||||
}
|
||||
|
||||
fn torn(self) -> TornScope<Self> {
|
||||
TornScope::new(self)
|
||||
}
|
||||
|
||||
fn market(self) -> MarketScope<Self> {
|
||||
MarketScope::new(self)
|
||||
}
|
||||
|
||||
fn racing(self) -> RacingScope<Self> {
|
||||
RacingScope::new(self)
|
||||
}
|
||||
|
||||
fn forum(self) -> ForumScope<Self> {
|
||||
ForumScope::new(self)
|
||||
}
|
||||
|
||||
fn key(self) -> KeyScope<Self> {
|
||||
KeyScope::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
pub trait BulkExecutorExt: BulkExecutor + Sized {
|
||||
fn user_bulk(self) -> BulkUserScope<Self>;
|
||||
|
||||
fn faction_bulk(self) -> BulkFactionScope<Self>;
|
||||
|
||||
fn torn_bulk(self) -> BulkTornScope<Self>;
|
||||
|
||||
fn market_bulk(self) -> BulkMarketScope<Self>;
|
||||
|
||||
fn racing_bulk(self) -> BulkRacingScope<Self>;
|
||||
|
||||
fn forum_bulk(self) -> BulkForumScope<Self>;
|
||||
|
||||
fn key_bulk(self) -> BulkKeyScope<Self>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
impl<T> BulkExecutorExt for T
|
||||
where
|
||||
T: BulkExecutor + Sized,
|
||||
{
|
||||
fn user_bulk(self) -> BulkUserScope<Self> {
|
||||
BulkUserScope::new(self)
|
||||
}
|
||||
|
||||
fn faction_bulk(self) -> BulkFactionScope<Self> {
|
||||
BulkFactionScope::new(self)
|
||||
}
|
||||
|
||||
fn torn_bulk(self) -> BulkTornScope<Self> {
|
||||
BulkTornScope::new(self)
|
||||
}
|
||||
|
||||
fn market_bulk(self) -> BulkMarketScope<Self> {
|
||||
BulkMarketScope::new(self)
|
||||
}
|
||||
|
||||
fn racing_bulk(self) -> BulkRacingScope<Self> {
|
||||
BulkRacingScope::new(self)
|
||||
}
|
||||
|
||||
fn forum_bulk(self) -> BulkForumScope<Self> {
|
||||
BulkForumScope::new(self)
|
||||
}
|
||||
|
||||
fn key_bulk(self) -> BulkKeyScope<Self> {
|
||||
BulkKeyScope::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ReqwestClient(reqwest::Client);
|
||||
|
||||
impl ReqwestClient {
|
||||
|
@ -237,53 +71,77 @@ impl ReqwestClient {
|
|||
}
|
||||
}
|
||||
|
||||
impl ReqwestClient {
|
||||
async fn execute_api_request(&self, request: ApiRequest) -> Result<ApiResponse, crate::Error> {
|
||||
pub trait ExecutorExt: Executor + Sized {
|
||||
fn user(&self) -> UserScope<'_, Self>;
|
||||
|
||||
fn faction(&self) -> FactionScope<'_, Self>;
|
||||
|
||||
fn torn(&self) -> TornScope<'_, Self>;
|
||||
|
||||
fn market(&self) -> MarketScope<'_, Self>;
|
||||
|
||||
fn racing(&self) -> RacingScope<'_, Self>;
|
||||
|
||||
fn forum(&self) -> ForumScope<'_, Self>;
|
||||
}
|
||||
|
||||
impl<T> ExecutorExt for T
|
||||
where
|
||||
T: Executor + Sized,
|
||||
{
|
||||
fn user(&self) -> UserScope<'_, Self> {
|
||||
UserScope::new(self)
|
||||
}
|
||||
|
||||
fn faction(&self) -> FactionScope<'_, Self> {
|
||||
FactionScope::new(self)
|
||||
}
|
||||
|
||||
fn torn(&self) -> TornScope<'_, Self> {
|
||||
TornScope::new(self)
|
||||
}
|
||||
|
||||
fn market(&self) -> MarketScope<'_, Self> {
|
||||
MarketScope::new(self)
|
||||
}
|
||||
|
||||
fn racing(&self) -> RacingScope<'_, Self> {
|
||||
RacingScope::new(self)
|
||||
}
|
||||
|
||||
fn forum(&self) -> ForumScope<'_, Self> {
|
||||
ForumScope::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Executor for ReqwestClient {
|
||||
type Error = crate::Error;
|
||||
|
||||
async fn execute<R>(&self, request: R) -> Result<ApiResponse<R::Discriminant>, Self::Error>
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
let request = request.into_request();
|
||||
let url = request.url();
|
||||
|
||||
let response = self.0.get(url).send().await?;
|
||||
let status = response.status();
|
||||
let body = response.bytes().await.ok();
|
||||
|
||||
Ok(ApiResponse { status, body })
|
||||
}
|
||||
}
|
||||
|
||||
impl Executor for &ReqwestClient {
|
||||
type Error = crate::Error;
|
||||
|
||||
async fn execute<R>(self, request: R) -> (R::Discriminant, Result<ApiResponse, Self::Error>)
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
let (d, request) = request.into_request();
|
||||
(d, self.execute_api_request(request).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl BulkExecutor for &ReqwestClient {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn execute<R>(
|
||||
self,
|
||||
requests: impl IntoIterator<Item = R>,
|
||||
) -> impl Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)>
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
futures::stream::iter(requests)
|
||||
.map(move |r| <Self as Executor>::execute(self, r))
|
||||
.buffer_unordered(25)
|
||||
Ok(ApiResponse {
|
||||
discriminant: request.disriminant,
|
||||
status,
|
||||
body,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{scopes::test::test_client, ApiError, Error};
|
||||
use crate::{ApiError, Error, scopes::test::test_client};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
#[tokio::test]
|
||||
async fn api_error() {
|
||||
let client = test_client().await;
|
||||
|
@ -295,22 +153,4 @@ mod test {
|
|||
other => panic!("Expected incorrect id entity relation error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "scopes")]
|
||||
#[tokio::test]
|
||||
async fn bulk_request() {
|
||||
let client = test_client().await;
|
||||
|
||||
let stream = client
|
||||
.faction_bulk()
|
||||
.basic_for_id(vec![19.into(), 89.into()], |b| b);
|
||||
|
||||
let mut responses: Vec<_> = stream.collect().await;
|
||||
|
||||
let (_id1, basic1) = responses.pop().unwrap();
|
||||
basic1.unwrap();
|
||||
|
||||
let (_id2, basic2) = responses.pop().unwrap();
|
||||
basic2.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
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)]
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
use bon::Builder;
|
||||
use bytes::Bytes;
|
||||
use http::StatusCode;
|
||||
|
||||
#[cfg(feature = "requests")]
|
||||
use crate::{
|
||||
executor::Executor,
|
||||
models::{FactionChainsResponse, FactionId},
|
||||
};
|
||||
|
||||
pub mod models;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ApiRequest {
|
||||
pub struct ApiRequest<D = ()> {
|
||||
pub disriminant: D,
|
||||
pub path: String,
|
||||
pub parameters: Vec<(&'static str, String)>,
|
||||
}
|
||||
|
||||
impl ApiRequest {
|
||||
impl<D> ApiRequest<D> {
|
||||
pub fn url(&self) -> String {
|
||||
let mut url = format!("https://api.torn.com/v2{}?", self.path);
|
||||
|
||||
let mut first = true;
|
||||
for (name, value) in &self.parameters {
|
||||
if first {
|
||||
first = false;
|
||||
} else {
|
||||
url.push('&');
|
||||
}
|
||||
url.push_str(&format!("{name}={value}"));
|
||||
}
|
||||
|
||||
|
@ -28,35 +28,77 @@ impl ApiRequest {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct ApiResponse {
|
||||
pub struct ApiResponse<D = ()> {
|
||||
pub discriminant: D,
|
||||
pub body: Option<Bytes>,
|
||||
pub status: StatusCode,
|
||||
}
|
||||
|
||||
pub trait IntoRequest: Send {
|
||||
type Discriminant: Send + 'static;
|
||||
type Discriminant: Send;
|
||||
type Response: for<'de> serde::Deserialize<'de> + Send;
|
||||
fn into_request(self) -> (Self::Discriminant, ApiRequest);
|
||||
fn into_request(self) -> ApiRequest<Self::Discriminant>;
|
||||
}
|
||||
|
||||
pub(crate) struct WrappedApiRequest<R>
|
||||
pub struct FactionScope<'e, E>(&'e E)
|
||||
where
|
||||
R: IntoRequest,
|
||||
E: Executor;
|
||||
|
||||
impl<E> FactionScope<'_, E>
|
||||
where
|
||||
E: Executor,
|
||||
{
|
||||
discriminant: R::Discriminant,
|
||||
request: ApiRequest,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> IntoRequest for WrappedApiRequest<R>
|
||||
where
|
||||
R: IntoRequest,
|
||||
{
|
||||
type Discriminant = R::Discriminant;
|
||||
type Response = R::Response;
|
||||
fn into_request(self) -> (Self::Discriminant, ApiRequest) {
|
||||
(self.discriminant, self.request)
|
||||
#[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 {}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,9 +9,8 @@ pub(super) mod test {
|
|||
use crate::{
|
||||
executor::{ExecutorExt, ReqwestClient},
|
||||
models::{
|
||||
faction_selection_name::FactionSelectionNameVariant,
|
||||
user_selection_name::UserSelectionNameVariant, AttackCode, PersonalStatsCategoryEnum,
|
||||
PersonalStatsStatName, UserListEnum,
|
||||
AttackCode, FactionSelectionName, PersonalStatsCategoryEnum, PersonalStatsStatName,
|
||||
UserListEnum,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -68,10 +67,7 @@ pub(super) mod test {
|
|||
let r = client
|
||||
.faction()
|
||||
.for_selections(|b| {
|
||||
b.selections([
|
||||
FactionSelectionNameVariant::Basic,
|
||||
FactionSelectionNameVariant::Balance,
|
||||
])
|
||||
b.selections([FactionSelectionName::Basic, FactionSelectionName::Balance])
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -370,15 +366,6 @@ pub(super) mod test {
|
|||
faction_scope.lookup(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn faction_reports() {
|
||||
let client = test_client().await;
|
||||
|
||||
let faction_scope = FactionScope(&client);
|
||||
|
||||
faction_scope.reports(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn forum_categories() {
|
||||
let client = test_client().await;
|
||||
|
@ -428,7 +415,7 @@ pub(super) mod test {
|
|||
let forum_scope = ForumScope(&client);
|
||||
|
||||
forum_scope
|
||||
.threads_for_category_ids([2].into(), |b| b)
|
||||
.threads_for_category_ids("2".to_owned(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -499,14 +486,14 @@ pub(super) mod test {
|
|||
racing_scope.carupgrades(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
/* #[tokio::test]
|
||||
#[tokio::test]
|
||||
async fn racing_races() {
|
||||
let client = test_client().await;
|
||||
|
||||
let racing_scope = RacingScope(&client);
|
||||
|
||||
racing_scope.races(|b| b).await.unwrap();
|
||||
} */
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn racing_race_for_race_id() {
|
||||
|
@ -652,7 +639,10 @@ pub(super) mod test {
|
|||
|
||||
let torn_scope = TornScope(&client);
|
||||
|
||||
torn_scope.items_for_ids([1].into(), |b| b).await.unwrap();
|
||||
torn_scope
|
||||
.items_for_ids("1".to_owned(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
@ -919,161 +909,6 @@ pub(super) mod test {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_popular() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Popular)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.is_user_personal_stats_popular());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_all() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::All)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.is_user_personal_stats_full());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_cat_attacking() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Attacking)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.try_as_user_personal_stats_category()
|
||||
.unwrap()
|
||||
.personalstats
|
||||
.is_personal_stats_attacking_public());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_cat_jobs() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Jobs)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.try_as_user_personal_stats_category()
|
||||
.unwrap()
|
||||
.personalstats
|
||||
.is_personal_stats_jobs_public());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_cat_trading() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Trading)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.try_as_user_personal_stats_category()
|
||||
.unwrap()
|
||||
.personalstats
|
||||
.is_personal_stats_trading());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_cat_jail() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Jail)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.try_as_user_personal_stats_category()
|
||||
.unwrap()
|
||||
.personalstats
|
||||
.is_personal_stats_jail());
|
||||
}
|
||||
|
||||
#[cfg(feature = "strum")]
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_cat_hospital() {
|
||||
let client = test_client().await;
|
||||
|
||||
let resp = client
|
||||
.user()
|
||||
.for_selections(|b| {
|
||||
b.selections([UserSelectionNameVariant::Personalstats])
|
||||
.cat(PersonalStatsCategoryEnum::Hospital)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(resp
|
||||
.user_personal_stats_response()
|
||||
.unwrap()
|
||||
.try_as_user_personal_stats_category()
|
||||
.unwrap()
|
||||
.personalstats
|
||||
.is_personal_stats_hospital());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_personalstats_for_id() {
|
||||
let client = test_client().await;
|
||||
|
@ -1119,25 +954,4 @@ pub(super) mod test {
|
|||
|
||||
client.user().attacks(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn user_reports() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.user().reports(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn key_info() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.key().info(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn key_log() {
|
||||
let client = test_client().await;
|
||||
|
||||
client.key().log(|b| b).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "torn-key-pool"
|
||||
version = "1.1.3"
|
||||
version = "1.0.1"
|
||||
edition = "2021"
|
||||
authors = ["Pyrit [2111649]"]
|
||||
license-file = { workspace = true }
|
||||
|
@ -11,10 +11,10 @@ description = "A generalised API key pool for torn-api"
|
|||
[features]
|
||||
default = ["postgres", "tokio-runtime"]
|
||||
postgres = ["dep:sqlx", "dep:chrono", "dep:indoc"]
|
||||
tokio-runtime = ["dep:tokio", "dep:rand", "dep:tokio-stream"]
|
||||
tokio-runtime = ["dep:tokio", "dep:rand"]
|
||||
|
||||
[dependencies]
|
||||
torn-api = { path = "../torn-api", default-features = false, version = "1.1.1" }
|
||||
torn-api = { path = "../torn-api", default-features = false, version = "1.0.1" }
|
||||
thiserror = "2"
|
||||
|
||||
sqlx = { version = "0.8", features = [
|
||||
|
@ -30,9 +30,6 @@ indoc = { version = "2", optional = true }
|
|||
tokio = { version = "1", optional = true, default-features = false, features = [
|
||||
"time",
|
||||
] }
|
||||
tokio-stream = { version = "0.1", optional = true, default-features = false, features = [
|
||||
"time",
|
||||
] }
|
||||
rand = { version = "0.9", optional = true }
|
||||
futures = "0.3"
|
||||
reqwest = { version = "0.12", default-features = false, features = [
|
||||
|
|
|
@ -3,14 +3,13 @@
|
|||
#[cfg(feature = "postgres")]
|
||||
pub mod postgres;
|
||||
|
||||
use std::{collections::HashMap, future::Future, ops::Deref, sync::Arc, time::Duration};
|
||||
use std::{collections::HashMap, future::Future, sync::Arc, time::Duration};
|
||||
|
||||
use futures::{future::BoxFuture, FutureExt, Stream, StreamExt};
|
||||
use futures::{future::BoxFuture, FutureExt};
|
||||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION};
|
||||
use serde::Deserialize;
|
||||
use tokio_stream::StreamExt as TokioStreamExt;
|
||||
use torn_api::{
|
||||
executor::{BulkExecutor, Executor},
|
||||
executor::Executor,
|
||||
request::{ApiRequest, ApiResponse},
|
||||
ApiError,
|
||||
};
|
||||
|
@ -81,46 +80,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<K, D> From<&str> for KeySelector<K, D>
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
{
|
||||
fn from(value: &str) -> Self {
|
||||
Self::Key(value.to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, D> From<D> for KeySelector<K, D>
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
{
|
||||
fn from(value: D) -> Self {
|
||||
Self::Has(vec![value])
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, D> From<&[D]> for KeySelector<K, D>
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
{
|
||||
fn from(value: &[D]) -> Self {
|
||||
Self::Has(value.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, D> From<Vec<D>> for KeySelector<K, D>
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
{
|
||||
fn from(value: Vec<D>) -> Self {
|
||||
Self::Has(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoSelector<K, D>: Send
|
||||
where
|
||||
K: ApiKey,
|
||||
|
@ -129,35 +88,30 @@ where
|
|||
fn into_selector(self) -> KeySelector<K, D>;
|
||||
}
|
||||
|
||||
impl<K, D, T> IntoSelector<K, D> for T
|
||||
impl<K, D> IntoSelector<K, D> for D
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
T: Into<KeySelector<K, D>> + Send,
|
||||
{
|
||||
fn into_selector(self) -> KeySelector<K, D> {
|
||||
self.into()
|
||||
KeySelector::Has(vec![self])
|
||||
}
|
||||
}
|
||||
|
||||
pub trait KeyPoolError:
|
||||
From<reqwest::Error> + From<serde_json::Error> + From<torn_api::ApiError> + From<Arc<Self>> + Send
|
||||
{
|
||||
}
|
||||
|
||||
impl<T> KeyPoolError for T where
|
||||
T: From<reqwest::Error>
|
||||
+ From<serde_json::Error>
|
||||
+ From<torn_api::ApiError>
|
||||
+ From<Arc<Self>>
|
||||
+ Send
|
||||
impl<K, D> IntoSelector<K, D> for KeySelector<K, D>
|
||||
where
|
||||
K: ApiKey,
|
||||
D: KeyDomain,
|
||||
{
|
||||
fn into_selector(self) -> KeySelector<K, D> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub trait KeyPoolStorage: Send + Sync {
|
||||
type Key: ApiKey;
|
||||
type Domain: KeyDomain;
|
||||
type Error: KeyPoolError;
|
||||
type Error: From<reqwest::Error> + From<serde_json::Error> + From<torn_api::ApiError> + Send;
|
||||
|
||||
fn acquire_key<S>(
|
||||
&self,
|
||||
|
@ -252,6 +206,65 @@ where
|
|||
>,
|
||||
}
|
||||
|
||||
pub struct KeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pool: &'p KeyPool<S>,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
}
|
||||
|
||||
impl<'p, S> KeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pub fn new(pool: &'p KeyPool<S>, selector: KeySelector<S::Key, S::Domain>) -> Self {
|
||||
Self { pool, selector }
|
||||
}
|
||||
|
||||
async fn execute_request<D>(&self, request: ApiRequest<D>) -> Result<ApiResponse<D>, S::Error>
|
||||
where
|
||||
D: Send,
|
||||
{
|
||||
let key = self.pool.storage.acquire_key(self.selector.clone()).await?;
|
||||
|
||||
let mut headers = HeaderMap::with_capacity(1);
|
||||
headers.insert(
|
||||
AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!("ApiKey {}", key.value())).unwrap(),
|
||||
);
|
||||
|
||||
let resp = self
|
||||
.pool
|
||||
.client
|
||||
.get(request.url())
|
||||
.headers(headers)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let status = resp.status();
|
||||
|
||||
let bytes = resp.bytes().await?;
|
||||
|
||||
if let Some(err) = decode_error(&bytes)? {
|
||||
if let Some(handler) = self.pool.options.error_hooks.get(&err.code()) {
|
||||
let retry = (*handler)(&self.pool.storage, &key).await?;
|
||||
|
||||
if retry {
|
||||
return Box::pin(self.execute_request(request)).await;
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
} else {
|
||||
Ok(ApiResponse {
|
||||
discriminant: request.disriminant,
|
||||
body: Some(bytes),
|
||||
status,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PoolBuilder<S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
|
@ -345,147 +358,20 @@ where
|
|||
|
||||
pub fn build(self) -> KeyPool<S> {
|
||||
KeyPool {
|
||||
inner: Arc::new(KeyPoolInner {
|
||||
client: self.client,
|
||||
storage: self.storage,
|
||||
options: self.options,
|
||||
}),
|
||||
client: self.client,
|
||||
storage: self.storage,
|
||||
options: Arc::new(self.options),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KeyPoolInner<S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pub client: reqwest::Client,
|
||||
pub storage: S,
|
||||
pub options: PoolOptions<S>,
|
||||
}
|
||||
|
||||
impl<S> KeyPoolInner<S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
async fn execute_with_key(
|
||||
&self,
|
||||
key: &S::Key,
|
||||
request: &ApiRequest,
|
||||
) -> Result<RequestResult, S::Error> {
|
||||
let mut headers = HeaderMap::with_capacity(1);
|
||||
headers.insert(
|
||||
AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!("ApiKey {}", key.value())).unwrap(),
|
||||
);
|
||||
|
||||
let resp = self
|
||||
.client
|
||||
.get(request.url())
|
||||
.headers(headers)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let status = resp.status();
|
||||
|
||||
let bytes = resp.bytes().await?;
|
||||
|
||||
if let Some(err) = decode_error(&bytes)? {
|
||||
if let Some(handler) = self.options.error_hooks.get(&err.code()) {
|
||||
let retry = (*handler)(&self.storage, key).await?;
|
||||
|
||||
if retry {
|
||||
return Ok(RequestResult::Retry);
|
||||
}
|
||||
}
|
||||
Err(err.into())
|
||||
} else {
|
||||
Ok(RequestResult::Response(ApiResponse {
|
||||
body: Some(bytes),
|
||||
status,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
async fn execute_request(
|
||||
&self,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
request: ApiRequest,
|
||||
) -> Result<ApiResponse, S::Error> {
|
||||
loop {
|
||||
let key = self.storage.acquire_key(selector.clone()).await?;
|
||||
match self.execute_with_key(&key, &request).await {
|
||||
Ok(RequestResult::Response(resp)) => return Ok(resp),
|
||||
Ok(RequestResult::Retry) => (),
|
||||
Err(why) => return Err(why),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn execute_bulk_requests<D, T: IntoIterator<Item = (D, ApiRequest)>>(
|
||||
&self,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
requests: T,
|
||||
) -> impl Stream<Item = (D, Result<ApiResponse, S::Error>)> + use<'_, D, S, T> {
|
||||
let requests: Vec<_> = requests.into_iter().collect();
|
||||
|
||||
let keys: Vec<_> = match self
|
||||
.storage
|
||||
.acquire_many_keys(selector.clone(), requests.len() as i64)
|
||||
.await
|
||||
{
|
||||
Ok(keys) => keys.into_iter().map(Ok).collect(),
|
||||
Err(why) => {
|
||||
let why = Arc::new(why);
|
||||
std::iter::repeat_n(why, requests.len())
|
||||
.map(|e| Err(S::Error::from(e)))
|
||||
.collect()
|
||||
}
|
||||
};
|
||||
|
||||
StreamExt::map(
|
||||
futures::stream::iter(std::iter::zip(requests, keys)),
|
||||
move |((discriminant, request), mut maybe_key)| {
|
||||
let selector = selector.clone();
|
||||
async move {
|
||||
loop {
|
||||
let key = match maybe_key {
|
||||
Ok(key) => key,
|
||||
Err(why) => return (discriminant, Err(why)),
|
||||
};
|
||||
match self.execute_with_key(&key, &request).await {
|
||||
Ok(RequestResult::Response(resp)) => return (discriminant, Ok(resp)),
|
||||
Ok(RequestResult::Retry) => (),
|
||||
Err(why) => return (discriminant, Err(why)),
|
||||
}
|
||||
maybe_key = self.storage.acquire_key(selector.clone()).await;
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
.buffer_unordered(25)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KeyPool<S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
inner: Arc<KeyPoolInner<S>>,
|
||||
}
|
||||
|
||||
impl<S> Deref for KeyPool<S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
type Target = KeyPoolInner<S>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
enum RequestResult {
|
||||
Response(ApiResponse),
|
||||
Retry,
|
||||
pub client: reqwest::Client,
|
||||
pub storage: S,
|
||||
pub options: Arc<PoolOptions<S>>,
|
||||
}
|
||||
|
||||
impl<S> KeyPool<S>
|
||||
|
@ -498,17 +384,6 @@ where
|
|||
{
|
||||
KeyPoolExecutor::new(self, selector.into_selector())
|
||||
}
|
||||
|
||||
pub fn throttled_torn_api<I>(
|
||||
&self,
|
||||
selector: I,
|
||||
distance: Duration,
|
||||
) -> ThrottledKeyPoolExecutor<S>
|
||||
where
|
||||
I: IntoSelector<S::Key, S::Domain>,
|
||||
{
|
||||
ThrottledKeyPoolExecutor::new(self, selector.into_selector(), distance)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_error(buf: &[u8]) -> Result<Option<ApiError>, serde_json::Error> {
|
||||
|
@ -534,145 +409,28 @@ fn decode_error(buf: &[u8]) -> Result<Option<ApiError>, serde_json::Error> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct KeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pool: &'p KeyPoolInner<S>,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
}
|
||||
|
||||
impl<'p, S> KeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pub fn new(pool: &'p KeyPool<S>, selector: KeySelector<S::Key, S::Domain>) -> Self {
|
||||
Self {
|
||||
pool: &pool.inner,
|
||||
selector,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Executor for KeyPoolExecutor<'_, S>
|
||||
where
|
||||
S: KeyPoolStorage + 'static,
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
async fn execute<R>(self, request: R) -> (R::Discriminant, Result<ApiResponse, Self::Error>)
|
||||
async fn execute<R>(
|
||||
&self,
|
||||
request: R,
|
||||
) -> Result<torn_api::request::ApiResponse<R::Discriminant>, Self::Error>
|
||||
where
|
||||
R: torn_api::request::IntoRequest,
|
||||
{
|
||||
let (d, request) = request.into_request();
|
||||
let request = request.into_request();
|
||||
|
||||
(d, self.pool.execute_request(self.selector, request).await)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> BulkExecutor for KeyPoolExecutor<'_, S>
|
||||
where
|
||||
S: KeyPoolStorage + 'static,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
fn execute<R>(
|
||||
self,
|
||||
requests: impl IntoIterator<Item = R>,
|
||||
) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
|
||||
where
|
||||
R: torn_api::request::IntoRequest,
|
||||
{
|
||||
let requests: Vec<_> = requests.into_iter().map(|r| r.into_request()).collect();
|
||||
self.pool
|
||||
.execute_bulk_requests(self.selector.clone(), requests)
|
||||
.into_stream()
|
||||
.flatten()
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ThrottledKeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pool: &'p KeyPoolInner<S>,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
distance: Duration,
|
||||
}
|
||||
|
||||
impl<S> Clone for ThrottledKeyPoolExecutor<'_, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
pool: self.pool,
|
||||
selector: self.selector.clone(),
|
||||
distance: self.distance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ThrottledKeyPoolExecutor<'_, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
async fn execute_request(self, request: ApiRequest) -> Result<ApiResponse, S::Error> {
|
||||
self.pool.execute_request(self.selector, request).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<'p, S> ThrottledKeyPoolExecutor<'p, S>
|
||||
where
|
||||
S: KeyPoolStorage,
|
||||
{
|
||||
pub fn new(
|
||||
pool: &'p KeyPool<S>,
|
||||
selector: KeySelector<S::Key, S::Domain>,
|
||||
distance: Duration,
|
||||
) -> Self {
|
||||
Self {
|
||||
pool: &pool.inner,
|
||||
selector,
|
||||
distance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> BulkExecutor for ThrottledKeyPoolExecutor<'_, S>
|
||||
where
|
||||
S: KeyPoolStorage + 'static,
|
||||
{
|
||||
type Error = S::Error;
|
||||
|
||||
fn execute<R>(
|
||||
self,
|
||||
requests: impl IntoIterator<Item = R>,
|
||||
) -> impl futures::Stream<Item = (R::Discriminant, Result<ApiResponse, Self::Error>)> + Unpin
|
||||
where
|
||||
R: torn_api::request::IntoRequest,
|
||||
{
|
||||
let requests: Vec<_> = requests.into_iter().map(|r| r.into_request()).collect();
|
||||
StreamExt::map(
|
||||
futures::stream::iter(requests).throttle(self.distance),
|
||||
move |(d, request)| {
|
||||
let this = self.clone();
|
||||
async move {
|
||||
let result = this.execute_request(request).await;
|
||||
(d, result)
|
||||
}
|
||||
},
|
||||
)
|
||||
.buffer_unordered(25)
|
||||
.boxed()
|
||||
self.execute_request(request).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "postgres")]
|
||||
mod test {
|
||||
use torn_api::executor::{BulkExecutorExt, ExecutorExt};
|
||||
use torn_api::executor::ExecutorExt;
|
||||
|
||||
use crate::postgres;
|
||||
|
||||
|
@ -693,48 +451,4 @@ mod test {
|
|||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn bulk(pool: sqlx::PgPool) {
|
||||
let (storage, _) = postgres::test::setup(pool).await;
|
||||
|
||||
let pool = PoolBuilder::new(storage)
|
||||
.use_default_hooks()
|
||||
.comment("test_runner")
|
||||
.build();
|
||||
|
||||
let responses = pool
|
||||
.torn_api(postgres::test::Domain::All)
|
||||
.faction_bulk()
|
||||
.basic_for_id(vec![19.into(), 89.into()], |b| b);
|
||||
let mut responses: Vec<_> = StreamExt::collect(responses).await;
|
||||
|
||||
let (_id1, basic1) = responses.pop().unwrap();
|
||||
basic1.unwrap();
|
||||
|
||||
let (_id2, basic2) = responses.pop().unwrap();
|
||||
basic2.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn bulk_trottled(pool: sqlx::PgPool) {
|
||||
let (storage, _) = postgres::test::setup(pool).await;
|
||||
|
||||
let pool = PoolBuilder::new(storage)
|
||||
.use_default_hooks()
|
||||
.comment("test_runner")
|
||||
.build();
|
||||
|
||||
let responses = pool
|
||||
.throttled_torn_api(postgres::test::Domain::All, Duration::from_millis(500))
|
||||
.faction_bulk()
|
||||
.basic_for_id(vec![19.into(), 89.into()], |b| b);
|
||||
let mut responses: Vec<_> = StreamExt::collect(responses).await;
|
||||
|
||||
let (_id1, basic1) = responses.pop().unwrap();
|
||||
basic1.unwrap();
|
||||
|
||||
let (_id2, basic2) = responses.pop().unwrap();
|
||||
basic2.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use futures::future::BoxFuture;
|
||||
use indoc::formatdoc;
|
||||
use sqlx::{FromRow, PgPool, Postgres, QueryBuilder};
|
||||
|
@ -39,9 +37,6 @@ where
|
|||
|
||||
#[error("Key not found: '{0:?}'")]
|
||||
KeyNotFound(KeySelector<PgKey<D>, D>),
|
||||
|
||||
#[error("Failed to acquire keys in bulk: {0}")]
|
||||
Bulk(#[from] Arc<Self>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, FromRow)]
|
||||
|
|
Loading…
Reference in a new issue