Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum support for TryIntoJs, structured errors from Result #155

Merged
merged 24 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
## Unreleased
### Improvements

## [4.4.0] - TBD
## [5.0.0] - TBD
### Improvements
- Added support for automatic conversion of structs into the JS representation by decorating their definition with `#[node_bindgen]`
- Added support for automatic conversion of structs and enums into the JS representation by decorating their definition with `#[node_bindgen]` ([#148](https://github.com/infinyon/node-bindgen/pull/148) and [#155](https://github.com/infinyon/node-bindgen/pull/155))
- Defined a `NjError::Native` Error payload, which allows errors to return structured data to JS
- `Result<T, E>` converts the error value to JS using `TryIntoJs` for structured error payloads
- Add support for passing tuples between Node and Rust ([#142](https://github.com/infinyon/node-bindgen/pull/142))
- Bump electron from 9.3.1 to 9.4.0 in /examples/electron ([#135](https://github.com/infinyon/node-bindgen/pull/135))
- Update JSArrayBuffer to be usable in `env.convert_to_rust` ([#136](https://github.com/infinyon/node-bindgen/pull/136))
Expand Down
97 changes: 50 additions & 47 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-bindgen"
version = "4.4.0"
version = "5.0.0"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2018"
description = "easy way to write nodejs module using rust"
Expand All @@ -15,7 +15,7 @@ build = ["nj-build"]

[dependencies]
nj-sys = { path = "nj-sys", version = "3.0.0", optional = true }
nj-core = { path = "nj-core", version = "4.3.0", optional = true }
nj-core = { path = "nj-core", version = "5.0.0", optional = true }
nj-build = { path = "nj-build", version = "0.2.3", optional = true }
nj-derive = { path = "nj-derive", version = "3.2.0", optional = true }

Expand Down
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ fn my_json() -> MyJson {

```js
let addon = require('./dist');
assert.deepEqual(addon.my_json(), {
assert.deepStrictEqual(addon.my_json(), {
someName: "John",
aNumber: 1337
});
Expand All @@ -281,6 +281,53 @@ Note that the fields must implement
Any references must also implement `Clone`.
Field names will be converted to camelCase.

## Enums

Enums will also have their JS representation autogenerated with the help of `node_bindgen`:

```rust
#[node_bindgen]
enum ErrorType {
WithMessage(String, usize),
WithFields {
val: usize
},
UnitErrorType
}

#[node_bindgen]
fn with_message() -> ErrorType {
ErrorType::WithMessage("test".to_owned(), 321)
}

#[node_bindgen]
fn with_fields() -> ErrorType {
ErrorType::WithFields {
val: 123
}
}

#[node_bindgen]
fn with_unit() -> ErrorType {
ErrorType::UnitErrorType
}
```

```js
assert.deepStrictEqual(addon.withMessage(), {
withMessage: ["test", 321n]
});
assert.deepStrictEqual(addon.withFields(), {
withFields: {
val: 123n
}
});
assert.deepStrictEqual(addon.withUnit(), "UnitErrorType")
```

Tuple variants will be converted into lists, struct variants converted to objects, and unit variants converted into strings matching the variant's name in PascalCase.
Generics and references are supported, with the same caveats as for structs.

## JavaScript class

JavaScript class is supported.
Expand Down