mod address;
mod composite;
mod error;
mod hostname;
mod input;
pub mod json;
mod list;
mod primitive;
mod string;
use std::borrow::Cow;
use std::sync::Arc;
use crate::{compose, network, scalar, text};
pub use address::{Domain, Ip, IpVersion, Port, Restriction, Url};
pub use composite::{AllOf, AnyOf, Multiple, NoneOf};
pub use error::Error;
pub use hostname::{Host, Hostname, Label};
pub use input::{Entries, Input, View};
pub use json::Json;
pub use list::{ArrayList, Assoc, Nullable, WhiteList};
pub use primitive::{Boolean, Float, Integer, Numeric, Range};
pub use string::{Contains, Globstar, HexColor, Identifier, Phone, Text, Wildcard};
pub use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Type {
Boolean,
Integer,
Float,
String,
Array,
Object,
Mixed,
}
impl Type {
pub const fn as_str(self) -> &'static str {
match self {
Type::Boolean => "boolean",
Type::Integer => "integer",
Type::Float => "double",
Type::String => "string",
Type::Array => "array",
Type::Object => "object",
Type::Mixed => "mixed",
}
}
pub fn from_php(value: &str) -> Option<Self> {
Some(match value {
"boolean" => Type::Boolean,
"integer" => Type::Integer,
"double" => Type::Float,
"string" => Type::String,
"array" => Type::Array,
"object" => Type::Object,
"mixed" => Type::Mixed,
_ => return None,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct Verdict {
pub valid: bool,
pub description: Option<Cow<'static, str>>,
pub decided: bool,
pub rule: Option<usize>,
}
impl Verdict {
pub const VALID: Verdict = Verdict::of(true);
pub const INVALID: Verdict = Verdict::of(false);
pub const fn of(valid: bool) -> Self {
Verdict { valid, description: None, decided: false, rule: None }
}
pub fn decided(valid: bool, description: Option<Cow<'static, str>>) -> Self {
Verdict { valid, description, decided: true, rule: None }
}
pub fn into_description(self, validator: &(impl Validator + ?Sized)) -> Option<Cow<'static, str>> {
self.description.or_else(|| Some(Cow::Owned(validator.rule_description(self.rule?)?)))
}
pub fn describe(&self, validator: &(impl Validator + ?Sized)) -> String {
match &self.description {
Some(d) => d.clone().into_owned(),
None => self.rule.and_then(|i| validator.rule_description(i)).unwrap_or_else(|| validator.description()),
}
}
pub fn try_describe(&self, validator: &(impl Validator + ?Sized)) -> Result<String, Error> {
match &self.description {
Some(d) => Ok(d.clone().into_owned()),
None => match self.rule.and_then(|i| validator.rule_description(i)) {
Some(d) => Ok(d),
None => validator.try_description(),
},
}
}
}
pub trait Validator: Send + Sync {
fn description(&self) -> String;
fn is_valid(&self, value: &Value) -> bool;
fn validate(&self, value: Input<'_>) -> Result<Verdict, Error> {
Ok(Verdict::of(match value {
Input::Json(v) => self.is_valid(v),
Input::Value(z) => z.try_to_json().is_some_and(|v| self.is_valid(&v)),
}))
}
fn check(&self, value: &Value) -> Result<(), String> {
match self.validate(Input::Json(value)) {
Ok(v) if v.valid => Ok(()),
Ok(v) => Err(v.describe(self)),
Err(_) => Err(self.description()),
}
}
fn is_array(&self) -> bool {
false
}
fn kind(&self) -> Type {
Type::Mixed
}
fn accepts_null(&self) -> bool {
self.is_valid(&Value::Null)
}
fn try_description(&self) -> Result<String, Error> {
Ok(self.description())
}
fn rule_description(&self, index: usize) -> Option<String> {
let _ = index;
None
}
fn as_range(&self) -> Option<&Range> {
None
}
}
macro_rules! is_valid_via_validate {
() => {
fn is_valid(&self, value: &serde_json::Value) -> bool {
self.validate($crate::object::Input::Json(value)).is_ok_and(|v| v.valid)
}
};
}
pub(crate) use is_valid_via_validate;
macro_rules! forward_validator {
($($ty:ty),*) => {$(
impl<V: Validator + ?Sized> Validator for $ty {
fn description(&self) -> String {
(**self).description()
}
fn is_valid(&self, value: &Value) -> bool {
(**self).is_valid(value)
}
fn validate(&self, value: Input<'_>) -> Result<Verdict, Error> {
(**self).validate(value)
}
fn check(&self, value: &Value) -> Result<(), String> {
(**self).check(value)
}
fn is_array(&self) -> bool {
(**self).is_array()
}
fn kind(&self) -> Type {
(**self).kind()
}
fn accepts_null(&self) -> bool {
(**self).accepts_null()
}
fn try_description(&self) -> Result<String, Error> {
(**self).try_description()
}
fn rule_description(&self, index: usize) -> Option<String> {
(**self).rule_description(index)
}
fn as_range(&self) -> Option<&Range> {
(**self).as_range()
}
}
)*};
}
forward_validator!(&V, Box<V>, Arc<V>);
pub fn number_format(n: i128) -> String {
let negative = n < 0;
let digits = n.unsigned_abs().to_string();
let mut out = String::with_capacity(digits.len() + digits.len() / 3 + 1);
if negative {
out.push('-');
}
let first = digits.len() % 3;
for (i, ch) in digits.chars().enumerate() {
if i != 0 && (i + 3 - first).is_multiple_of(3) {
out.push(',');
}
out.push(ch);
}
out
}