use std::collections::BTreeMap;
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Simple {
Str,
Float,
Int,
Bool,
}
pub(super) const SIMPLE: [(&str, Simple); 21] = [
("$schema", Simple::Str),
("$id", Simple::Str),
("$comment", Simple::Str),
("format", Simple::Str),
("contentEncoding", Simple::Str),
("contentMediaType", Simple::Str),
("pattern", Simple::Str),
("minimum", Simple::Float),
("multipleOf", Simple::Float),
("exclusiveMaximum", Simple::Float),
("exclusiveMinimum", Simple::Float),
("maximum", Simple::Float),
("minLength", Simple::Int),
("maxLength", Simple::Int),
("minItems", Simple::Int),
("maxItems", Simple::Int),
("minProperties", Simple::Int),
("maxProperties", Simple::Int),
("deprecated", Simple::Bool),
("readOnly", Simple::Bool),
("writeOnly", Simple::Bool),
];
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct Required {
pub(super) strings: Vec<String>,
pub(super) flag: bool,
}
#[derive(Debug, Clone, Default)]
pub(super) struct Schema {
pub(super) types: Vec<String>,
pub(super) required: Required,
pub(super) properties: Option<BTreeMap<String, Schema>>,
pub(super) pattern_properties: Option<BTreeMap<String, Schema>>,
pub(super) definitions: Option<BTreeMap<String, Schema>>,
pub(super) items: Option<Box<Schema>>,
pub(super) any_of: Vec<Schema>,
pub(super) all_of: Vec<Schema>,
pub(super) one_of: Vec<Schema>,
pub(super) not: Option<Box<Schema>>,
pub(super) if_: Option<Box<Schema>>,
pub(super) then: Option<Box<Schema>>,
pub(super) else_: Option<Box<Schema>>,
pub(super) contains: Option<Box<Schema>>,
pub(super) property_names: Option<Box<Schema>>,
pub(super) additional_properties: Option<Value>,
pub(super) additional_items: Option<Value>,
pub(super) dependencies: Option<BTreeMap<String, Value>>,
pub(super) default: Option<Value>,
pub(super) const_: Option<Value>,
pub(super) const_was_set: bool,
pub(super) const_from_value: bool,
pub(super) reference: String,
pub(super) title: String,
pub(super) description: String,
pub(super) examples: Vec<Value>,
pub(super) enum_: Vec<Value>,
pub(super) unique_items: bool,
pub(super) simple: BTreeMap<&'static str, Value>,
pub(super) custom: BTreeMap<String, Value>,
pub(super) has_data: bool,
}
impl Schema {
pub(super) fn str_of(&self, name: &str) -> &str {
self.simple
.get(name)
.and_then(Value::as_str)
.unwrap_or_default()
}
pub(super) fn float_of(&self, name: &str) -> Option<f64> {
self.simple.get(name).and_then(Value::as_f64)
}
pub(super) fn int_of(&self, name: &str) -> Option<i64> {
self.simple.get(name).and_then(Value::as_i64)
}
}