use crate::json_schema::generate_json_schema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[repr(transparent)]
pub struct SchemaRef(Arc<Value>);
impl From<Value> for SchemaRef {
fn from(value: Value) -> Self {
SchemaRef(Arc::new(value))
}
}
impl AsRef<Value> for SchemaRef {
fn as_ref(&self) -> &Value {
&self.0
}
}
impl schemars::JsonSchema for SchemaRef {
fn schema_name() -> std::borrow::Cow<'static, str> {
"Schema".into()
}
fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "object",
"additionalProperties": true,
"description": "A valid JSON Schema object."
})
}
}
impl SchemaRef {
pub fn for_type<T: schemars::JsonSchema>() -> Self {
let json_schema = generate_json_schema::<T>();
json_schema.into()
}
pub fn parse_json(s: &str) -> Result<Self, serde_json::Error> {
let value = serde_json::from_str::<Value>(s)?;
Ok(value.into())
}
pub fn as_value(&self) -> &Value {
&self.0
}
}