extern crate self as trilean_json_shape;
mod def;
mod path;
mod pattern;
mod schema;
mod shape;
mod typescript;
mod validate;
mod value;
pub use def::{Class, Constraint, Definitions, FieldDef, Kind, ObjectDef, Registry, ShapeDef};
pub use path::{Issue, Issues, Path, PathSegment};
pub use pattern::{Pattern, PatternError};
pub use schema::{Draft, SchemaOptions, json_schema, schema_definitions, schema_of};
pub use shape::{Map, OpenObject, Shape};
pub use trilean_json_shape_derive::Shape;
pub use typescript::typescript_declarations;
pub use validate::{accepts, check, check_with_work, normalise};
pub use value::{MAX_SAFE_INTEGER, Value, number_to_string};
#[doc(hidden)]
pub mod private {
pub use serde;
use crate::def::{Class, Definitions, ShapeDef};
use crate::path::Path;
use crate::value::Value;
pub const fn class_in(actual: Class, allowed: &[Class]) -> bool {
let mut position = 0;
while position < allowed.len() {
if allowed[position] as u8 == actual as u8 {
return true;
}
position += 1;
}
false
}
const fn same(left: &str, right: &str) -> bool {
let (left, right) = (left.as_bytes(), right.as_bytes());
if left.len() != right.len() {
return false;
}
let mut position = 0;
while position < left.len() {
if left[position] != right[position] {
return false;
}
position += 1;
}
true
}
pub const fn keys_unique(parts: &[&[&str]]) -> bool {
let mut first_part = 0;
while first_part < parts.len() {
let mut first_key = 0;
while first_key < parts[first_part].len() {
let mut second_part = first_part;
while second_part < parts.len() {
let mut second_key = if second_part == first_part {
first_key + 1
} else {
0
};
while second_key < parts[second_part].len() {
if same(parts[first_part][first_key], parts[second_part][second_key]) {
return false;
}
second_key += 1;
}
second_part += 1;
}
first_key += 1;
}
first_part += 1;
}
true
}
pub const fn concat_keys<const N: usize>(parts: &[&[&'static str]]) -> [&'static str; N] {
let mut joined = [""; N];
let mut written = 0;
let mut part = 0;
while part < parts.len() {
let mut key = 0;
while key < parts[part].len() {
joined[written] = parts[part][key];
written += 1;
key += 1;
}
part += 1;
}
assert!(written == N, "the length does not match the keys");
joined
}
pub fn decide(
path: &mut Path,
definitions: &Definitions,
alternative: &ShapeDef,
value: &Value,
) -> bool {
crate::validate::decide(path, definitions, alternative, value)
}
}
#[cfg(test)]
mod tests {
use super::Class;
#[test]
fn messages_quote_strings_as_serde_json_does() {
for text in [
"",
"plain",
"a \"quote\" and a \\ backslash",
"\n\r\t\u{8}\u{c}\u{0}\u{1f}\u{7f}",
"\u{2028}\u{2029}\u{e9}\u{1F600}",
"^[a-z]\\.json$",
] {
assert_eq!(
crate::pattern::json_quote(text),
serde_json::to_string(text).expect("JSON"),
"{text:?}"
);
}
}
use super::private::{class_in, concat_keys, keys_unique};
#[test]
fn classes_are_matched_by_identity() {
assert!(class_in(Class::String, &[Class::Integer, Class::String]));
assert!(!class_in(Class::Named, &[Class::Integer, Class::String]));
}
#[test]
fn keys_are_unique_only_when_no_part_repeats_one() {
assert!(keys_unique(&[&["a", "b"], &["c"], &[]]));
assert!(!keys_unique(&[&["a", "b"], &["c", "a"]]));
assert!(!keys_unique(&[&["a", "a"]]));
}
#[test]
fn keys_are_joined_in_order() {
assert_eq!(
concat_keys::<3>(&[&["a"], &[], &["b", "c"]]),
["a", "b", "c"]
);
}
}