Skip to content

Commit

Permalink
Automatically serialize uuids (#160)
Browse files Browse the repository at this point in the history
Behind an optional feature flag. Curiously enough it looks like I've found a cargo bug where having a feature named exactly the same as an optional crate, fails to pull in the optional crate. Fixed through a roundabout feature name on core.

Co-authored-by: Michał Kawalec <ext.mkawalec@riotgames.com>
Co-authored-by: Nick Mosher <nicholastmosher@gmail.com>
Co-authored-by: Michal Kawalec <ext.mkawalec@riotgames.com>
  • Loading branch information
3 people committed Jul 12, 2021
1 parent 572d56a commit d9c9f63
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
- Update JSArrayBuffer to be usable in `env.convert_to_rust` ([#136](https://github.com/infinyon/node-bindgen/pull/136))
- Added missing unsigned and signed integer conversions to JS [#158](https://github.com/infinyon/node-bindgen/pull/158)
- `serde_json` is automatically serialized to it's JS representation ([#159](https://github.com/infinyon/node-bindgen/pull/159))
- `uuid`s are automatically serialized to JS ([#160](https://github.com/infinyon/node-bindgen/pull/160))

## [4.3.0] - 2021-03-13
### Improvements
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -13,6 +13,7 @@ default = ["node"]
node = ["nj-sys", "nj-core", "nj-derive"]
build = ["nj-build"]
serde-json = ["nj-core/serde-json"]
uuid = ["nj-core/convert-uuid"]

[dependencies]
nj-sys = { path = "nj-sys", version = "3.0.0", optional = true }
Expand Down
15 changes: 15 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/Cargo.toml
Expand Up @@ -17,4 +17,5 @@ members = [
"cleanup",
"bigint",
"tuples",
"uuid",
]
1 change: 1 addition & 0 deletions examples/uuid/.gitignore
@@ -0,0 +1 @@
dist
15 changes: 15 additions & 0 deletions examples/uuid/Cargo.toml
@@ -0,0 +1,15 @@
[package]
name = "nj-example-uuid"
version = "0.1.0"
authors = ["fluvio.io"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
node-bindgen = { path = "../..", features = ["uuid"] }
uuid = "0.8.2"

[build-dependencies]
node-bindgen = { path = "../../", features = ["build"] }
11 changes: 11 additions & 0 deletions examples/uuid/Makefile
@@ -0,0 +1,11 @@
all: build

build:
nj-cli build

test: build
npx -y ts-node test.ts

clean:
rm -rf dist

1 change: 1 addition & 0 deletions examples/uuid/README.md
@@ -0,0 +1 @@
Pass `uuid::Uuid` objects to and from Node
3 changes: 3 additions & 0 deletions examples/uuid/build.rs
@@ -0,0 +1,3 @@
fn main() {
node_bindgen::build::configure();
}
26 changes: 26 additions & 0 deletions examples/uuid/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/uuid/package.json
@@ -0,0 +1,5 @@
{
"devDependencies": {
"@types/node": "^15.12.0"
}
}
13 changes: 13 additions & 0 deletions examples/uuid/src/lib.rs
@@ -0,0 +1,13 @@
use node_bindgen::derive::node_bindgen;

/// Create a new random Uuid to return
#[node_bindgen]
fn make_uuid() -> uuid::Uuid {
uuid::Uuid::parse_str("f7509856-9ae5-4c07-976d-a5b3f983e4af").unwrap()
}

#[node_bindgen]
fn take_uuid(uuid: uuid::Uuid) {
let string = uuid.to_string();
assert_eq!(string, "f7509856-9ae5-4c07-976d-a5b3f983e4af");
}
12 changes: 12 additions & 0 deletions examples/uuid/test.ts
@@ -0,0 +1,12 @@
const assert = require('assert');

interface UuidExample {
makeUuid(): string;
takeUuid(uuid: string);
}
let addon: UuidExample = require('./dist');

const new_uuid: string = addon.makeUuid();
assert.equal(new_uuid, "f7509856-9ae5-4c07-976d-a5b3f983e4af");

addon.takeUuid(new_uuid);
2 changes: 2 additions & 0 deletions nj-core/Cargo.toml
Expand Up @@ -13,6 +13,7 @@ test = false

[features]
serde-json = ["serde_json"]
convert-uuid = ["uuid"]

[dependencies]
log = "0.4.8"
Expand All @@ -26,3 +27,4 @@ fluvio-future = { version = "0.3.0", default-features=false, features=["task","s
pin-utils = "0.1.0"
num-bigint = "0.4.0"
serde_json = { version = "1.0", optional = true }
uuid = { version = "0.8", optional = true }
22 changes: 22 additions & 0 deletions nj-core/src/convert.rs
Expand Up @@ -126,6 +126,28 @@ impl TryIntoJs for serde_json::Value {
}
}

#[cfg(feature = "convert-uuid")]
impl TryIntoJs for uuid::Uuid {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let as_str = self
.to_hyphenated()
.encode_lower(&mut uuid::Uuid::encode_buffer())
.to_string();

as_str.try_to_js(js_env)
}
}

#[cfg(feature = "convert-uuid")]
impl JSValue<'_> for uuid::Uuid {
fn convert_to_rust(env: &JsEnv, js_value: napi_value) -> Result<Self, NjError> {
let string = String::convert_to_rust(env, js_value)?;
let uuid = uuid::Uuid::parse_str(&string)
.map_err(|e| NjError::Other(format!("Failed to parse Uuid: {}", e)))?;
Ok(uuid)
}
}

impl<T, E> TryIntoJs for Result<T, E>
where
T: TryIntoJs,
Expand Down

0 comments on commit d9c9f63

Please sign in to comment.