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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
/*
* AquaVM Workflow Engine
*
* Copyright (C) 2024 Fluence DAO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* This file is based on serde_json crate licensed under conditions
* of MIT License.
* Copyright (C) 2023 by Erick Tryzelaar and David Tolnay
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
use super::JValue;
use crate::{JsonString, Map};
use serde_json::Number;
use std::borrow::Cow;
use std::collections::HashMap;
use std::rc::Rc;
use std::string::String;
use std::vec::Vec;
macro_rules! from_integer {
($($ty:ident)*) => {
$(
impl From<$ty> for JValue {
fn from(n: $ty) -> Self {
JValue::Number(n.into())
}
}
)*
};
}
from_integer! {
i8 i16 i32 i64 isize
u8 u16 u32 u64 usize
}
impl From<f32> for JValue {
/// Convert 32-bit floating point number to `JValue::Number`, or
/// `JValue::Null` if infinite or NaN.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let f: f32 = 13.37;
/// let x: JValue = f.into();
/// ```
fn from(f: f32) -> Self {
Number::from_f64(f as _).map_or(JValue::Null, JValue::Number)
}
}
impl From<f64> for JValue {
/// Convert 64-bit floating point number to `JValue::Number`, or
/// `JValue::Null` if infinite or NaN.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let f: f64 = 13.37;
/// let x: JValue = f.into();
/// ```
fn from(f: f64) -> Self {
Number::from_f64(f).map_or(JValue::Null, JValue::Number)
}
}
impl From<bool> for JValue {
/// Convert boolean to `JValue::Bool`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let b = false;
/// let x: JValue = b.into();
/// ```
fn from(f: bool) -> Self {
JValue::Bool(f)
}
}
impl From<String> for JValue {
/// Convert `String` to `JValue::String`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let s: String = "lorem".to_string();
/// let x: JValue = s.into();
/// ```
fn from(f: String) -> Self {
JValue::String(f.into())
}
}
impl From<JsonString> for JValue {
/// Convert `JsonString` to `JValue::String`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let s: String = "lorem".to_string();
/// let x: JValue = s.into();
/// ```
fn from(f: JsonString) -> Self {
JValue::String(f)
}
}
impl From<&str> for JValue {
/// Convert string slice to `JValue::String`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let s: &str = "lorem";
/// let x: JValue = s.into();
/// ```
fn from(f: &str) -> Self {
JValue::String(f.into())
}
}
impl<'a> From<Cow<'a, str>> for JValue {
/// Convert copy-on-write string to `JValue::String`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
/// use std::borrow::Cow;
///
/// let s: Cow<str> = Cow::Borrowed("lorem");
/// let x: JValue = s.into();
/// ```
///
/// ```
/// use air_interpreter_value::JValue;
/// use std::borrow::Cow;
///
/// let s: Cow<str> = Cow::Owned("lorem".to_string());
/// let x: JValue = s.into();
/// ```
fn from(f: Cow<'a, str>) -> Self {
JValue::String(f.into())
}
}
impl From<Number> for JValue {
/// Convert `serde_json::Number` to `JValue::Number`.
///
/// # Examples
///
/// ```
/// use serde_json::Number;
/// use air_interpreter_value::JValue;
///
/// let n = Number::from(7);
/// let x: JValue = n.into();
/// ```
fn from(f: Number) -> Self {
JValue::Number(f)
}
}
impl From<Map<JsonString, JValue>> for JValue {
/// Convert map (with string keys) to `JValue::Object`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::{Map, JValue, JsonString};
///
/// let mut m = Map::<JsonString, JValue>::new();
/// m.insert("Lorem".into(), "ipsum".into());
/// let x: JValue = m.into();
/// ```
fn from(f: Map<JsonString, JValue>) -> Self {
JValue::Object(f.into())
}
}
impl<K: Into<JsonString>, V: Into<JValue>> From<HashMap<K, V>> for JValue {
/// Convert map (with string keys) to `JValue::Object`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
/// use std::collections::HashMap;
///
/// let mut m = HashMap::<&str, &str>::new();
/// m.insert("Lorem", "ipsum");
/// let x: JValue = m.into();
/// ```
fn from(f: HashMap<K, V>) -> Self {
JValue::object_from_pairs(f)
}
}
impl<T: Into<JValue>> From<Vec<T>> for JValue {
/// Convert a `Vec` to `JValue::Array`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let v = vec!["lorem", "ipsum", "dolor"];
/// let x: JValue = v.into();
/// ```
fn from(f: Vec<T>) -> Self {
JValue::Array(f.into_iter().map(Into::into).collect())
}
}
impl<T: Clone + Into<JValue>> From<&[T]> for JValue {
/// Convert a slice to `JValue::Array`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let v: &[&str] = &["lorem", "ipsum", "dolor"];
/// let x: JValue = v.into();
/// ```
fn from(f: &[T]) -> Self {
JValue::Array(f.iter().cloned().map(Into::into).collect())
}
}
impl<T: Into<JValue>> FromIterator<T> for JValue {
/// Create a `JValue::Array` by collecting an iterator of array elements.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let v = std::iter::repeat(42).take(5);
/// let x: JValue = v.collect();
/// ```
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
/// let x: JValue = v.into_iter().collect();
/// ```
///
/// ```
/// use std::iter::FromIterator;
/// use air_interpreter_value::JValue;
///
/// let x: JValue = JValue::from_iter(vec!["lorem", "ipsum", "dolor"]);
/// ```
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
JValue::Array(iter.into_iter().map(Into::into).collect())
}
}
impl<K: Into<JsonString>, V: Into<JValue>> FromIterator<(K, V)> for JValue {
/// Create a `JValue::Object` by collecting an iterator of key-value pairs.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)];
/// let x: JValue = v.into_iter().collect();
/// ```
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
JValue::Object(Rc::new(
iter.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
))
}
}
impl From<()> for JValue {
/// Convert `()` to `JValue::Null`.
///
/// # Examples
///
/// ```
/// use air_interpreter_value::JValue;
///
/// let u = ();
/// let x: JValue = u.into();
/// ```
#[inline]
fn from((): ()) -> Self {
JValue::Null
}
}
impl<T> From<Option<T>> for JValue
where
T: Into<JValue>,
{
fn from(opt: Option<T>) -> Self {
match opt {
None => JValue::Null,
Some(value) => Into::into(value),
}
}
}
impl From<&serde_json::Value> for JValue {
fn from(value: &serde_json::Value) -> Self {
use serde_json::Value;
match value {
Value::Null => JValue::Null,
Value::Bool(b) => JValue::Bool(*b),
Value::Number(n) => JValue::Number(n.clone()),
Value::String(s) => JValue::String(s.as_str().into()),
Value::Array(a) => JValue::Array(a.iter().map(Into::into).collect()),
Value::Object(o) => {
let oo = Map::from_iter(o.into_iter().map(|(k, v)| (k.as_str().into(), v.into())));
JValue::Object(oo.into())
}
}
}
}
impl From<serde_json::Value> for JValue {
#[inline]
fn from(value: serde_json::Value) -> Self {
Self::from(&value)
}
}