feat: Initial commit
This commit is contained in:
1
typegen/.gitignore
vendored
Normal file
1
typegen/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
11
typegen/Cargo.toml
Normal file
11
typegen/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "typegen"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
indexmap = { version = "2.12.0", features = ["serde"] }
|
||||
quote = "1.0.41"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
syn = { version = "2.0.108", features = ["full", "parsing"] }
|
||||
80
typegen/src/main.rs
Normal file
80
typegen/src/main.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use indexmap::IndexMap;
|
||||
use syn::{FieldsNamed, parse_quote, token::Enum};
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
pub struct JellyfinOpenapi {
|
||||
components: Components,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
pub struct Components {
|
||||
schemas: indexmap::IndexMap<String, Schema>,
|
||||
}
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
pub struct Schema {
|
||||
#[serde(rename = "type")]
|
||||
_type: Types,
|
||||
properties: Option<indexmap::IndexMap<String, Property>>,
|
||||
#[serde(rename = "oneOf")]
|
||||
one_of: Option<Vec<EnumVariant>>,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
pub struct EnumVariant {
|
||||
#[serde(rename = "$ref")]
|
||||
_ref: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
pub struct Property {
|
||||
#[serde(rename = "type")]
|
||||
_type: Option<Types>,
|
||||
nullable: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Types {
|
||||
Object,
|
||||
String,
|
||||
Boolean,
|
||||
Array,
|
||||
Integer,
|
||||
Number,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let json = include_str!("../../jellyfin.json");
|
||||
let jellyfin_openapi: JellyfinOpenapi = serde_json::from_str(json).unwrap();
|
||||
let structs: IndexMap<String, Schema> = jellyfin_openapi
|
||||
.components
|
||||
.schemas
|
||||
.iter()
|
||||
.filter(|(_k, v)| v.properties.is_some())
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
let enums: IndexMap<String, Schema> = jellyfin_openapi
|
||||
.components
|
||||
.schemas
|
||||
.iter()
|
||||
.filter(|(k, v)| v.one_of.is_some())
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect();
|
||||
|
||||
let syn_structs: Vec<syn::ItemStruct> = structs
|
||||
.iter()
|
||||
.map(|(key, value)| {
|
||||
let fields = value
|
||||
.properties
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(name, _type)| format!("{}:{}", name, _type.is_));
|
||||
parse_quote! {
|
||||
pub struct #key {
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
Reference in New Issue
Block a user