Skip to content

Commit 1c63db3

Browse files
committed
Rename crate
1 parent f0e4313 commit 1c63db3

File tree

7 files changed

+68
-16
lines changed

7 files changed

+68
-16
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
2-
name = "rquickjs-moduledef-ext"
2+
name = "rquickjs-module"
33
version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
rquickjs = { version = "*", features = ["full-async", "parallel", "macro"] }
7+
rquickjs = { version = "0.8.1", features = ["loader"] }
8+
9+
[dev-dependencies]
10+
rquickjs = { version = "0.8.1", features = ["futures", "macro"] }
811
tokio = { version = "1", features = ["full"] }

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
1-
# rquickjs-moduledef-ext
2-
Experiment to improve ModuleDef
1+
# rquickjs module
2+
3+
[![github](https://img.shields.io/badge/github-delskayn/rquickjs-8da0cb.svg?style=for-the-badge&logo=github)](https://github.com/rquickjs/rquickjs-module)
4+
[![crates](https://img.shields.io/crates/v/rquickjs.svg?style=for-the-badge&color=fc8d62&logo=rust)](https://crates.io/crates/rquickjs-module)
5+
6+
This is an extension to [rquickjs](https://github.com/DelSkayn/rquickjs) to allow the ecosystem to create more unified Rust modules.
7+
8+
The goal was to create a better version of [`ModuleDef`](https://docs.rs/rquickjs/latest/rquickjs/module/trait.ModuleDef.html) that would allow it to have options as input and set global.
9+
10+
For example, a `fetch` module using `ModuleDefExt` could set a global `fetch` function and have as options an allowlist of domains.
11+
12+
## Usage

src/definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rquickjs::{
1010
///
1111
/// ```
1212
/// use rquickjs::{Ctx, JsLifetime, Object, Result};
13-
/// use rquickjs_moduledef_ext::{ModuleDefExt, ModuleImpl};
13+
/// use rquickjs_module::{ModuleDefExt, ModuleImpl};
1414
///
1515
/// #[derive(JsLifetime, Debug)]
1616
/// struct MyModuleOptions {

src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ mod tests {
1414
JsLifetime, Object, Result, Value,
1515
};
1616

17-
use crate::{
18-
definition::{GlobalsOnly, ModuleImpl},
19-
globals_only_module, ModuleLoader,
20-
};
17+
use crate::{globals_only_module, GlobalsOnly, ModuleImpl, ModuleLoader};
2118

2219
use super::ModuleDefExt;
2320

@@ -29,9 +26,7 @@ mod tests {
2926
&GlobalsOnly
3027
}
3128

32-
fn options(self) -> () {
33-
()
34-
}
29+
fn options(self) {}
3530
}
3631

3732
struct Example2;

src/loader/builder.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@ fn load_module_func<D: ModuleDef>(ctx: Ctx<'_>, name: Vec<u8>) -> Result<Module<
1212
Module::declare_def::<D, _>(ctx, name)
1313
}
1414

15+
/// Builder to create a [`ModuleLoader`], [`ModuleResolver`] and [`GlobalInitializer`]
16+
///
17+
/// # Example
18+
/// ```rust
19+
/// use rquickjs_module::{ModuleLoader, ModuleDefExt, ModuleImpl};
20+
///
21+
/// struct MyModule;
22+
///
23+
/// impl ModuleDefExt for MyModule {
24+
/// type Implementation = ModuleImpl<()>;
25+
///
26+
/// fn implementation() -> &'static Self::Implementation {
27+
/// &ModuleImpl {
28+
/// declare: |decl| {
29+
/// decl.declare("hello")?;
30+
/// Ok(())
31+
/// },
32+
/// evaluate: |ctx, exports, options| {
33+
/// exports.export("hello", "world".to_string())?;
34+
/// Ok(())
35+
/// },
36+
/// name: "my-module",
37+
/// }
38+
/// }
39+
///
40+
/// fn options(self) -> () {}
41+
/// }
42+
///
43+
/// ```
1544
#[derive(Default)]
1645
pub struct ModuleLoaderBuilder {
1746
modules: HashMap<&'static str, ModuleLoadFn>,
@@ -20,6 +49,17 @@ pub struct ModuleLoaderBuilder {
2049
}
2150

2251
impl ModuleLoaderBuilder {
52+
#[must_use]
53+
pub fn with_module<O, M, R>(mut self, module: M) -> Self
54+
where
55+
for<'js> O: JsLifetime<'js> + 'static,
56+
R: ModuleDef + HasModule,
57+
M: AsModule<O, R>,
58+
{
59+
self.add_module(module);
60+
self
61+
}
62+
2363
pub fn add_module<O, M, R>(&mut self, module: M) -> &mut Self
2464
where
2565
for<'js> O: JsLifetime<'js> + 'static,

src/loader/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ use rquickjs::{loader::Loader, Ctx, Error, Module, Object, Result};
55
pub use self::builder::ModuleLoaderBuilder;
66
pub use self::global::GlobalInitializer;
77
pub use self::resolver::ModuleResolver;
8+
89
mod builder;
910
mod global;
1011
mod resolver;
1112

12-
pub type GlobalLoadFn = Box<dyn for<'js> FnOnce(&Ctx<'js>, &Object<'js>) -> Result<()>>;
13-
pub type ModuleLoadFn = for<'js> fn(Ctx<'js>, Vec<u8>) -> Result<Module<'js>>;
13+
type GlobalLoadFn = Box<dyn for<'js> FnOnce(&Ctx<'js>, &Object<'js>) -> Result<()>>;
14+
type ModuleLoadFn = for<'js> fn(Ctx<'js>, Vec<u8>) -> Result<Module<'js>>;
1415

16+
/// Loader for Rust modules defined using [`crate::ModuleDefExt`].
17+
///
18+
/// See [`ModuleLoaderBuilder`] for usage.
1519
pub struct ModuleLoader {
1620
modules: HashMap<&'static str, ModuleLoadFn>,
1721
}

src/wrapper/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ mod module;
88
/// Module metadata
99
///
1010
/// We use this trait to still access metadata once we have
11-
/// converted it from a `ModuleDefExt` to a `ModuleDef`.
11+
/// converted it from a [`ModuleDefExt`] to a [`ModuleDef`].
1212
///
1313
/// This is necessary for the loader to work.
1414
pub trait HasModule {
1515
fn name() -> &'static str;
1616
fn is_module() -> bool;
1717
}
1818

19-
/// Convert a `ModuleDefExt` to a `ModuleDef`
19+
/// Convert a [`ModuleDefExt`] to a [`ModuleDef`]
2020
pub trait AsModule<O, R>
2121
where
2222
Self: ModuleDefExt<O>,

0 commit comments

Comments
 (0)