From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/serde_json/src/value/from.rs | 278 ++++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 vendor/serde_json/src/value/from.rs (limited to 'vendor/serde_json/src/value/from.rs') diff --git a/vendor/serde_json/src/value/from.rs b/vendor/serde_json/src/value/from.rs new file mode 100644 index 0000000..ed1e333 --- /dev/null +++ b/vendor/serde_json/src/value/from.rs @@ -0,0 +1,278 @@ +use super::Value; +use crate::map::Map; +use crate::number::Number; +use alloc::borrow::Cow; +use alloc::string::{String, ToString}; +use alloc::vec::Vec; + +macro_rules! from_integer { + ($($ty:ident)*) => { + $( + impl From<$ty> for Value { + fn from(n: $ty) -> Self { + Value::Number(n.into()) + } + } + )* + }; +} + +from_integer! { + i8 i16 i32 i64 isize + u8 u16 u32 u64 usize +} + +#[cfg(feature = "arbitrary_precision")] +from_integer! { + i128 u128 +} + +impl From for Value { + /// Convert 32-bit floating point number to `Value::Number`, or + /// `Value::Null` if infinite or NaN. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let f: f32 = 13.37; + /// let x: Value = f.into(); + /// ``` + fn from(f: f32) -> Self { + Number::from_f32(f).map_or(Value::Null, Value::Number) + } +} + +impl From for Value { + /// Convert 64-bit floating point number to `Value::Number`, or + /// `Value::Null` if infinite or NaN. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let f: f64 = 13.37; + /// let x: Value = f.into(); + /// ``` + fn from(f: f64) -> Self { + Number::from_f64(f).map_or(Value::Null, Value::Number) + } +} + +impl From for Value { + /// Convert boolean to `Value::Bool`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let b = false; + /// let x: Value = b.into(); + /// ``` + fn from(f: bool) -> Self { + Value::Bool(f) + } +} + +impl From for Value { + /// Convert `String` to `Value::String`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let s: String = "lorem".to_string(); + /// let x: Value = s.into(); + /// ``` + fn from(f: String) -> Self { + Value::String(f) + } +} + +impl From<&str> for Value { + /// Convert string slice to `Value::String`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let s: &str = "lorem"; + /// let x: Value = s.into(); + /// ``` + fn from(f: &str) -> Self { + Value::String(f.to_string()) + } +} + +impl<'a> From> for Value { + /// Convert copy-on-write string to `Value::String`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// use std::borrow::Cow; + /// + /// let s: Cow = Cow::Borrowed("lorem"); + /// let x: Value = s.into(); + /// ``` + /// + /// ``` + /// use serde_json::Value; + /// use std::borrow::Cow; + /// + /// let s: Cow = Cow::Owned("lorem".to_string()); + /// let x: Value = s.into(); + /// ``` + fn from(f: Cow<'a, str>) -> Self { + Value::String(f.into_owned()) + } +} + +impl From for Value { + /// Convert `Number` to `Value::Number`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::{Number, Value}; + /// + /// let n = Number::from(7); + /// let x: Value = n.into(); + /// ``` + fn from(f: Number) -> Self { + Value::Number(f) + } +} + +impl From> for Value { + /// Convert map (with string keys) to `Value::Object`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::{Map, Value}; + /// + /// let mut m = Map::new(); + /// m.insert("Lorem".to_string(), "ipsum".into()); + /// let x: Value = m.into(); + /// ``` + fn from(f: Map) -> Self { + Value::Object(f) + } +} + +impl> From> for Value { + /// Convert a `Vec` to `Value::Array`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let v = vec!["lorem", "ipsum", "dolor"]; + /// let x: Value = v.into(); + /// ``` + fn from(f: Vec) -> Self { + Value::Array(f.into_iter().map(Into::into).collect()) + } +} + +impl> From<&[T]> for Value { + /// Convert a slice to `Value::Array`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let v: &[&str] = &["lorem", "ipsum", "dolor"]; + /// let x: Value = v.into(); + /// ``` + fn from(f: &[T]) -> Self { + Value::Array(f.iter().cloned().map(Into::into).collect()) + } +} + +impl> FromIterator for Value { + /// Create a `Value::Array` by collecting an iterator of array elements. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let v = std::iter::repeat(42).take(5); + /// let x: Value = v.collect(); + /// ``` + /// + /// ``` + /// use serde_json::Value; + /// + /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"]; + /// let x: Value = v.into_iter().collect(); + /// ``` + /// + /// ``` + /// use std::iter::FromIterator; + /// use serde_json::Value; + /// + /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]); + /// ``` + fn from_iter>(iter: I) -> Self { + Value::Array(iter.into_iter().map(Into::into).collect()) + } +} + +impl, V: Into> FromIterator<(K, V)> for Value { + /// Create a `Value::Object` by collecting an iterator of key-value pairs. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)]; + /// let x: Value = v.into_iter().collect(); + /// ``` + fn from_iter>(iter: I) -> Self { + Value::Object( + iter.into_iter() + .map(|(k, v)| (k.into(), v.into())) + .collect(), + ) + } +} + +impl From<()> for Value { + /// Convert `()` to `Value::Null`. + /// + /// # Examples + /// + /// ``` + /// use serde_json::Value; + /// + /// let u = (); + /// let x: Value = u.into(); + /// ``` + fn from((): ()) -> Self { + Value::Null + } +} + +impl From> for Value +where + T: Into, +{ + fn from(opt: Option) -> Self { + match opt { + None => Value::Null, + Some(value) => Into::into(value), + } + } +} -- cgit v1.2.3