use crate::prelude::*;
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum SchemaKind {
Object,
Array,
String,
Number,
Integer,
Boolean,
Null,
}
impl Default for SchemaKind {
fn default() -> Self {
Self::Object
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Schema {
#[serde(rename = "type")]
pub kind: SchemaKind,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "enum")]
pub variants: Option<HashSet<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub items: Option<Box<Schema>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<HashMap<String, Box<Schema>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub required: Option<HashSet<String>>,
#[serde(rename = "additionalProperties")]
#[serde(skip_serializing_if = "Option::is_none")]
pub additional_properties: Option<bool>,
}
impl Schema {
pub fn new(kind: SchemaKind, descr: impl Into<String>) -> Self {
Self {
kind,
description: match descr.into() {
s if !s.is_empty() => Some(s),
_ => None,
},
..Default::default()
}
}
pub fn object(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Object, descr)
}
pub fn array(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Array, descr)
}
pub fn string(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::String, descr)
}
pub fn number(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Number, descr)
}
pub fn integer(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Integer, descr)
}
pub fn boolean(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Boolean, descr)
}
pub fn null(descr: impl Into<String>) -> Self {
Self::new(SchemaKind::Null, descr)
}
pub fn set_description(&mut self, descr: impl Into<String>) {
self.description.replace(descr.into());
}
pub fn description(mut self, descr: impl Into<String>) -> Self {
self.set_description(descr);
self
}
pub fn set_variants(&mut self, vars: HashSet<String>) {
self.variants.get_or_insert_default().extend(vars);
}
pub fn variants(mut self, vars: HashSet<String>) -> Self {
self.set_variants(vars);
self
}
pub fn set_variant(&mut self, var: impl Into<String>) {
self.variants.get_or_insert_default().insert(var.into());
}
pub fn variant(mut self, var: impl Into<String>) -> Self {
self.set_variant(var);
self
}
pub fn set_minimum(&mut self, min: f64) {
self.minimum.replace(min);
}
pub fn minimum(mut self, min: f64) -> Self {
self.set_minimum(min);
self
}
pub fn set_maximum(&mut self, max: f64) {
self.maximum.replace(max);
}
pub fn maximum(mut self, max: f64) -> Self {
self.set_maximum(max);
self
}
pub fn set_items(&mut self, schema: Schema) {
self.items.replace(Box::new(schema));
}
pub fn items(mut self, schema: Schema) -> Self {
self.set_items(schema);
self
}
pub fn set_properties(&mut self, props: HashMap<impl Into<String>, Box<Schema>>) {
self.properties
.get_or_insert_default()
.extend(props.into_iter().map(|(k, v)| (k.into(), v)));
}
pub fn properties(mut self, props: HashMap<impl Into<String>, Box<Schema>>) -> Self {
self.set_properties(props);
self
}
pub fn set_property(&mut self, name: impl Into<String>, schema: Schema, required: bool) {
let name = name.into();
let reqs = self.required.get_or_insert_default();
if required {
reqs.insert(name.clone());
}
self.properties
.get_or_insert_default()
.insert(name, Box::new(schema));
}
pub fn property(mut self, name: impl Into<String>, schema: Schema, required: bool) -> Self {
self.set_property(name, schema, required);
self
}
pub fn set_required_property(&mut self, name: impl Into<String>, schema: Schema) {
self.set_property(name, schema, true);
}
pub fn required_property(self, name: impl Into<String>, schema: Schema) -> Self {
self.property(name, schema, true)
}
pub fn set_optional_property(&mut self, name: impl Into<String>, schema: Schema) {
self.set_property(name, schema, false);
}
pub fn optional_property(self, name: impl Into<String>, schema: Schema) -> Self {
self.property(name, schema, false)
}
pub fn set_required(&mut self, reqs: Vec<impl Into<String>>) {
self.required
.get_or_insert_default()
.extend(reqs.into_iter().map(Into::into));
}
pub fn required(mut self, reqs: Vec<impl Into<String>>) -> Self {
self.set_required(reqs);
self
}
pub fn set_require(&mut self, name: impl Into<String>) {
self.required.get_or_insert_default().insert(name.into());
}
pub fn require(mut self, name: impl Into<String>) -> Self {
self.set_require(name);
self
}
}