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)]
77pub enum ExtendedValueType {
78 I32,
80 I16,
82 I8,
84 U32,
86 U16,
88 U8,
90 Usize,
92 F32,
94 Char,
96 None,
98}
99
100impl Default for ExtendedValueType {
101 fn default() -> Self {
102 Self::None
103 }
104}
105
106impl fmt::Display for ExtendedValueType {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 match self {
109 Self::I32 => write!(f, "i32"),
110 Self::I16 => write!(f, "i16"),
111 Self::I8 => write!(f, "i8"),
112 Self::U32 => write!(f, "u32"),
113 Self::U16 => write!(f, "u16"),
114 Self::U8 => write!(f, "u8"),
115 Self::Usize => write!(f, "usize"),
116 Self::F32 => write!(f, "f32"),
117 Self::Char => write!(f, "char"),
118 Self::None => write!(f, "none"),
119 }
120 }
121}
122
123#[derive(Copy, Clone, Debug, PartialEq, Eq)]
125pub enum ValueType {
126 Null,
128 Bool,
130 I64,
132 I128,
134 U64,
136 U128,
138 F64,
140 String,
142 Array,
144 Object,
146 Extended(ExtendedValueType),
148 #[cfg(feature = "custom-types")]
149 Custom(&'static str),
151}
152impl fmt::Display for ValueType {
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 match self {
155 Self::Null => write!(f, "null"),
156 Self::Bool => write!(f, "bool"),
157 Self::I64 => write!(f, "i64"),
158 Self::I128 => write!(f, "i128"),
159 Self::U64 => write!(f, "u64"),
160 Self::U128 => write!(f, "u128"),
161 Self::F64 => write!(f, "f64"),
162 Self::String => write!(f, "string"),
163 Self::Array => write!(f, "array"),
164 Self::Object => write!(f, "object"),
165 Self::Extended(ty) => write!(f, "{ty}"),
166 #[cfg(feature = "custom-types")]
167 Self::Custom(name) => write!(f, "{name}"),
168 }
169 }
170}
171
172impl Default for ValueType {
173 fn default() -> Self {
174 Self::Null
175 }
176}
177
178#[allow(clippy::trait_duplication_in_bounds)] pub trait ValueBuilder<'input>:
181 Default
182 + From<StaticNode>
183 + From<i8>
184 + From<i16>
185 + From<i32>
186 + From<i64>
187 + From<u8>
188 + From<u16>
189 + From<u32>
190 + From<u64>
191 + From<f32>
192 + From<f64>
193 + From<bool>
194 + From<()>
195 + From<String>
196 + From<&'input str>
197 + From<Cow<'input, str>>
198{
199 fn array_with_capacity(capacity: usize) -> Self;
201 fn object_with_capacity(capacity: usize) -> Self;
203 #[must_use]
205 fn array() -> Self {
206 Self::array_with_capacity(0)
207 }
208 #[must_use]
210 fn object() -> Self {
211 Self::object_with_capacity(0)
212 }
213 fn null() -> Self;
215}
216
217#[derive(Clone, Debug, PartialEq, Eq)]
219pub struct TryTypeError {
220 pub expected: ValueType,
222 pub got: ValueType,
224}
225
226impl std::fmt::Display for TryTypeError {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228 write!(f, "Expected type {}, got {}", self.expected, self.got)
229 }
230}
231
232impl std::error::Error for TryTypeError {}
233
234