use std::fmt;
use std::sync::Arc;
use crate::schema::parse::Unknown;
#[derive(Clone, Debug, PartialEq)]
pub struct SchemaAst {
pub kind: SchemaKind,
pub constraints: Constraints,
pub annotations: Annotations,
}
impl SchemaAst {
pub fn new(kind: SchemaKind) -> Self {
Self {
kind,
constraints: Constraints::default(),
annotations: Annotations::default(),
}
}
pub fn any() -> Self {
Self::new(SchemaKind::Any)
}
pub fn with_constraints(mut self, constraints: Constraints) -> Self {
self.constraints = constraints;
self
}
pub fn with_annotations(mut self, annotations: Annotations) -> Self {
self.annotations = annotations;
self
}
}
impl Default for SchemaAst {
fn default() -> Self {
Self::any()
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum SchemaKind {
Any,
Boolean,
Integer,
Number,
String,
Literal(Unknown),
Array(Box<SchemaAst>),
Tuple(Vec<SchemaAst>),
Struct(Vec<Field>),
Record(Box<SchemaAst>),
Nullable(Box<SchemaAst>),
Union(Vec<SchemaAst>),
Lazy(LazyAst),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Field {
pub name: String,
pub schema: SchemaAst,
pub required: bool,
}
impl Field {
pub fn required(name: impl Into<String>, schema: SchemaAst) -> Self {
Self {
name: name.into(),
schema,
required: true,
}
}
pub fn optional(name: impl Into<String>, schema: SchemaAst) -> Self {
Self {
name: name.into(),
schema,
required: false,
}
}
}
#[derive(Clone)]
pub struct LazyAst {
name: Option<String>,
thunk: Arc<dyn Fn() -> SchemaAst + Send + Sync>,
}
impl LazyAst {
pub fn new(name: Option<String>, thunk: impl Fn() -> SchemaAst + Send + Sync + 'static) -> Self {
Self {
name,
thunk: Arc::new(thunk),
}
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn force(&self) -> SchemaAst {
(self.thunk)()
}
}
impl fmt::Debug for LazyAst {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LazyAst").field("name", &self.name).finish()
}
}
impl PartialEq for LazyAst {
fn eq(&self, other: &Self) -> bool {
match (&self.name, &other.name) {
(Some(a), Some(b)) => a == b,
(None, None) => Arc::ptr_eq(&self.thunk, &other.thunk),
_ => false,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Constraints {
pub min_length: Option<usize>,
pub max_length: Option<usize>,
pub pattern: Option<String>,
pub format: Option<String>,
pub minimum: Option<f64>,
pub maximum: Option<f64>,
pub exclusive_minimum: Option<f64>,
pub exclusive_maximum: Option<f64>,
pub min_items: Option<usize>,
pub max_items: Option<usize>,
pub enum_values: Option<Vec<Unknown>>,
}
impl Constraints {
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Annotations {
pub title: Option<String>,
pub description: Option<String>,
pub examples: Vec<Unknown>,
pub default: Option<Unknown>,
pub deprecated: bool,
}
impl Annotations {
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
}
pub(crate) fn tighten_min<T: PartialOrd + Copy>(slot: &mut Option<T>, new: T) {
*slot = Some(match *slot {
Some(old) if old > new => old,
_ => new,
});
}
pub(crate) fn tighten_max<T: PartialOrd + Copy>(slot: &mut Option<T>, new: T) {
*slot = Some(match *slot {
Some(old) if old < new => old,
_ => new,
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_ast_has_no_constraints_or_annotations() {
let ast = SchemaAst::new(SchemaKind::String);
assert!(ast.constraints.is_empty());
assert!(ast.annotations.is_empty());
assert_eq!(SchemaAst::default().kind, SchemaKind::Any);
}
#[test]
fn field_constructors_set_required() {
let req = Field::required("a", SchemaAst::any());
let opt = Field::optional("b", SchemaAst::any());
assert!(req.required && !opt.required);
assert_eq!(opt.name, "b");
}
#[test]
fn tighten_keeps_the_stricter_bound() {
let mut lo = Some(5_usize);
tighten_min(&mut lo, 3);
assert_eq!(lo, Some(5));
tighten_min(&mut lo, 8);
assert_eq!(lo, Some(8));
let mut hi = None;
tighten_max(&mut hi, 10.0);
tighten_max(&mut hi, 20.0);
assert_eq!(hi, Some(10.0));
}
#[test]
fn lazy_equality_by_name_or_identity() {
let a = LazyAst::new(Some("T".into()), SchemaAst::any);
let b = LazyAst::new(Some("T".into()), || SchemaAst::new(SchemaKind::String));
assert_eq!(a, b);
let c = LazyAst::new(None, SchemaAst::any);
assert_eq!(c, c.clone());
assert_ne!(c, LazyAst::new(None, SchemaAst::any));
assert_ne!(a, c);
assert_eq!(a.name(), Some("T"));
assert_eq!(b.force().kind, SchemaKind::String);
assert!(format!("{a:?}").contains("T"));
}
}