Store JavaScript classes in separate directory
This commit is contained in:
parent
292d35280c
commit
5f1156341c
8 changed files with 115 additions and 111 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
29
lib/src/fnc/script/classes/duration.rs
Normal file
29
lib/src/fnc/script/classes/duration.rs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
3
lib/src/fnc/script/classes/mod.rs
Normal file
3
lib/src/fnc/script/classes/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod duration;
|
||||
pub mod record;
|
||||
pub mod uuid;
|
36
lib/src/fnc/script/classes/record.rs
Normal file
36
lib/src/fnc/script/classes/record.rs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
29
lib/src/fnc/script/classes/uuid.rs
Normal file
29
lib/src/fnc/script/classes/uuid.rs
Normal file
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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::<classes::duration::Duration>() {
|
||||
let v = v.into_instance::<classes::duration::Duration>().unwrap();
|
||||
let v: &classes::duration::Duration = v.as_ref();
|
||||
if (&v).instance_of::<classes::duration::duration::Duration>() {
|
||||
let v = v.into_instance::<classes::duration::duration::Duration>().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::<classes::record::Record>() {
|
||||
let v = v.into_instance::<classes::record::Record>().unwrap();
|
||||
let v: &classes::record::Record = v.as_ref();
|
||||
if (&v).instance_of::<classes::record::record::Record>() {
|
||||
let v = v.into_instance::<classes::record::record::Record>().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::<classes::uuid::Uuid>() {
|
||||
let v = v.into_instance::<classes::uuid::Uuid>().unwrap();
|
||||
let v: &classes::uuid::Uuid = v.as_ref();
|
||||
if (&v).instance_of::<classes::uuid::uuid::Uuid>() {
|
||||
let v = v.into_instance::<classes::uuid::uuid::Uuid>().unwrap();
|
||||
let v: &classes::uuid::uuid::Uuid = v.as_ref();
|
||||
let v = v.value.clone();
|
||||
return Ok(Uuid::from(v).into());
|
||||
}
|
||||
|
|
|
@ -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::<classes::duration::Duration>::instance(
|
||||
Value::Duration(v) => Ok(Class::<classes::duration::duration::Duration>::instance(
|
||||
ctx,
|
||||
classes::duration::Duration {
|
||||
classes::duration::duration::Duration {
|
||||
value: v.to_raw(),
|
||||
},
|
||||
)?
|
||||
.into_value()),
|
||||
Value::Thing(v) => Ok(Class::<classes::record::Record>::instance(
|
||||
Value::Thing(v) => Ok(Class::<classes::record::record::Record>::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::<classes::uuid::Uuid>::instance(
|
||||
Value::Uuid(v) => Ok(Class::<classes::uuid::uuid::Uuid>::instance(
|
||||
ctx,
|
||||
classes::uuid::Uuid {
|
||||
classes::uuid::uuid::Uuid {
|
||||
value: v.to_raw(),
|
||||
},
|
||||
)?
|
||||
|
|
|
@ -40,11 +40,11 @@ pub async fn run(
|
|||
// Register the fetch function as a global object
|
||||
global.init_def::<globals::fetch::Fetch>()?;
|
||||
// Register the Duration type as a global class
|
||||
global.init_def::<classes::Duration>()?;
|
||||
global.init_def::<classes::duration::Duration>()?;
|
||||
// Register the Record type as a global class
|
||||
global.init_def::<classes::Record>()?;
|
||||
global.init_def::<classes::record::Record>()?;
|
||||
// Register the Uuid type as a global class
|
||||
global.init_def::<classes::Uuid>()?;
|
||||
global.init_def::<classes::uuid::Uuid>()?;
|
||||
// Attempt to compile the script
|
||||
let res = ctx.compile("script", src)?;
|
||||
// Attempt to fetch the main export
|
||||
|
|
Loading…
Reference in a new issue