#![warn(unused_extern_crates)]
#![cfg_attr(feature = "portable", feature(portable_simd))]
#![deny(
clippy::all,
clippy::unwrap_used,
clippy::unnecessary_unwrap,
clippy::pedantic,
missing_docs
)]
#![allow(clippy::module_name_repetitions)]
#[cfg(all(feature = "128bit", feature = "c-abi"))]
compile_error!(
"Combining the features `128bit` and `c-abi` is impossible because i128's \
ABI is unstable (see \
https://github.com/rust-lang/unsafe-code-guidelines/issues/119). Please \
use only one of them in order to compile this crate. If you don't know \
where this error is coming from, it's possible that you depend on \
value-trait twice indirectly, once with the `c-abi` feature, and once with \
the `128bit` feature, and that they have been merged by Cargo."
);
#[cfg(all(feature = "c-abi", feature = "ordered-float"))]
compile_error!(
"Combining the features `c-abi` and `ordered-float` is impossible because ordered_float::OrderedFloat does not implement `StableAbi`"
);
use std::borrow::Cow;
use std::fmt;
mod array;
pub mod generator;
mod impls;
mod node;
mod object;
mod option;
pub mod prelude;
pub mod base;
pub mod derived;
pub use node::StaticNode;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AccessError {
NotAnObject,
NotAnArray,
}
impl fmt::Display for AccessError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotAnArray => write!(f, "The value is not an array"),
Self::NotAnObject => write!(f, "The value is not an object"),
}
}
}
impl std::error::Error for AccessError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExtendedValueType {
I32,
I16,
I8,
U32,
U16,
U8,
Usize,
F32,
Char,
None,
}
impl Default for ExtendedValueType {
fn default() -> Self {
Self::None
}
}
impl fmt::Display for ExtendedValueType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::I32 => write!(f, "i32"),
Self::I16 => write!(f, "i16"),
Self::I8 => write!(f, "i8"),
Self::U32 => write!(f, "u32"),
Self::U16 => write!(f, "u16"),
Self::U8 => write!(f, "u8"),
Self::Usize => write!(f, "usize"),
Self::F32 => write!(f, "f32"),
Self::Char => write!(f, "char"),
Self::None => write!(f, "none"),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ValueType {
Null,
Bool,
I64,
I128,
U64,
U128,
F64,
String,
Array,
Object,
Extended(ExtendedValueType),
#[cfg(feature = "custom-types")]
Custom(&'static str),
}
impl fmt::Display for ValueType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Null => write!(f, "null"),
Self::Bool => write!(f, "bool"),
Self::I64 => write!(f, "i64"),
Self::I128 => write!(f, "i128"),
Self::U64 => write!(f, "u64"),
Self::U128 => write!(f, "u128"),
Self::F64 => write!(f, "f64"),
Self::String => write!(f, "string"),
Self::Array => write!(f, "array"),
Self::Object => write!(f, "object"),
Self::Extended(ty) => write!(f, "{ty}"),
#[cfg(feature = "custom-types")]
Self::Custom(name) => write!(f, "{name}"),
}
}
}
impl Default for ValueType {
fn default() -> Self {
Self::Null
}
}
#[allow(clippy::trait_duplication_in_bounds)] pub trait ValueBuilder<'input>:
Default
+ From<StaticNode>
+ From<i8>
+ From<i16>
+ From<i32>
+ From<i64>
+ From<u8>
+ From<u16>
+ From<u32>
+ From<u64>
+ From<f32>
+ From<f64>
+ From<bool>
+ From<()>
+ From<String>
+ From<&'input str>
+ From<Cow<'input, str>>
{
fn array_with_capacity(capacity: usize) -> Self;
fn object_with_capacity(capacity: usize) -> Self;
#[must_use]
fn array() -> Self {
Self::array_with_capacity(0)
}
#[must_use]
fn object() -> Self {
Self::object_with_capacity(0)
}
fn null() -> Self;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TryTypeError {
pub expected: ValueType,
pub got: ValueType,
}
impl std::fmt::Display for TryTypeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Expected type {}, got {}", self.expected, self.got)
}
}
impl std::error::Error for TryTypeError {}