diff --git a/crates/serde_hkx_features/src/error.rs b/crates/serde_hkx_features/src/error.rs index 8a1e0a58e..17b8c1b05 100644 --- a/crates/serde_hkx_features/src/error.rs +++ b/crates/serde_hkx_features/src/error.rs @@ -97,11 +97,16 @@ pub enum Error { }, // Extra formats - #[cfg(any(feature = "extra_fmt", feature = "json_schema"))] + #[cfg(feature = "extra_fmt")] #[snafu(transparent)] ExtraSerdeError { source: crate::serde_extra::error::ExtraSerdeError, }, + + // Extra formats + #[cfg(feature = "json_schema")] + #[snafu(transparent)] + JsonError { source: simd_json::Error }, } /// `Result` for `serde_hkx_features` crate. diff --git a/crates/serde_hkx_features/src/json_schema_gen.rs b/crates/serde_hkx_features/src/json_schema_gen.rs index 046c9afd4..35ea790bd 100644 --- a/crates/serde_hkx_features/src/json_schema_gen.rs +++ b/crates/serde_hkx_features/src/json_schema_gen.rs @@ -1,21 +1,15 @@ //! Json schema generator. -use crate::error::{Error, Result}; +use crate::error::Result; use crate::types_wrapper::ClassPtrMap; +use schemars::schema_for; /// Generate json schema. /// /// # Errors /// - When parse Json failed. pub fn generate_json_schema() -> Result { - let schema_settings = schemars::generate::SchemaSettings::draft2020_12(); - let mut generator = schemars::SchemaGenerator::new(schema_settings); - let def = generator.definitions_mut(); - def.insert("$schema".into(), Some("").into()); - let schema = generator.into_root_schema_for::(); - let schema = simd_json::to_string_pretty(&schema).map_err(|e| Error::JsonError { - input: std::path::PathBuf::new(), - source: e, - })?; + let schema = schema_for!(ClassPtrMap); + let schema = simd_json::to_string_pretty(&schema)?; Ok(schema) } diff --git a/crates/serde_hkx_features/src/types_wrapper.rs b/crates/serde_hkx_features/src/types_wrapper.rs index 83721caa6..c379be29e 100644 --- a/crates/serde_hkx_features/src/types_wrapper.rs +++ b/crates/serde_hkx_features/src/types_wrapper.rs @@ -6,6 +6,7 @@ use havok_types::Pointer; use indexmap::IndexMap; use rayon::prelude::*; use serde::{Deserialize, Serialize}; +use std::borrow::Cow; /// - key: class index(e.g `#0001`) /// - value: C++ Class @@ -14,13 +15,18 @@ use serde::{Deserialize, Serialize}; pub struct ClassPtrMap<'a> { /// Path of Json schema #[cfg_attr(feature = "json_schema", schemars(rename = "$schema"))] - #[serde(default)] - schema: Option, + #[serde(default = "default_schema_url")] + schema: Option>, #[serde(flatten)] #[serde(borrow)] classes: IndexMap>, } +#[inline] +const fn default_schema_url() -> Option> { + Some(Cow::Borrowed("https://raw.githubusercontent.com/SARDONYX-sard/serde-hkx/refs/tags/0.7.0/assets/serde_hkx_schema.json")) +} + impl<'a> ClassPtrMap<'a> { pub fn from_class_map(value: ClassMap<'a>) -> Self { Self {