1#![warn(unused_extern_crates)]
4#![cfg_attr(feature = "portable", feature(portable_simd))]
5#![deny(
6 clippy::all,
7 clippy::unwrap_used,
8 clippy::unnecessary_unwrap,
9 clippy::pedantic,
10 missing_docs
11)]
12#![allow(clippy::module_name_repetitions)]
14
15#[cfg(all(feature = "128bit", feature = "c-abi"))]
16compile_error!(
17 "Combining the features `128bit` and `c-abi` is impossible because i128's \
18 ABI is unstable (see \
19 https://github.com/rust-lang/unsafe-code-guidelines/issues/119). Please \
20 use only one of them in order to compile this crate. If you don't know \
21 where this error is coming from, it's possible that you depend on \
22 value-trait twice indirectly, once with the `c-abi` feature, and once with \
23 the `128bit` feature, and that they have been merged by Cargo."
24);
25
26#[cfg(all(feature = "c-abi", feature = "ordered-float"))]
27compile_error!(
28 "Combining the features `c-abi` and `ordered-float` is impossible because ordered_float::OrderedFloat does not implement `StableAbi`"
29);
30
31use std::borrow::Cow;
32use std::fmt;
33
34mod array;
35pub mod generator;
37mod impls;
38mod node;
39mod object;
40mod option;
41pub mod prelude;
43
44pub mod base;
46
47pub mod derived;
49
50pub use node::StaticNode;
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum AccessError {
55 NotAnObject,
59 NotAnArray,
63}
64
65impl fmt::Display for AccessError {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 match self {
68 Self::NotAnArray => write!(f, "The value is not an array"),
69 Self::NotAnObject => write!(f, "The value is not an object"),
70 }
71 }
72}
73impl std::error::Error for AccessError {}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
77pub enum ExtendedValueType {
78 I32,
80 I16,
82 I8,
84 U32,
86 U16,
88 U8,
90 Usize,
92 F32,
94 Char,
96 #[default]
98 None,
99}
100
101impl fmt::Display for ExtendedValueType {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 Self::I32 => write!(f, "i32"),
105 Self::I16 => write!(f, "i16"),
106 Self::I8 => write!(f, "i8"),
107 Self::U32 => write!(f, "u32"),
108 Self::U16 => write!(f, "u16"),
109 Self::U8 => write!(f, "u8"),
110 Self::Usize => write!(f, "usize"),
111 Self::F32 => write!(f, "f32"),
112 Self::Char => write!(f, "char"),
113 Self::None => write!(f, "none"),
114 }
115 }
116}
117
118#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
120pub enum ValueType {
121 #[default]
123 Null,
124 Bool,
126 I64,
128 I128,
130 U64,
132 U128,
134 F64,
136 String,
138 Array,
140 Object,
142 Extended(ExtendedValueType),
144 #[cfg(feature = "custom-types")]
145 Custom(&'static str),
147}
148impl fmt::Display for ValueType {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 match self {
151 Self::Null => write!(f, "null"),
152 Self::Bool => write!(f, "bool"),
153 Self::I64 => write!(f, "i64"),
154 Self::I128 => write!(f, "i128"),
155 Self::U64 => write!(f, "u64"),
156 Self::U128 => write!(f, "u128"),
157 Self::F64 => write!(f, "f64"),
158 Self::String => write!(f, "string"),
159 Self::Array => write!(f, "array"),
160 Self::Object => write!(f, "object"),
161 Self::Extended(ty) => write!(f, "{ty}"),
162 #[cfg(feature = "custom-types")]
163 Self::Custom(name) => write!(f, "{name}"),
164 }
165 }
166}
167
168#[allow(clippy::trait_duplication_in_bounds)] pub trait ValueBuilder<'input>:
171 Default
172 + From<StaticNode>
173 + From<i8>
174 + From<i16>
175 + From<i32>
176 + From<i64>
177 + From<u8>
178 + From<u16>
179 + From<u32>
180 + From<u64>
181 + From<f32>
182 + From<f64>
183 + From<bool>
184 + From<()>
185 + From<String>
186 + From<&'input str>
187 + From<Cow<'input, str>>
188{
189 fn array_with_capacity(capacity: usize) -> Self;
191 fn object_with_capacity(capacity: usize) -> Self;
193 #[must_use]
195 fn array() -> Self {
196 Self::array_with_capacity(0)
197 }
198 #[must_use]
200 fn object() -> Self {
201 Self::object_with_capacity(0)
202 }
203 fn null() -> Self;
205}
206
207#[derive(Clone, Debug, PartialEq, Eq)]
209pub struct TryTypeError {
210 pub expected: ValueType,
212 pub got: ValueType,
214}
215
216impl std::fmt::Display for TryTypeError {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 write!(f, "Expected type {}, got {}", self.expected, self.got)
219 }
220}
221
222impl std::error::Error for TryTypeError {}
223
224