From 612cd39b0588b0352f1955458654630de0b306e0 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 12 Feb 2023 15:49:53 +0000 Subject: [PATCH] Add `toJSON()` method to custom JavaScript classes --- lib/src/fnc/script/classes/duration.rs | 8 ++++++++ lib/src/fnc/script/classes/record.rs | 8 ++++++++ lib/src/fnc/script/classes/uuid.rs | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/src/fnc/script/classes/duration.rs b/lib/src/fnc/script/classes/duration.rs index 29818871..bb383a3a 100644 --- a/lib/src/fnc/script/classes/duration.rs +++ b/lib/src/fnc/script/classes/duration.rs @@ -1,9 +1,13 @@ #[js::bind(object, public)] #[quickjs(bare)] #[allow(non_snake_case)] +#[allow(unused_variables)] #[allow(clippy::module_inception)] pub mod duration { + use crate::sql::value::Value; + use js::Rest; + #[derive(Clone)] #[quickjs(class)] pub struct Duration { @@ -26,5 +30,9 @@ pub mod duration { pub fn toString(&self) -> String { self.value.to_owned() } + /// Convert the object to JSON + pub fn toJSON(&self, args: Rest) -> String { + self.value.to_owned() + } } } diff --git a/lib/src/fnc/script/classes/record.rs b/lib/src/fnc/script/classes/record.rs index 7967d43e..76756011 100644 --- a/lib/src/fnc/script/classes/record.rs +++ b/lib/src/fnc/script/classes/record.rs @@ -1,9 +1,13 @@ #[js::bind(object, public)] #[quickjs(bare)] #[allow(non_snake_case)] +#[allow(unused_variables)] #[allow(clippy::module_inception)] pub mod record { + use crate::sql::value::Value; + use js::Rest; + #[derive(Clone)] #[quickjs(class)] pub struct Record { @@ -33,5 +37,9 @@ pub mod record { pub fn toString(&self) -> String { format!("{}:{}", self.tb, self.id) } + /// Convert the object to JSON + pub fn toJSON(&self, args: Rest) -> String { + format!("{}:{}", self.tb, self.id) + } } } diff --git a/lib/src/fnc/script/classes/uuid.rs b/lib/src/fnc/script/classes/uuid.rs index 0969f5c3..06c75c37 100644 --- a/lib/src/fnc/script/classes/uuid.rs +++ b/lib/src/fnc/script/classes/uuid.rs @@ -1,9 +1,13 @@ #[js::bind(object, public)] #[quickjs(bare)] #[allow(non_snake_case)] +#[allow(unused_variables)] #[allow(clippy::module_inception)] pub mod uuid { + use crate::sql::value::Value; + use js::Rest; + #[derive(Clone)] #[quickjs(class)] pub struct Uuid { @@ -26,5 +30,9 @@ pub mod uuid { pub fn toString(&self) -> String { self.value.to_owned() } + /// Convert the object to JSON + pub fn toJSON(&self, args: Rest) -> String { + self.value.to_owned() + } } }