From ea9423ccbc4734848e532c1aac8911e872c32751 Mon Sep 17 00:00:00 2001 From: DmitryAstafyev Date: Fri, 17 Nov 2023 15:00:43 +0100 Subject: [PATCH] Consider undefined/null for optional rust types --- CHANGELOG.md | 3 +++ Cargo.lock | 4 ++-- Cargo.toml | 2 +- nj-core/Cargo.toml | 2 +- nj-core/src/basic.rs | 14 +++++++++++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b49283..c9ba3c03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,7 @@ # Changelog +## [6.0.2] - 2023-11-17 +- Consider undefined/null for rust type Option + ## [6.0.1] - 2023-09-27 - doc update diff --git a/Cargo.lock b/Cargo.lock index 901665bc..81eeb93f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -787,7 +787,7 @@ dependencies = [ [[package]] name = "nj-core" -version = "6.0.0" +version = "6.0.1" dependencies = [ "async-trait", "ctor 0.2.4", @@ -822,7 +822,7 @@ version = "4.0.0" [[package]] name = "node-bindgen" -version = "6.0.1" +version = "6.0.2" dependencies = [ "nj-build", "nj-core", diff --git a/Cargo.toml b/Cargo.toml index f4b82eac..0fd4128a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "node-bindgen" -version = "6.0.1" +version = "6.0.2" authors = ["Fluvio Contributors "] edition = "2021" description = "easy way to write nodejs module using rust" diff --git a/nj-core/Cargo.toml b/nj-core/Cargo.toml index b53c3577..2a68274b 100644 --- a/nj-core/Cargo.toml +++ b/nj-core/Cargo.toml @@ -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" diff --git a/nj-core/src/basic.rs b/nj-core/src/basic.rs index 5196fc1a..3beb06e7 100644 --- a/nj-core/src/basic.rs +++ b/nj-core/src/basic.rs @@ -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 { + 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)?)) @@ -886,7 +894,11 @@ where fn convert_arg_at(js_cb: &'a JsCallback, index: usize) -> Result { 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) }