surrealdb_core/fnc/script/
into.rs

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
use super::classes;
use crate::sql::geometry::Geometry;
use crate::sql::number::Number;
use crate::sql::value::Value;
use js::Array;
use js::BigInt;
use js::Class;
use js::Ctx;
use js::Error;
use js::Exception;
use js::FromIteratorJs as _;
use js::IntoJs;
use js::Null;
use js::Object;
use js::TypedArray;
use js::Undefined;
use rust_decimal::prelude::ToPrimitive;

const F64_INT_MAX: i64 = ((1u64 << f64::MANTISSA_DIGITS) - 1) as i64;
const F64_INT_MIN: i64 = -F64_INT_MAX - 1;

impl<'js> IntoJs<'js> for Value {
	fn into_js(self, ctx: &Ctx<'js>) -> Result<js::Value<'js>, Error> {
		(&self).into_js(ctx)
	}
}

impl<'js> IntoJs<'js> for &Value {
	fn into_js(self, ctx: &Ctx<'js>) -> Result<js::Value<'js>, Error> {
		match *self {
			Value::Null => Null.into_js(ctx),
			Value::None => Undefined.into_js(ctx),
			Value::Bool(boolean) => Ok(js::Value::new_bool(ctx.clone(), boolean)),
			Value::Strand(ref v) => js::String::from_str(ctx.clone(), v)?.into_js(ctx),
			Value::Number(Number::Int(v)) => {
				if ((i32::MIN as i64)..=(i32::MAX as i64)).contains(&v) {
					Ok(js::Value::new_int(ctx.clone(), v as i32))
				} else if (F64_INT_MIN..=F64_INT_MAX).contains(&v) {
					Ok(js::Value::new_float(ctx.clone(), v as f64))
				} else {
					Ok(js::Value::from(BigInt::from_i64(ctx.clone(), v)?))
				}
			}
			Value::Number(Number::Float(v)) => Ok(js::Value::new_float(ctx.clone(), v)),
			Value::Number(Number::Decimal(v)) => {
				if v.is_integer() {
					if let Some(v) = v.to_i64() {
						if ((i32::MIN as i64)..=(i32::MAX as i64)).contains(&v) {
							Ok(js::Value::new_int(ctx.clone(), v as i32))
						} else if (F64_INT_MIN..=F64_INT_MAX).contains(&v) {
							Ok(js::Value::new_float(ctx.clone(), v as f64))
						} else {
							Ok(js::Value::from(BigInt::from_i64(ctx.clone(), v)?))
						}
					} else {
						Err(Exception::from_message(
							ctx.clone(),
							"Couldn't convert SurrealQL Decimal to JavaScript number",
						)?
						.throw())
					}
				} else if let Ok(v) = v.try_into() {
					Ok(js::Value::new_float(ctx.clone(), v))
				} else {
					// FIXME: Add support for larger numbers if rquickjs ever adds support.
					Err(Exception::from_message(
						ctx.clone(),
						"Couldn't convert SurrealQL Decimal to a JavaScript number",
					)?
					.throw())
				}
			}
			Value::Datetime(ref v) => {
				let date: js::function::Constructor = ctx.globals().get("Date")?;
				date.construct((v.0.timestamp_millis(),))
			}
			Value::Thing(ref v) => Ok(Class::<classes::record::Record>::instance(
				ctx.clone(),
				classes::record::Record {
					value: v.to_owned(),
				},
			)?
			.into_value()),
			Value::Duration(ref v) => Ok(Class::<classes::duration::Duration>::instance(
				ctx.clone(),
				classes::duration::Duration {
					value: Some(v.to_owned()),
				},
			)?
			.into_value()),
			Value::Uuid(ref v) => Ok(Class::<classes::uuid::Uuid>::instance(
				ctx.clone(),
				classes::uuid::Uuid {
					value: Some(v.to_owned()),
				},
			)?
			.into_value()),
			Value::Array(ref v) => {
				let x = Array::new(ctx.clone())?;
				for (i, v) in v.iter().enumerate() {
					x.set(i, v)?;
				}
				x.into_js(ctx)
			}
			Value::Object(ref v) => {
				let x = Object::new(ctx.clone())?;
				for (k, v) in v.iter() {
					x.set(k, v)?;
				}
				x.into_js(ctx)
			}
			Value::Bytes(ref v) => TypedArray::new_copy(ctx.clone(), v.0.as_slice())?.into_js(ctx),
			Value::Geometry(ref v) => v.into_js(ctx),
			_ => Undefined.into_js(ctx),
		}
	}
}

impl<'js> IntoJs<'js> for &Geometry {
	fn into_js(self, ctx: &Ctx<'js>) -> js::Result<js::Value<'js>> {
		let (ty, coords) = match self {
			Geometry::Point(x) => {
				("Point".into_js(ctx)?, Array::from_iter_js(ctx, [x.0.x, x.0.y])?)
			}
			Geometry::Line(x) => {
				let array = Array::new(ctx.clone())?;
				for (idx, c) in x.0.iter().enumerate() {
					let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
					array.set(idx, coord)?;
				}
				("LineString".into_js(ctx)?, array)
			}
			Geometry::Polygon(x) => {
				let coords = Array::new(ctx.clone())?;

				let string = Array::new(ctx.clone())?;
				for (idx, c) in x.exterior().0.iter().enumerate() {
					let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
					string.set(idx, coord)?;
				}

				coords.set(0, string)?;

				for (idx, int) in x.interiors().iter().enumerate() {
					let string = Array::new(ctx.clone())?;
					for (idx, c) in int.0.iter().enumerate() {
						let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
						string.set(idx, coord)?;
					}
					coords.set(idx + 1, string)?;
				}

				("Polygon".into_js(ctx)?, coords)
			}
			Geometry::MultiPoint(x) => {
				let array = Array::new(ctx.clone())?;
				for (idx, c) in x.0.iter().enumerate() {
					let coord = Array::from_iter_js(ctx, [c.x(), c.y()])?;
					array.set(idx, coord)?;
				}
				("MultiPoint".into_js(ctx)?, array)
			}
			Geometry::MultiLine(x) => {
				let lines = Array::new(ctx.clone())?;
				for (idx, l) in x.0.iter().enumerate() {
					let array = Array::new(ctx.clone())?;
					for (idx, c) in l.0.iter().enumerate() {
						let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
						array.set(idx, coord)?;
					}
					lines.set(idx, array)?
				}
				("MultiLineString".into_js(ctx)?, lines)
			}
			Geometry::MultiPolygon(x) => {
				let polygons = Array::new(ctx.clone())?;

				for (idx, p) in x.0.iter().enumerate() {
					let coords = Array::new(ctx.clone())?;

					let string = Array::new(ctx.clone())?;
					for (idx, c) in p.exterior().0.iter().enumerate() {
						let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
						string.set(idx, coord)?;
					}

					coords.set(0, string)?;

					for (idx, int) in p.interiors().iter().enumerate() {
						let string = Array::new(ctx.clone())?;
						for (idx, c) in int.0.iter().enumerate() {
							let coord = Array::from_iter_js(ctx, [c.x, c.y])?;
							string.set(idx, coord)?;
						}
						coords.set(idx + 1, string)?;
					}

					polygons.set(idx, coords)?;
				}
				("MultiPolygon".into_js(ctx)?, polygons)
			}
			Geometry::Collection(x) => {
				let geoms = Array::new(ctx.clone())?;

				for (idx, g) in x.iter().enumerate() {
					let g = g.into_js(ctx)?;
					geoms.set(idx, g)?;
				}

				let object = Object::new(ctx.clone())?;
				object.set("type", "GeometryCollection")?;
				object.set("geometries", geoms)?;
				return Ok(object.into_value());
			}
		};
		let object = Object::new(ctx.clone())?;
		object.set("type", ty)?;
		object.set("coordinates", coords)?;
		Ok(object.into_value())
	}
}