Skip to content

Commit

Permalink
Implement custom JsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamaan476 committed Jan 16, 2025
1 parent 94ae9d6 commit 53ee92d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions modeling-cmds/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use enum_iterator::Sequence;
use parse_display_derive::{Display, FromStr};
use schemars::JsonSchema;
use schemars::{schema::SchemaObject, JsonSchema};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand Down Expand Up @@ -966,10 +966,9 @@ mod tests {
}

/// How a property of an object should be transformed.
#[derive(Clone, Debug, PartialEq, Deserialize, JsonSchema, Serialize)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
#[cfg_attr(feature = "ts-rs", ts(export_to = "ModelingCmd.ts"))]
#[serde(rename = "TransformBy")]
pub struct TransformBy<T> {
/// The scale, or rotation, or translation.
pub property: T,
Expand All @@ -983,6 +982,24 @@ pub struct TransformBy<T> {
pub is_local: bool,
}

impl<T: JsonSchema> JsonSchema for TransformBy<T> {
fn schema_name() -> String {
format!("TransformByFor{}", T::schema_name())
}

fn schema_id() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(format!("{}::TransformBy<{}>", module_path!(), T::schema_id()))
}

fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
SchemaObject {
instance_type: Some(schemars::schema::InstanceType::String.into()),
..Default::default()
}
.into()
}
}

/// Container that holds a translate, rotate and scale.
#[derive(Clone, Debug, PartialEq, Deserialize, JsonSchema, Serialize)]
#[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))]
Expand Down

0 comments on commit 53ee92d

Please sign in to comment.