diff --git a/Cargo.toml b/Cargo.toml index d926ac2..a5103ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,13 +11,24 @@ readme = "README.md" default = ["all"] all = ["timers", "url", "console", "sqlite"] -timers = ["tokio/time"] -url = [] -console = [] -sqlite = ["sqlx"] +timers = ["rquickjs-extra-timers"] +url = ["rquickjs-extra-url"] +console = ["rquickjs-extra-console"] +sqlite = ["rquickjs-extra-sqlite"] [dependencies] +rquickjs-extra-console = { path = "modules/console", optional = true } +rquickjs-extra-sqlite = { path = "modules/sqlite", optional = true } +rquickjs-extra-timers = { path = "modules/timers", optional = true } +rquickjs-extra-url = { path = "modules/url", optional = true } + +[workspace] +resolver = "2" +members = ["modules/*", "libs/*"] + +[workspace.dependencies] either = "1" +futures = { version = "0.3" } log = { version = "0.4" } rquickjs = { version = "0.6", features = [ "array-buffer", @@ -28,9 +39,5 @@ rquickjs = { version = "0.6", features = [ sqlx = { version = "0.8.2", default-features = false, features = [ "sqlite", "runtime-tokio", -], optional = true } +] } tokio = { version = "1" } - -[dev-dependencies] -futures = { version = "0.3" } -tokio = { version = "1", features = ["full"] } diff --git a/libs/test/Cargo.toml b/libs/test/Cargo.toml new file mode 100644 index 0000000..46a7da7 --- /dev/null +++ b/libs/test/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rquickjs-extra-test" +description = "Test library for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +futures = { workspace = true } +rquickjs = { workspace = true } +tokio = { workspace = true, features = ["full"] } diff --git a/src/test.rs b/libs/test/src/lib.rs similarity index 100% rename from src/test.rs rename to libs/test/src/lib.rs diff --git a/libs/utils/Cargo.toml b/libs/utils/Cargo.toml new file mode 100644 index 0000000..33a875b --- /dev/null +++ b/libs/utils/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "rquickjs-extra-utils" +description = "Utils library for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +rquickjs = { workspace = true } diff --git a/src/ffi/c_string.rs b/libs/utils/src/ffi/c_string.rs similarity index 95% rename from src/ffi/c_string.rs rename to libs/utils/src/ffi/c_string.rs index 7cc7fca..27de0dd 100644 --- a/src/ffi/c_string.rs +++ b/libs/utils/src/ffi/c_string.rs @@ -42,6 +42,10 @@ impl<'js> CString<'js> { self.len } + pub fn is_empty(&self) -> bool { + self.len == 0 + } + pub fn as_str(&self) -> Result<&str> { let bytes = unsafe { slice::from_raw_parts(self.ptr as *const u8, self.len) }; str::from_utf8(bytes) diff --git a/src/ffi/c_vec.rs b/libs/utils/src/ffi/c_vec.rs similarity index 88% rename from src/ffi/c_vec.rs rename to libs/utils/src/ffi/c_vec.rs index d7be3bd..1515f33 100644 --- a/src/ffi/c_vec.rs +++ b/libs/utils/src/ffi/c_vec.rs @@ -1,6 +1,6 @@ use rquickjs::{Result, TypedArray, Value}; -use crate::utils::result::ResultExt; +use crate::result::ResultExt; #[derive(Debug)] pub struct CVec<'js> { @@ -29,6 +29,10 @@ impl<'js> CVec<'js> { self.len } + pub fn is_empty(&self) -> bool { + self.len == 0 + } + pub fn as_slice(&self) -> &[u8] { unsafe { std::slice::from_raw_parts(self.ptr, self.len) } } diff --git a/src/ffi/mod.rs b/libs/utils/src/ffi/mod.rs similarity index 100% rename from src/ffi/mod.rs rename to libs/utils/src/ffi/mod.rs diff --git a/src/utils/mod.rs b/libs/utils/src/lib.rs similarity index 71% rename from src/utils/mod.rs rename to libs/utils/src/lib.rs index a5fc50c..76f2311 100644 --- a/src/utils/mod.rs +++ b/libs/utils/src/lib.rs @@ -1,2 +1,3 @@ +pub mod ffi; pub mod module; pub mod result; diff --git a/src/utils/module.rs b/libs/utils/src/module.rs similarity index 100% rename from src/utils/module.rs rename to libs/utils/src/module.rs diff --git a/src/utils/result.rs b/libs/utils/src/result.rs similarity index 100% rename from src/utils/result.rs rename to libs/utils/src/result.rs diff --git a/modules/console/Cargo.toml b/modules/console/Cargo.toml new file mode 100644 index 0000000..3374e73 --- /dev/null +++ b/modules/console/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rquickjs-extra-console" +description = "Console module for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +log = { workspace = true } +rquickjs = { workspace = true } + +[dev-dependencies] +rquickjs-extra-test = { path = "../../libs/test" } diff --git a/src/modules/console/formatter.rs b/modules/console/src/formatter.rs similarity index 99% rename from src/modules/console/formatter.rs rename to modules/console/src/formatter.rs index ccc76bb..a6d2376 100644 --- a/src/modules/console/formatter.rs +++ b/modules/console/src/formatter.rs @@ -220,8 +220,9 @@ impl FormatterBuilder { #[cfg(test)] mod tests { + use rquickjs_extra_test::test_with; + use super::*; - use crate::test::test_with; type StdString = std::string::String; diff --git a/src/modules/console/mod.rs b/modules/console/src/lib.rs similarity index 98% rename from src/modules/console/mod.rs rename to modules/console/src/lib.rs index 11f886c..47b5c24 100644 --- a/src/modules/console/mod.rs +++ b/modules/console/src/lib.rs @@ -94,8 +94,9 @@ pub fn init(ctx: &Ctx<'_>) -> Result<()> { #[cfg(test)] mod tests { + use rquickjs_extra_test::test_with; + use super::*; - use crate::test::test_with; #[test] fn test_console() { diff --git a/modules/os/Cargo.toml b/modules/os/Cargo.toml new file mode 100644 index 0000000..9360a91 --- /dev/null +++ b/modules/os/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "rquickjs-extra-os" +description = "OS module for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] + +[dev-dependencies] +futures = { version = "0.3" } +tokio = { version = "1", features = ["full"] } diff --git a/modules/os/src/lib.rs b/modules/os/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/modules/sqlite/Cargo.toml b/modules/sqlite/Cargo.toml new file mode 100644 index 0000000..a0d01e0 --- /dev/null +++ b/modules/sqlite/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "rquickjs-extra-sqlite" +description = "SQLite module for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +either = { workspace = true } +rquickjs = { workspace = true } +rquickjs-extra-utils = { path = "../../libs/utils" } +sqlx = { workspace = true } + +[dev-dependencies] +rquickjs-extra-test = { path = "../../libs/test" } +tokio = { workspace = true, features = ["full"] } diff --git a/src/modules/sqlite/argument.rs b/modules/sqlite/src/argument.rs similarity index 95% rename from src/modules/sqlite/argument.rs rename to modules/sqlite/src/argument.rs index 963fdcc..8929c71 100644 --- a/src/modules/sqlite/argument.rs +++ b/modules/sqlite/src/argument.rs @@ -1,11 +1,10 @@ use rquickjs::{Ctx, Exception, FromJs, Result, TypedArray}; +use rquickjs_extra_utils::ffi::{CString, CVec}; +use rquickjs_extra_utils::result::ResultExt; use sqlx::query::Query; use sqlx::sqlite::SqliteArguments; use sqlx::Sqlite; -use crate::ffi::{CString, CVec}; -use crate::utils::result::ResultExt; - #[derive(Debug)] pub enum Argument<'js> { Null, diff --git a/src/modules/sqlite/database.rs b/modules/sqlite/src/database.rs similarity index 95% rename from src/modules/sqlite/database.rs rename to modules/sqlite/src/database.rs index 757091e..45900a7 100644 --- a/src/modules/sqlite/database.rs +++ b/modules/sqlite/src/database.rs @@ -1,8 +1,8 @@ use rquickjs::{Ctx, Result}; +use rquickjs_extra_utils::result::ResultExt; use sqlx::{Executor, SqlitePool}; use super::Statement; -use crate::utils::result::ResultExt; #[rquickjs::class] #[derive(rquickjs::class::Trace)] @@ -41,9 +41,9 @@ impl Database { #[cfg(test)] mod tests { use rquickjs::CatchResultExt; + use rquickjs_extra_test::{call_test, test_async_with, ModuleEvaluator}; - use crate::sqlite::SqliteModule; - use crate::test::{call_test, test_async_with, ModuleEvaluator}; + use crate::SqliteModule; #[tokio::test] async fn test_database_exec() { diff --git a/src/modules/sqlite/mod.rs b/modules/sqlite/src/lib.rs similarity index 94% rename from src/modules/sqlite/mod.rs rename to modules/sqlite/src/lib.rs index 90876b8..a04787c 100644 --- a/src/modules/sqlite/mod.rs +++ b/modules/sqlite/src/lib.rs @@ -3,13 +3,13 @@ use rquickjs::{ module::{Declarations, Exports, ModuleDef}, Class, Ctx, Result, }; +use rquickjs_extra_utils::module::export_default; pub use self::argument::Argument; pub use self::database::Database; pub use self::open::{open, OpenOptions}; pub use self::statement::Statement; pub use self::value::Value; -use crate::utils::module::export_default; mod argument; mod database; diff --git a/src/modules/sqlite/open.rs b/modules/sqlite/src/open.rs similarity index 98% rename from src/modules/sqlite/open.rs rename to modules/sqlite/src/open.rs index b9ee359..88f9bef 100644 --- a/src/modules/sqlite/open.rs +++ b/modules/sqlite/src/open.rs @@ -6,10 +6,9 @@ use std::{ use either::Either; use rquickjs::{Ctx, FromJs, Null, Object, Result, Value}; +use rquickjs_extra_utils::result::ResultExt; use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; -use crate::utils::result::ResultExt; - use super::Database; static IN_MEMORY_DB_SEQ: AtomicUsize = AtomicUsize::new(0); diff --git a/src/modules/sqlite/statement.rs b/modules/sqlite/src/statement.rs similarity index 97% rename from src/modules/sqlite/statement.rs rename to modules/sqlite/src/statement.rs index 1eb4254..745698e 100644 --- a/src/modules/sqlite/statement.rs +++ b/modules/sqlite/src/statement.rs @@ -1,12 +1,11 @@ use rquickjs::function::Rest; use rquickjs::{Ctx, Object, Result}; +use rquickjs_extra_utils::result::ResultExt; use sqlx::query::Query; use sqlx::sqlite::SqliteArguments; use sqlx::Sqlite; use sqlx::{sqlite::SqliteStatement, Column as _, Row as _, SqlitePool, Statement as _}; -use crate::utils::result::ResultExt; - use super::{Argument, Value}; #[rquickjs::class] @@ -101,9 +100,9 @@ impl Statement { #[cfg(test)] mod tests { use rquickjs::CatchResultExt; + use rquickjs_extra_test::{call_test, test_async_with, ModuleEvaluator}; - use crate::sqlite::SqliteModule; - use crate::test::{call_test, test_async_with, ModuleEvaluator}; + use crate::SqliteModule; #[tokio::test] async fn test_statement_all() { diff --git a/src/modules/sqlite/value.rs b/modules/sqlite/src/value.rs similarity index 97% rename from src/modules/sqlite/value.rs rename to modules/sqlite/src/value.rs index 4280965..ac8030d 100644 --- a/src/modules/sqlite/value.rs +++ b/modules/sqlite/src/value.rs @@ -1,9 +1,8 @@ use rquickjs::{Ctx, Exception, IntoJs, Result, String, TypedArray}; +use rquickjs_extra_utils::result::ResultExt; use sqlx::sqlite::{SqliteColumn, SqliteRow}; use sqlx::{Column as _, Decode, Row as _, TypeInfo as _, ValueRef}; -use crate::utils::result::ResultExt; - pub enum Value<'q> { Null, Integer(i64), diff --git a/modules/timers/Cargo.toml b/modules/timers/Cargo.toml new file mode 100644 index 0000000..0994f29 --- /dev/null +++ b/modules/timers/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "rquickjs-extra-timers" +description = "Timers module for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +log = { workspace = true } +rquickjs = { workspace = true } +tokio = { workspace = true, features = ["time"] } + +[dev-dependencies] +futures = { workspace = true } +rquickjs-extra-test = { path = "../../libs/test" } +tokio = { workspace = true, features = ["full"] } diff --git a/src/modules/timers.rs b/modules/timers/src/lib.rs similarity index 99% rename from src/modules/timers.rs rename to modules/timers/src/lib.rs index 624ba8f..f0cd657 100644 --- a/src/modules/timers.rs +++ b/modules/timers/src/lib.rs @@ -113,9 +113,9 @@ mod tests { use futures::FutureExt; use rquickjs::promise::Promise; use rquickjs::CatchResultExt; + use rquickjs_extra_test::test_async_with; use super::*; - use crate::test::test_async_with; #[tokio::test] async fn test_set_timeout() { diff --git a/modules/url/Cargo.toml b/modules/url/Cargo.toml new file mode 100644 index 0000000..06a7056 --- /dev/null +++ b/modules/url/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rquickjs-extra-url" +description = "URL module for RQuickJS" +version = "0.0.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/rquickjs/rquickjs-extra" +readme = "README.md" + +[dependencies] +either = { workspace = true } +rquickjs = { workspace = true } + +[dev-dependencies] +rquickjs-extra-test = { path = "../../libs/test" } diff --git a/src/modules/url/mod.rs b/modules/url/src/lib.rs similarity index 100% rename from src/modules/url/mod.rs rename to modules/url/src/lib.rs diff --git a/src/modules/url/url_search_params.rs b/modules/url/src/url_search_params.rs similarity index 99% rename from src/modules/url/url_search_params.rs rename to modules/url/src/url_search_params.rs index 79c7c0e..4f0e53b 100644 --- a/src/modules/url/url_search_params.rs +++ b/modules/url/src/url_search_params.rs @@ -325,9 +325,9 @@ impl<'js> FromJs<'js> for URLSearchParamsInput<'js> { #[cfg(test)] mod tests { use rquickjs::{CatchResultExt, Class}; + use rquickjs_extra_test::test_with; use super::*; - use crate::test::test_with; #[test] fn test_basic() { diff --git a/src/lib.rs b/src/lib.rs index e9b3ec8..4c2f224 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,11 @@ -pub use self::modules::*; +#[cfg(feature = "console")] +pub use rquickjs_extra_console as console; -mod ffi; -mod modules; -#[cfg(test)] -mod test; -mod utils; +#[cfg(feature = "sqlite")] +pub use rquickjs_extra_sqlite as sqlite; + +#[cfg(feature = "timers")] +pub use rquickjs_extra_timers as timers; + +#[cfg(feature = "url")] +pub use rquickjs_extra_url as url; diff --git a/src/modules/mod.rs b/src/modules/mod.rs deleted file mode 100644 index 91e8cc6..0000000 --- a/src/modules/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -#[cfg(feature = "console")] -pub mod console; -#[cfg(feature = "sqlite")] -pub mod sqlite; -#[cfg(feature = "timers")] -pub mod timers; -#[cfg(feature = "url")] -pub mod url;