summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_yaml/src/value/from.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/serde_yaml/src/value/from.rs')
-rw-r--r--third_party/rust/serde_yaml/src/value/from.rs180
1 files changed, 180 insertions, 0 deletions
diff --git a/third_party/rust/serde_yaml/src/value/from.rs b/third_party/rust/serde_yaml/src/value/from.rs
new file mode 100644
index 0000000000..c02855d3b1
--- /dev/null
+++ b/third_party/rust/serde_yaml/src/value/from.rs
@@ -0,0 +1,180 @@
+use crate::{Mapping, Value};
+
+// Implement a bunch of conversion to make it easier to create YAML values
+// on the fly.
+
+macro_rules! from_number {
+ ($($ty:ident)*) => {
+ $(
+ impl From<$ty> for Value {
+ fn from(n: $ty) -> Self {
+ Value::Number(n.into())
+ }
+ }
+ )*
+ };
+}
+
+from_number! {
+ i8 i16 i32 i64 isize
+ u8 u16 u32 u64 usize
+ f32 f64
+}
+
+impl From<bool> for Value {
+ /// Convert boolean to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let b = false;
+ /// let x: Value = b.into();
+ /// ```
+ fn from(f: bool) -> Self {
+ Value::Bool(f)
+ }
+}
+
+impl From<String> for Value {
+ /// Convert `String` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let s: String = "lorem".to_string();
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: String) -> Self {
+ Value::String(f)
+ }
+}
+
+impl<'a> From<&'a str> for Value {
+ /// Convert string slice to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let s: &str = "lorem";
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: &str) -> Self {
+ Value::String(f.to_string())
+ }
+}
+
+use std::borrow::Cow;
+
+impl<'a> From<Cow<'a, str>> for Value {
+ /// Convert copy-on-write string to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ /// use std::borrow::Cow;
+ ///
+ /// let s: Cow<str> = Cow::Borrowed("lorem");
+ /// let x: Value = s.into();
+ /// ```
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ /// use std::borrow::Cow;
+ ///
+ /// let s: Cow<str> = Cow::Owned("lorem".to_string());
+ /// let x: Value = s.into();
+ /// ```
+ fn from(f: Cow<'a, str>) -> Self {
+ Value::String(f.to_string())
+ }
+}
+
+impl From<Mapping> for Value {
+ /// Convert map (with string keys) to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::{Mapping, Value};
+ ///
+ /// let mut m = Mapping::new();
+ /// m.insert("Lorem".into(), "ipsum".into());
+ /// let x: Value = m.into();
+ /// ```
+ fn from(f: Mapping) -> Self {
+ Value::Mapping(f)
+ }
+}
+
+impl<T: Into<Value>> From<Vec<T>> for Value {
+ /// Convert a `Vec` to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let v = vec!["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into();
+ /// ```
+ fn from(f: Vec<T>) -> Self {
+ Value::Sequence(f.into_iter().map(Into::into).collect())
+ }
+}
+
+impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
+ /// Convert a slice to `Value`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into();
+ /// ```
+ fn from(f: &'a [T]) -> Self {
+ Value::Sequence(f.iter().cloned().map(Into::into).collect())
+ }
+}
+
+use std::iter::FromIterator;
+
+impl<T: Into<Value>> FromIterator<T> for Value {
+ /// Convert an iteratable type to a YAML sequence
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let v = std::iter::repeat(42).take(5);
+ /// let x: Value = v.collect();
+ /// ```
+ ///
+ /// ```
+ /// use serde_yaml::Value;
+ ///
+ /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
+ /// let x: Value = v.into_iter().collect();
+ /// ```
+ ///
+ /// ```
+ /// use std::iter::FromIterator;
+ /// use serde_yaml::Value;
+ ///
+ /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
+ /// ```
+ fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
+ let vec = iter.into_iter().map(T::into).collect();
+
+ Value::Sequence(vec)
+ }
+}