1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use std::io::{self, Write};
use crate::{array::Array, object::Object, ValueType};
/// Type information on a value
pub trait TypedValue {
/// Gets the type of the current value
#[must_use]
fn value_type(&self) -> ValueType;
}
/// Type checks for custom values on a value
pub trait TypedCustomValue {
#[cfg(feature = "custom-types")]
/// returns if a type is a custom type
fn is_custom(&self) -> bool {
false
}
}
/// Access to scalar value types
pub trait ValueAsScalar {
/// Tries to represent the value as a 'null';
#[must_use]
fn as_null(&self) -> Option<()>;
/// Tries to represent the value as a bool
#[must_use]
fn as_bool(&self) -> Option<bool>;
/// Tries to represent the value as an i128
#[inline]
#[must_use]
fn as_i128(&self) -> Option<i128> {
self.as_i64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an i64
#[must_use]
fn as_i64(&self) -> Option<i64>;
/// Tries to represent the value as an i32
#[inline]
#[must_use]
fn as_i32(&self) -> Option<i32> {
self.as_i64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an i16
#[inline]
#[must_use]
fn as_i16(&self) -> Option<i16> {
self.as_i64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an i8
#[inline]
#[must_use]
fn as_i8(&self) -> Option<i8> {
self.as_i64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an u128
#[inline]
#[must_use]
fn as_u128(&self) -> Option<u128> {
self.as_u64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an u64
#[must_use]
fn as_u64(&self) -> Option<u64>;
/// Tries to represent the value as an usize
#[inline]
#[must_use]
fn as_usize(&self) -> Option<usize> {
self.as_u64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an u32
#[inline]
#[must_use]
fn as_u32(&self) -> Option<u32> {
self.as_u64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an u16
#[inline]
#[must_use]
fn as_u16(&self) -> Option<u16> {
self.as_u64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as an u8
#[inline]
#[must_use]
fn as_u8(&self) -> Option<u8> {
self.as_u64().and_then(|u| u.try_into().ok())
}
/// Tries to represent the value as a f64
#[must_use]
fn as_f64(&self) -> Option<f64>;
/// Tries to represent the value as a f32
#[inline]
#[must_use]
fn as_f32(&self) -> Option<f32> {
self.as_f64().and_then(|u| {
if u <= f64::from(std::f32::MAX) && u >= f64::from(std::f32::MIN) {
// Since we check above
#[allow(clippy::cast_possible_truncation)]
Some(u as f32)
} else {
None
}
})
}
/// Casts the current value to a f64 if possible, this will turn integer
/// values into floats.
#[must_use]
#[inline]
#[allow(clippy::cast_precision_loss, clippy::option_if_let_else)]
fn cast_f64(&self) -> Option<f64> {
if let Some(f) = self.as_f64() {
Some(f)
} else if let Some(u) = self.as_u128() {
Some(u as f64)
} else {
self.as_i128().map(|i| i as f64)
}
}
/// Tries to represent the value as a &str
#[must_use]
fn as_str(&self) -> Option<&str>;
/// Tries to represent the value as a Char
#[inline]
#[must_use]
fn as_char(&self) -> Option<char> {
self.as_str().and_then(|s| s.chars().next())
}
}
/// Trait to allow accessing data inside a Value
pub trait ValueAsContainer {
/// The array structure
type Array: Array;
/// The object structure
type Object: Object;
/// Tries to represent the value as an array and returns a reference to it
#[must_use]
fn as_array(&self) -> Option<&Self::Array>;
/// Tries to represent the value as an object and returns a reference to it
#[must_use]
fn as_object(&self) -> Option<&Self::Object>;
}
/// Mutatability for container values
pub trait ValueAsMutContainer {
/// The type for Arrays
type Array;
/// The type for Objects
type Object;
/// Tries to represent the value as an array and returns a mutable reference to it
fn as_array_mut(&mut self) -> Option<&mut Self::Array>;
/// Tries to represent the value as an object and returns a mutable reference to it
fn as_object_mut(&mut self) -> Option<&mut Self::Object>;
}
/// A trait that specifies how to turn the Value `into` it's sub types
pub trait ValueIntoString {
/// The type for Strings
type String;
/// Tries to turn the value into it's string representation
#[must_use]
fn into_string(self) -> Option<Self::String>;
}
/// A trait that specifies how to turn the Value `into` it's sub types
pub trait ValueIntoContainer {
/// The type for Arrays
type Array;
/// The type for Objects
type Object;
/// Tries to turn the value into it's array representation
#[must_use]
fn into_array(self) -> Option<Self::Array>;
/// Tries to turn the value into it's object representation
#[must_use]
fn into_object(self) -> Option<Self::Object>;
}
/// A Value that can be serialized and written
pub trait Writable {
/// Encodes the value into it's JSON representation as a string
#[must_use]
fn encode(&self) -> String;
/// Encodes the value into it's JSON representation as a string (pretty printed)
#[must_use]
fn encode_pp(&self) -> String;
/// Encodes the value into it's JSON representation into a Writer
///
/// # Errors
///
/// Will return `Err` if an IO error is encountered
fn write<'writer, W>(&self, w: &mut W) -> io::Result<()>
where
W: 'writer + Write;
/// Encodes the value into it's JSON representation into a Writer, pretty printed
///
/// # Errors
///
/// Will return `Err` if an IO error is encountered.
fn write_pp<'writer, W>(&self, w: &mut W) -> io::Result<()>
where
W: 'writer + Write;
}