Skip to content

Commit

Permalink
Consider undefined/null for optional rust types
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Nov 17, 2023
1 parent 7c87f99 commit ea9423c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [6.0.2] - 2023-11-17
- Consider undefined/null for rust type Option<T>

## [6.0.1] - 2023-09-27
- doc update

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-bindgen"
version = "6.0.1"
version = "6.0.2"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2021"
description = "easy way to write nodejs module using rust"
Expand Down
2 changes: 1 addition & 1 deletion nj-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nj-core"
version = "6.0.0"
version = "6.0.1"
authors = ["fluvio.io"]
edition = "2021"
description = "high level wrapper for Node N-API"
Expand Down
14 changes: 13 additions & 1 deletion nj-core/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,14 @@ impl JsEnv {
Ok(valuetype)
}

/// is value undefined or null
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn is_undefined_or_null(&self, napi_value: napi_value) -> Result<bool, NjError> {
let valuetype = self.value_type(napi_value)?;
Ok(valuetype == crate::sys::napi_valuetype_napi_undefined
|| valuetype == crate::sys::napi_valuetype_napi_null)
}

/// get string representation of value type
pub fn value_type_string(&self, napi_value: napi_value) -> Result<&'static str, NjError> {
Ok(napi_value_type_to_string(self.value_type(napi_value)?))
Expand Down Expand Up @@ -886,7 +894,11 @@ where

fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result<Self, NjError> {
if index < js_cb.args.len() {
Ok(Some(T::convert_to_rust(js_cb.env(), js_cb.args[index])?))
if js_cb.env().is_undefined_or_null(js_cb.args[index])? {
Ok(None)
} else {
Ok(Some(T::convert_to_rust(js_cb.env(), js_cb.args[index])?))
}
} else {
Ok(None)
}
Expand Down

0 comments on commit ea9423c

Please sign in to comment.