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
use super::*; use std::convert::TryFrom; impl From<&Value> for Value { #[inline] fn from(value: &Value) -> Self { value.clone() } } impl From<String> for Value { #[inline] fn from(value: String) -> Self { let _timer = BoaProfiler::global().start_event("From<String>", "value"); Self::string(value) } } impl From<Box<str>> for Value { #[inline] fn from(value: Box<str>) -> Self { Self::string(String::from(value)) } } impl From<&str> for Value { #[inline] fn from(value: &str) -> Value { Value::string(value) } } impl From<&Box<str>> for Value { #[inline] fn from(value: &Box<str>) -> Self { Self::string(value.as_ref()) } } impl From<char> for Value { #[inline] fn from(value: char) -> Self { Value::string(value.to_string()) } } impl From<RcString> for Value { #[inline] fn from(value: RcString) -> Self { Value::String(value) } } impl From<RcSymbol> for Value { #[inline] fn from(value: RcSymbol) -> Self { Value::Symbol(value) } } #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub struct TryFromCharError; impl Display for TryFromCharError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Could not convert value to a char type") } } impl From<f64> for Value { fn from(value: f64) -> Self { Self::rational(value) } } impl From<u32> for Value { #[inline] fn from(value: u32) -> Value { if let Ok(integer) = i32::try_from(value) { Value::integer(integer) } else { Value::rational(value) } } } impl From<i32> for Value { fn from(value: i32) -> Value { Value::integer(value) } } impl From<BigInt> for Value { fn from(value: BigInt) -> Self { Value::bigint(value) } } impl From<RcBigInt> for Value { fn from(value: RcBigInt) -> Self { Value::BigInt(value) } } impl From<usize> for Value { fn from(value: usize) -> Value { Value::integer(value as i32) } } impl From<bool> for Value { fn from(value: bool) -> Self { Value::boolean(value) } } impl From<&Value> for bool { fn from(value: &Value) -> Self { value.to_boolean() } } impl<T> From<&[T]> for Value where T: Clone + Into<Value>, { fn from(value: &[T]) -> Self { let mut array = Object::default(); for (i, item) in value.iter().enumerate() { array.insert_property(i, Property::default().value(item.clone().into())); } Self::from(array) } } impl<T> From<Vec<T>> for Value where T: Into<Value>, { fn from(value: Vec<T>) -> Self { let mut array = Object::default(); for (i, item) in value.into_iter().enumerate() { array.insert_property(i, Property::default().value(item.into())); } Value::from(array) } } impl From<Object> for Value { fn from(object: Object) -> Self { let _timer = BoaProfiler::global().start_event("From<Object>", "value"); Value::object(object) } } impl From<GcObject> for Value { fn from(object: GcObject) -> Self { let _timer = BoaProfiler::global().start_event("From<GcObject>", "value"); Value::Object(object) } } #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub struct TryFromObjectError; impl Display for TryFromObjectError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Could not convert value to an Object type") } } impl From<()> for Value { fn from(_: ()) -> Self { Value::null() } } impl<T> From<Option<T>> for Value where T: Into<Value>, { fn from(value: Option<T>) -> Self { match value { Some(value) => value.into(), None => Value::null(), } } }