surrealdb_core/fnc/script/classes/
uuid.rs

1use crate::sql::uuid;
2use js::{class::Trace, JsLifetime};
3
4#[derive(Clone, Trace, JsLifetime)]
5#[js::class]
6#[non_exhaustive]
7pub struct Uuid {
8	#[qjs(skip_trace)]
9	pub(crate) value: Option<uuid::Uuid>,
10}
11
12#[js::methods]
13impl Uuid {
14	#[qjs(constructor)]
15	pub fn new(value: String) -> Self {
16		Self {
17			value: uuid::Uuid::try_from(value).ok(),
18		}
19	}
20	#[qjs(get)]
21	pub fn value(&self) -> String {
22		match &self.value {
23			Some(v) => v.to_raw(),
24			None => String::from("Invalid Uuid"),
25		}
26	}
27	// Compare two Uuid instances
28	pub fn is(a: &Uuid, b: &Uuid) -> bool {
29		a.value.is_some() && b.value.is_some() && a.value == b.value
30	}
31	/// Convert the object to a string
32	#[qjs(rename = "toString")]
33	pub fn js_to_string(&self) -> String {
34		match &self.value {
35			Some(v) => v.to_raw(),
36			None => String::from("Invalid Uuid"),
37		}
38	}
39	/// Convert the object to JSON
40	#[qjs(rename = "toJSON")]
41	pub fn to_json(&self) -> String {
42		match &self.value {
43			Some(v) => v.to_raw(),
44			None => String::from("Invalid Uuid"),
45		}
46	}
47}