feat: Initial commit
This commit is contained in:
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