Skip to content

Commit

Permalink
fix: fix to compile error json schema feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Dec 14, 2024
1 parent 1ff69e4 commit 9d2e09f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
7 changes: 6 additions & 1 deletion crates/serde_hkx_features/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 4 additions & 10 deletions crates/serde_hkx_features/src/json_schema_gen.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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::<ClassPtrMap>();
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)
}

Expand Down
10 changes: 8 additions & 2 deletions crates/serde_hkx_features/src/types_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>,
#[serde(default = "default_schema_url")]
schema: Option<Cow<'a, str>>,
#[serde(flatten)]
#[serde(borrow)]
classes: IndexMap<Pointer, Classes<'a>>,
}

#[inline]
const fn default_schema_url() -> Option<Cow<'static, str>> {
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 {
Expand Down

0 comments on commit 9d2e09f

Please sign in to comment.