From 7715c5a674624b27e092ebc0fa3508b46030bb7d Mon Sep 17 00:00:00 2001 From: "italiano@oplabs.co" Date: Tue, 19 Nov 2024 15:47:53 -0500 Subject: [PATCH 1/3] Adds toml-parser with napi bindings --- .gitignore | 1 + nx.json | 10 + packages/cli/package.json | 2 +- packages/cli/tsconfig.json | 2 +- packages/cli/tsup.config.ts | 3 - packages/toml-parser/.gitignore | 2 + packages/toml-parser/Cargo.lock | 326 ++++++++++++++++++++++++++++++ packages/toml-parser/Cargo.toml | 16 ++ packages/toml-parser/build.rs | 5 + packages/toml-parser/package.json | 12 ++ packages/toml-parser/src/lib.rs | 26 +++ pnpm-lock.yaml | 25 ++- 12 files changed, 416 insertions(+), 14 deletions(-) create mode 100644 nx.json create mode 100644 packages/toml-parser/.gitignore create mode 100644 packages/toml-parser/Cargo.lock create mode 100644 packages/toml-parser/Cargo.toml create mode 100644 packages/toml-parser/build.rs create mode 100644 packages/toml-parser/package.json create mode 100644 packages/toml-parser/src/lib.rs diff --git a/.gitignore b/.gitignore index 4ce910e..18bb198 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ packages/*/.env /coverage # build artifacts +**/target/ **/build/ **/dist/ **/tsconfig.tsbuildinfo diff --git a/nx.json b/nx.json new file mode 100644 index 0000000..34b1ea6 --- /dev/null +++ b/nx.json @@ -0,0 +1,10 @@ +{ + "$schema": "./node_modules/nx/schemas/nx-schema.json", + "neverConnectToCloud": true, + "targetDefaults": { + "build": { + "cache": true, + "dependsOn": ["^build"] + } + } + } \ No newline at end of file diff --git a/packages/cli/package.json b/packages/cli/package.json index a0eec19..4a23b6e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -27,6 +27,7 @@ "ink": "^5.0.1", "pastel": "^3.0.0", "react": "^18.2.0", + "toml-parser": "workspace:*", "viem": "^2.21.41", "zod": "^3.21.4", "zod-validation-error": "^3.4.0", @@ -36,7 +37,6 @@ "@types/node": "^22.9.0", "@types/react": "^18.0.32", "@vdemedes/prettier-config": "^2.0.1", - "esbuild-plugin-wasm": "^1.1.0", "eslint-config-xo-react": "^0.27.0", "eslint-plugin-react": "^7.32.2", "eslint-plugin-react-hooks": "^4.6.0", diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index ece9d67..1ca5251 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -38,4 +38,4 @@ "noEmitOnError": true }, "include": ["src"] -} +} \ No newline at end of file diff --git a/packages/cli/tsup.config.ts b/packages/cli/tsup.config.ts index 96ca245..f75dceb 100644 --- a/packages/cli/tsup.config.ts +++ b/packages/cli/tsup.config.ts @@ -1,8 +1,6 @@ /* eslint-disable no-console */ import { defineConfig } from 'tsup' -import { wasmLoader } from 'esbuild-plugin-wasm' -const wasmPlugin = wasmLoader({ mode: 'embedded' }) export default defineConfig({ name: 'super', @@ -12,5 +10,4 @@ export default defineConfig({ splitting: false, sourcemap: true, clean: true, - esbuildPlugins: [wasmPlugin], }) diff --git a/packages/toml-parser/.gitignore b/packages/toml-parser/.gitignore new file mode 100644 index 0000000..cab659e --- /dev/null +++ b/packages/toml-parser/.gitignore @@ -0,0 +1,2 @@ +index.node +index.d.ts diff --git a/packages/toml-parser/Cargo.lock b/packages/toml-parser/Cargo.lock new file mode 100644 index 0000000..5f7881d --- /dev/null +++ b/packages/toml-parser/Cargo.lock @@ -0,0 +1,326 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "libloading" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" +dependencies = [ + "cfg-if", + "windows-targets", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "napi" +version = "2.16.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "214f07a80874bb96a8433b3cdfc84980d56c7b02e1a0d7ba4ba0db5cef785e2b" +dependencies = [ + "bitflags", + "ctor", + "napi-derive", + "napi-sys", + "once_cell", +] + +[[package]] +name = "napi-build" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1c0f5d67ee408a4685b61f5ab7e58605c8ae3f2b4189f0127d804ff13d5560a" + +[[package]] +name = "napi-derive" +version = "2.16.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17435f7a00bfdab20b0c27d9c56f58f6499e418252253081bfff448099da31d1" +dependencies = [ + "cfg-if", + "convert_case", + "napi-derive-backend", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "napi-derive-backend" +version = "1.0.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "967c485e00f0bf3b1bdbe510a38a4606919cf1d34d9a37ad41f25a81aa077abe" +dependencies = [ + "convert_case", + "once_cell", + "proc-macro2", + "quote", + "regex", + "semver", + "syn", +] + +[[package]] +name = "napi-sys" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427802e8ec3a734331fec1035594a210ce1ff4dc5bc1950530920ab717964ea3" +dependencies = [ + "libloading", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "serde" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] +name = "toml-parser" +version = "0.0.1" +dependencies = [ + "napi", + "napi-build", + "napi-derive", + "serde_json", + "toml", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/packages/toml-parser/Cargo.toml b/packages/toml-parser/Cargo.toml new file mode 100644 index 0000000..5726fa2 --- /dev/null +++ b/packages/toml-parser/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "toml-parser" +edition = "2021" +version = "0.0.1" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +napi = "2" +napi-derive = "2" +serde_json = "1.0" +toml = "0.5" + +[build-dependencies] +napi-build = "2" diff --git a/packages/toml-parser/build.rs b/packages/toml-parser/build.rs new file mode 100644 index 0000000..1f866b6 --- /dev/null +++ b/packages/toml-parser/build.rs @@ -0,0 +1,5 @@ +extern crate napi_build; + +fn main() { + napi_build::setup(); +} diff --git a/packages/toml-parser/package.json b/packages/toml-parser/package.json new file mode 100644 index 0000000..4720514 --- /dev/null +++ b/packages/toml-parser/package.json @@ -0,0 +1,12 @@ +{ + "name": "toml-parser", + "type": "module", + "main": "./index.node", + "version": "0.0.1", + "scripts": { + "build": "napi build --release" + }, + "dependencies": { + "@napi-rs/cli": "^2.18.4" + } +} diff --git a/packages/toml-parser/src/lib.rs b/packages/toml-parser/src/lib.rs new file mode 100644 index 0000000..1f14a4d --- /dev/null +++ b/packages/toml-parser/src/lib.rs @@ -0,0 +1,26 @@ +use std::collections::HashMap; + +use napi::bindgen_prelude::*; +use napi_derive::napi; + +use toml; +use serde_json; + +type TomlContent = HashMap; + +#[napi] +pub fn parse_toml(toml_str: String) -> Result { + let parsed = toml::from_str::(&toml_str); + + match parsed { + Ok(data) => { + let json_str = serde_json::to_string(&data) + .map_err(|e| napi::Error::new(napi::Status::InvalidArg, &e.to_string()))?; + Ok(json_str) + } + Err(e) => { + let err = napi::Error::new(napi::Status::InvalidArg, &e.to_string()); + Err(err) + } + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 31cec0d..7eb69a2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,9 @@ importers: react: specifier: ^18.2.0 version: 18.3.1 + toml-parser: + specifier: workspace:* + version: link:../toml-parser viem: specifier: ^2.21.41 version: 2.21.41(typescript@5.6.3)(zod@3.23.8) @@ -63,9 +66,6 @@ importers: '@vdemedes/prettier-config': specifier: ^2.0.1 version: 2.0.1 - esbuild-plugin-wasm: - specifier: ^1.1.0 - version: 1.1.0 eslint-config-xo-react: specifier: ^0.27.0 version: 0.27.0(eslint-plugin-react-hooks@4.6.2(eslint@8.57.1))(eslint-plugin-react@7.37.2(eslint@8.57.1))(eslint@8.57.1) @@ -103,6 +103,12 @@ importers: specifier: ^5.4.11 version: 5.4.11(@types/node@22.9.0) + packages/toml-parser: + dependencies: + '@napi-rs/cli': + specifier: ^2.18.4 + version: 2.18.4 + packages: '@adraffy/ens-normalize@1.11.0': @@ -705,6 +711,11 @@ packages: '@jridgewell/trace-mapping@0.3.9': resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} + '@napi-rs/cli@2.18.4': + resolution: {integrity: sha512-SgJeA4df9DE2iAEpr3M2H0OKl/yjtg1BnRI5/JyowS71tUWhrfSu2LT0V3vlHET+g1hBVlrO60PmEXwUEKp8Mg==} + engines: {node: '>= 10'} + hasBin: true + '@napi-rs/wasm-runtime@0.2.4': resolution: {integrity: sha512-9zESzOO5aDByvhIAsOy9TbpZ0Ur2AJbUI7UT73kcUTS2mxAMHOBaa1st/jAymNoCtvrit99kkzT1FZuXVcgfIQ==} @@ -1390,10 +1401,6 @@ packages: resolution: {integrity: sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==} engines: {node: '>= 0.4'} - esbuild-plugin-wasm@1.1.0: - resolution: {integrity: sha512-0bQ6+1tUbySSnxzn5jnXHMDvYnT0cN/Wd4Syk8g/sqAIJUg7buTIi22svS3Qz6ssx895NT+TgLPb33xi1OkZig==} - engines: {node: '>=0.10.0'} - esbuild@0.21.5: resolution: {integrity: sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==} engines: {node: '>=12'} @@ -3379,6 +3386,8 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.0 + '@napi-rs/cli@2.18.4': {} + '@napi-rs/wasm-runtime@0.2.4': dependencies: '@emnapi/core': 1.3.1 @@ -4070,8 +4079,6 @@ snapshots: is-date-object: 1.0.5 is-symbol: 1.0.4 - esbuild-plugin-wasm@1.1.0: {} - esbuild@0.21.5: optionalDependencies: '@esbuild/aix-ppc64': 0.21.5 From 8a74f97d6a9a2ed6fc041d7f113f6f4bffbc92eb Mon Sep 17 00:00:00 2001 From: "italiano@oplabs.co" Date: Tue, 19 Nov 2024 18:27:53 -0500 Subject: [PATCH 2/3] Add tests and linting --- packages/toml-parser/build.rs | 2 +- packages/toml-parser/package.json | 5 ++++- packages/toml-parser/src/lib.rs | 25 ++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/packages/toml-parser/build.rs b/packages/toml-parser/build.rs index 1f866b6..9fc2367 100644 --- a/packages/toml-parser/build.rs +++ b/packages/toml-parser/build.rs @@ -1,5 +1,5 @@ extern crate napi_build; fn main() { - napi_build::setup(); + napi_build::setup(); } diff --git a/packages/toml-parser/package.json b/packages/toml-parser/package.json index 4720514..ae0305f 100644 --- a/packages/toml-parser/package.json +++ b/packages/toml-parser/package.json @@ -4,7 +4,10 @@ "main": "./index.node", "version": "0.0.1", "scripts": { - "build": "napi build --release" + "build": "napi build --release", + "test": "cargo test", + "lint": "cargo fmt --check", + "lint:fix": "cargo fmt && cargo fmt --check" }, "dependencies": { "@napi-rs/cli": "^2.18.4" diff --git a/packages/toml-parser/src/lib.rs b/packages/toml-parser/src/lib.rs index 1f14a4d..3622b6f 100644 --- a/packages/toml-parser/src/lib.rs +++ b/packages/toml-parser/src/lib.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use napi::bindgen_prelude::*; use napi_derive::napi; -use toml; use serde_json; +use toml; type TomlContent = HashMap; @@ -24,3 +24,26 @@ pub fn parse_toml(toml_str: String) -> Result { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_parse_toml() { + let toml_str = r#" + [testcli] + test_key = "test" + + [testcli.test_nested] + test_arr = [1, 2, 3] + "# + .to_string(); + let result = parse_toml(toml_str); + assert_eq!(result.is_ok(), true); + assert_eq!( + result.unwrap(), + r#"{"testcli":{"test_key":"test","test_nested":{"test_arr":[1,2,3]}}}"# + ); + } +} From 137dde0f98beed5a524552aabea7ba190ec8ca46 Mon Sep 17 00:00:00 2001 From: "italiano@oplabs.co" Date: Tue, 19 Nov 2024 19:05:40 -0500 Subject: [PATCH 3/3] adds entry that forces require usage --- packages/toml-parser/index.js | 3 +++ packages/toml-parser/package.json | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 packages/toml-parser/index.js diff --git a/packages/toml-parser/index.js b/packages/toml-parser/index.js new file mode 100644 index 0000000..0e40e81 --- /dev/null +++ b/packages/toml-parser/index.js @@ -0,0 +1,3 @@ +const { parseToml } = require('./index.node'); + +module.exports = { parseToml }; diff --git a/packages/toml-parser/package.json b/packages/toml-parser/package.json index ae0305f..910366c 100644 --- a/packages/toml-parser/package.json +++ b/packages/toml-parser/package.json @@ -1,8 +1,7 @@ { "name": "toml-parser", - "type": "module", - "main": "./index.node", "version": "0.0.1", + "main": "./index.js", "scripts": { "build": "napi build --release", "test": "cargo test",