From 5f1156341c28839ddb83c3b711e40dffd18eea30 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 18 Aug 2022 16:09:57 +0100 Subject: [PATCH] Store JavaScript classes in separate directory --- lib/src/fnc/script/classes.rs | 93 -------------------------- lib/src/fnc/script/classes/duration.rs | 29 ++++++++ lib/src/fnc/script/classes/mod.rs | 3 + lib/src/fnc/script/classes/record.rs | 36 ++++++++++ lib/src/fnc/script/classes/uuid.rs | 29 ++++++++ lib/src/fnc/script/from.rs | 18 ++--- lib/src/fnc/script/into.rs | 12 ++-- lib/src/fnc/script/main.rs | 6 +- 8 files changed, 115 insertions(+), 111 deletions(-) delete mode 100644 lib/src/fnc/script/classes.rs create mode 100644 lib/src/fnc/script/classes/duration.rs create mode 100644 lib/src/fnc/script/classes/mod.rs create mode 100644 lib/src/fnc/script/classes/record.rs create mode 100644 lib/src/fnc/script/classes/uuid.rs diff --git a/lib/src/fnc/script/classes.rs b/lib/src/fnc/script/classes.rs deleted file mode 100644 index b3d4ddec..00000000 --- a/lib/src/fnc/script/classes.rs +++ /dev/null @@ -1,93 +0,0 @@ -#[js::bind(object, public)] -#[quickjs(bare)] -pub mod duration { - - #[derive(Clone)] - #[quickjs(class)] - pub struct Duration { - #[quickjs(hide)] - pub(crate) value: String, - } - - impl Duration { - #[quickjs(constructor)] - pub fn new(value: String) -> Self { - Self { - value, - } - } - #[quickjs(rename = "toString")] - pub fn output(&self) -> String { - self.value.to_owned() - } - #[quickjs(get)] - pub fn value(&self) -> &str { - &self.value - } - } -} - -#[js::bind(object, public)] -#[quickjs(bare)] -pub mod uuid { - - #[derive(Clone)] - #[quickjs(class)] - pub struct Uuid { - #[quickjs(hide)] - pub(crate) value: String, - } - - impl Uuid { - #[quickjs(constructor)] - pub fn new(value: String) -> Self { - Self { - value, - } - } - #[quickjs(rename = "toString")] - pub fn output(&self) -> String { - self.value.to_owned() - } - #[quickjs(get)] - pub fn value(&self) -> &str { - &self.value - } - } -} - -#[js::bind(object, public)] -#[quickjs(bare)] -pub mod record { - - #[derive(Clone)] - #[quickjs(class)] - pub struct Record { - #[quickjs(hide)] - pub(crate) tb: String, - #[quickjs(hide)] - pub(crate) id: String, - } - - impl Record { - #[quickjs(constructor)] - pub fn new(tb: String, id: String) -> Self { - Self { - tb, - id, - } - } - #[quickjs(rename = "toString")] - pub fn output(&self) -> String { - format!("{}:{}", self.tb, self.id) - } - #[quickjs(get)] - pub fn tb(&self) -> &str { - &self.tb - } - #[quickjs(get)] - pub fn id(&self) -> &str { - &self.id - } - } -} diff --git a/lib/src/fnc/script/classes/duration.rs b/lib/src/fnc/script/classes/duration.rs new file mode 100644 index 00000000..8dd576a9 --- /dev/null +++ b/lib/src/fnc/script/classes/duration.rs @@ -0,0 +1,29 @@ +#[js::bind(object, public)] +#[quickjs(bare)] +#[allow(clippy::module_inception)] +pub mod duration { + + #[derive(Clone)] + #[quickjs(class)] + pub struct Duration { + #[quickjs(hide)] + pub(crate) value: String, + } + + impl Duration { + #[quickjs(constructor)] + pub fn new(value: String) -> Self { + Self { + value, + } + } + #[quickjs(rename = "toString")] + pub fn output(&self) -> String { + self.value.to_owned() + } + #[quickjs(get)] + pub fn value(&self) -> &str { + &self.value + } + } +} diff --git a/lib/src/fnc/script/classes/mod.rs b/lib/src/fnc/script/classes/mod.rs new file mode 100644 index 00000000..23ba11a6 --- /dev/null +++ b/lib/src/fnc/script/classes/mod.rs @@ -0,0 +1,3 @@ +pub mod duration; +pub mod record; +pub mod uuid; diff --git a/lib/src/fnc/script/classes/record.rs b/lib/src/fnc/script/classes/record.rs new file mode 100644 index 00000000..344198ec --- /dev/null +++ b/lib/src/fnc/script/classes/record.rs @@ -0,0 +1,36 @@ +#[js::bind(object, public)] +#[quickjs(bare)] +#[allow(clippy::module_inception)] +pub mod record { + + #[derive(Clone)] + #[quickjs(class)] + pub struct Record { + #[quickjs(hide)] + pub(crate) tb: String, + #[quickjs(hide)] + pub(crate) id: String, + } + + impl Record { + #[quickjs(constructor)] + pub fn new(tb: String, id: String) -> Self { + Self { + tb, + id, + } + } + #[quickjs(rename = "toString")] + pub fn output(&self) -> String { + format!("{}:{}", self.tb, self.id) + } + #[quickjs(get)] + pub fn tb(&self) -> &str { + &self.tb + } + #[quickjs(get)] + pub fn id(&self) -> &str { + &self.id + } + } +} diff --git a/lib/src/fnc/script/classes/uuid.rs b/lib/src/fnc/script/classes/uuid.rs new file mode 100644 index 00000000..ac949d2e --- /dev/null +++ b/lib/src/fnc/script/classes/uuid.rs @@ -0,0 +1,29 @@ +#[js::bind(object, public)] +#[quickjs(bare)] +#[allow(clippy::module_inception)] +pub mod uuid { + + #[derive(Clone)] + #[quickjs(class)] + pub struct Uuid { + #[quickjs(hide)] + pub(crate) value: String, + } + + impl Uuid { + #[quickjs(constructor)] + pub fn new(value: String) -> Self { + Self { + value, + } + } + #[quickjs(rename = "toString")] + pub fn output(&self) -> String { + self.value.to_owned() + } + #[quickjs(get)] + pub fn value(&self) -> &str { + &self.value + } + } +} diff --git a/lib/src/fnc/script/from.rs b/lib/src/fnc/script/from.rs index df06dbb5..073d5a7c 100644 --- a/lib/src/fnc/script/from.rs +++ b/lib/src/fnc/script/from.rs @@ -38,23 +38,23 @@ impl<'js> FromJs<'js> for Value { // Extract the value as an object let v = val.into_object().unwrap(); // Check to see if this object is a duration - if (&v).instance_of::() { - let v = v.into_instance::().unwrap(); - let v: &classes::duration::Duration = v.as_ref(); + if (&v).instance_of::() { + let v = v.into_instance::().unwrap(); + let v: &classes::duration::duration::Duration = v.as_ref(); let v = v.value.clone(); return Ok(Duration::from(v).into()); } // Check to see if this object is a record - if (&v).instance_of::() { - let v = v.into_instance::().unwrap(); - let v: &classes::record::Record = v.as_ref(); + if (&v).instance_of::() { + let v = v.into_instance::().unwrap(); + let v: &classes::record::record::Record = v.as_ref(); let v = (v.tb.clone(), v.id.clone()); return Ok(Thing::from(v).into()); } // Check to see if this object is a uuid - if (&v).instance_of::() { - let v = v.into_instance::().unwrap(); - let v: &classes::uuid::Uuid = v.as_ref(); + if (&v).instance_of::() { + let v = v.into_instance::().unwrap(); + let v: &classes::uuid::uuid::Uuid = v.as_ref(); let v = v.value.clone(); return Ok(Uuid::from(v).into()); } diff --git a/lib/src/fnc/script/into.rs b/lib/src/fnc/script/into.rs index db409ed3..5521ea7a 100644 --- a/lib/src/fnc/script/into.rs +++ b/lib/src/fnc/script/into.rs @@ -35,24 +35,24 @@ impl<'js> IntoJs<'js> for &Value { let date: js::Function = ctx.globals().get("Date")?; date.construct((v.0.timestamp_millis(),)) } - Value::Duration(v) => Ok(Class::::instance( + Value::Duration(v) => Ok(Class::::instance( ctx, - classes::duration::Duration { + classes::duration::duration::Duration { value: v.to_raw(), }, )? .into_value()), - Value::Thing(v) => Ok(Class::::instance( + Value::Thing(v) => Ok(Class::::instance( ctx, - classes::record::Record { + classes::record::record::Record { tb: v.tb.to_owned(), id: v.id.to_raw(), }, )? .into_value()), - Value::Uuid(v) => Ok(Class::::instance( + Value::Uuid(v) => Ok(Class::::instance( ctx, - classes::uuid::Uuid { + classes::uuid::uuid::Uuid { value: v.to_raw(), }, )? diff --git a/lib/src/fnc/script/main.rs b/lib/src/fnc/script/main.rs index 72299135..ec17ac4d 100644 --- a/lib/src/fnc/script/main.rs +++ b/lib/src/fnc/script/main.rs @@ -40,11 +40,11 @@ pub async fn run( // Register the fetch function as a global object global.init_def::()?; // Register the Duration type as a global class - global.init_def::()?; + global.init_def::()?; // Register the Record type as a global class - global.init_def::()?; + global.init_def::()?; // Register the Uuid type as a global class - global.init_def::()?; + global.init_def::()?; // Attempt to compile the script let res = ctx.compile("script", src)?; // Attempt to fetch the main export