Skip to content

Commit

Permalink
Generate node bindings using napi-rs
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Nov 22, 2022
1 parent 597d5f6 commit 605ae40
Show file tree
Hide file tree
Showing 12 changed files with 531 additions and 245 deletions.
426 changes: 257 additions & 169 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions aries_vcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ derive_builder = "0.10.2"
tokio = { version = "1.15.0" }
messages = { path = "../messages" }
zmq = { version = "0.9.2" }
napi = "2.10.1"

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.5"
Expand Down
16 changes: 16 additions & 0 deletions aries_vcx/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,22 @@ impl From<SenderConfigBuilderError> for VcxError {
}
}

impl From<VcxError> for napi::Error {
fn from(err: VcxError) -> Self {
let reason = err.to_string();
error!("{}", reason);
napi::Error::new(napi::Status::Unknown, reason)
}
}

impl From<napi::Error> for VcxError {
fn from(err: napi::Error) -> Self {
let reason = err.to_string();
error!("{}", reason);
VcxError::from_msg(VcxErrorKind::UnknownError, reason)
}
}

impl From<AgencyClientErrorKind> for VcxErrorKind {
fn from(agency_err: AgencyClientErrorKind) -> VcxErrorKind {
match agency_err {
Expand Down
2 changes: 2 additions & 0 deletions aries_vcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extern crate derive_builder;
#[cfg(test)]
extern crate async_channel;

extern crate napi;

pub extern crate messages;

#[macro_use]
Expand Down
8 changes: 8 additions & 0 deletions libvcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ chrono = "0.4.19"
time = "0.1.44"
lazy_static = "1.3"
libc = "=0.2.114"
napi = { version = "2.10.1", default-features = false, features = [
"async",
"serde-json",
] }
napi-derive = { version = "2.9.1" }
rand = "0.7.3"
serde = "1.0.97"
serde_json = "1.0.40"
Expand All @@ -44,3 +49,6 @@ android_logger = "0.5"

[dev-dependencies]
tokio = { version = "1.15", features = [ "rt", "macros" ] }

[build-dependencies]
napi-build = "2.0.1"
3 changes: 3 additions & 0 deletions libvcx/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate napi_build;

use std::env;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -33,4 +35,5 @@ fn main() {
}
}
}
napi_build::setup();
}
36 changes: 27 additions & 9 deletions libvcx/src/api_lib/api_c/out_of_band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub extern "C" fn vcx_out_of_band_sender_create(
);

execute_async::<BoxFuture<'static, Result<(), ()>>>(Box::pin(async move {
match out_of_band::create_out_of_band(&config).await {
match out_of_band::create_out_of_band(config).await {
Ok(handle) => {
trace!(
"vcx_out_of_band_sender_create_cb(command_handle: {}, rc: {}, handle: {})",
Expand All @@ -41,6 +41,7 @@ pub extern "C" fn vcx_out_of_band_sender_create(
cb(command_handle, error::SUCCESS.code_num, handle);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_create_cb(command_handle: {}, rc: {}, handle: {})",
Expand Down Expand Up @@ -73,7 +74,7 @@ pub extern "C" fn vcx_out_of_band_receiver_create(
);

execute(move || {
match out_of_band::create_out_of_band_msg_from_msg(&message) {
match out_of_band::create_out_of_band_msg_from_msg(message) {
Ok(handle) => {
trace!(
"vcx_out_of_band_receiver_create_cb(command_handle: {}, rc: {}, handle: {})",
Expand All @@ -84,6 +85,7 @@ pub extern "C" fn vcx_out_of_band_receiver_create(
cb(command_handle, error::SUCCESS.code_num, handle);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_create_cb(command_handle: {}, rc: {}, handle: {}):",
Expand Down Expand Up @@ -127,6 +129,7 @@ pub extern "C" fn vcx_out_of_band_sender_get_thread_id(
cb(command_handle, error::SUCCESS.code_num, thid.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_get_thread_id_cb(command_handle: {}, rc: {}, thid: {})",
Expand Down Expand Up @@ -170,6 +173,7 @@ pub extern "C" fn vcx_out_of_band_receiver_get_thread_id(
cb(command_handle, error::SUCCESS.code_num, thid.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_get_thread_id_cb(command_handle: {}, rc: {}, thid: {})",
Expand Down Expand Up @@ -204,7 +208,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_message(
);

execute(move || {
match out_of_band::append_message(handle, &message) {
match out_of_band::append_message(handle, message) {
Ok(()) => {
trace!(
"vcx_out_of_band_sender_append_message_cb(command_handle: {}, rc: {})",
Expand All @@ -214,6 +218,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_message(
cb(command_handle, error::SUCCESS.code_num);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_append_message_cb(command_handle: {}, rc: {})",
Expand Down Expand Up @@ -248,7 +253,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_service(
);

execute(move || {
match out_of_band::append_service(handle, &service) {
match out_of_band::append_service(handle, service) {
Ok(()) => {
trace!(
"vcx_out_of_band_sender_append_service_cb(command_handle: {}, rc: {})",
Expand All @@ -258,6 +263,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_service(
cb(command_handle, error::SUCCESS.code_num);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_append_service_cb(command_handle: {}, rc: {})",
Expand Down Expand Up @@ -292,7 +298,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_service_did(
);

execute(move || {
match out_of_band::append_service_did(handle, &did) {
match out_of_band::append_service_did(handle, did) {
Ok(()) => {
trace!(
"vcx_out_of_band_sender_append_service_did_cb(command_handle: {}, rc: {})",
Expand All @@ -302,6 +308,7 @@ pub extern "C" fn vcx_out_of_band_sender_append_service_did(
cb(command_handle, error::SUCCESS.code_num);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_append_service_did_cb(command_handle: {}, rc: {})",
Expand Down Expand Up @@ -345,6 +352,7 @@ pub extern "C" fn vcx_out_of_band_receiver_extract_message(
cb(command_handle, error::SUCCESS.code_num, msg.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_extract_message_cb(command_handle: {}, rc: {}, msg: {})",
Expand Down Expand Up @@ -388,6 +396,7 @@ pub extern "C" fn vcx_out_of_band_to_message(
cb(command_handle, error::SUCCESS.code_num, msg.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_to_message_cb(command_handle: {}, rc: {}, msg: {})",
Expand Down Expand Up @@ -434,12 +443,14 @@ pub extern "C" fn vcx_out_of_band_receiver_connection_exists(
};

execute_async::<BoxFuture<'static, Result<(), ()>>>(Box::pin(async move {
match out_of_band::connection_exists(handle, &conn_handles).await {
Ok((conn_handle, found_one)) => {
match out_of_band::connection_exists(handle, conn_handles).await {
Ok(conn_handle) => {
let found_one = conn_handle == 0;
trace!("vcx_out_of_band_receiver_connection_exists_cb(command_handle: {}, rc: {}, conn_handle: {}, found_one: {})", command_handle, error::SUCCESS.message, conn_handle, found_one);
cb(command_handle, error::SUCCESS.code_num, conn_handle, found_one);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!("vcx_out_of_band_receiver_connection_exists_cb(command_handle: {}, rc: {}, conn_handle: {}, found_one: {})", command_handle, err, 0, false);
cb(command_handle, err.into(), 0, false);
Expand Down Expand Up @@ -480,6 +491,7 @@ pub extern "C" fn vcx_out_of_band_receiver_build_connection(
cb(command_handle, error::SUCCESS.code_num, connection.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_build_connection_cb(command_handle: {}, rc: {}, connection: {})",
Expand Down Expand Up @@ -523,6 +535,7 @@ pub extern "C" fn vcx_out_of_band_sender_serialize(
cb(command_handle, error::SUCCESS.code_num, oob_json.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_serialize_cb(command_handle: {}, rc: {}, oob_json: {})",
Expand Down Expand Up @@ -566,6 +579,7 @@ pub extern "C" fn vcx_out_of_band_receiver_serialize(
cb(command_handle, error::SUCCESS.code_num, oob_json.as_ptr());
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_serialize_cb(command_handle: {}, rc: {}, oob_json: {})",
Expand Down Expand Up @@ -598,7 +612,7 @@ pub extern "C" fn vcx_out_of_band_sender_deserialize(
);

execute(move || {
match out_of_band::from_string_sender(&oob_json) {
match out_of_band::from_string_sender(oob_json) {
Ok(handle) => {
trace!(
"vcx_out_of_band_sender_deserialize_cb(command_handle: {}, rc: {}, handle: {})",
Expand All @@ -609,6 +623,7 @@ pub extern "C" fn vcx_out_of_band_sender_deserialize(
cb(command_handle, error::SUCCESS.code_num, handle);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_sender_deserialize_cb(command_handle: {}, rc: {}, handle: {})",
Expand Down Expand Up @@ -641,7 +656,7 @@ pub extern "C" fn vcx_out_of_band_receiver_deserialize(
);

execute(move || {
match out_of_band::from_string_receiver(&oob_json) {
match out_of_band::from_string_receiver(oob_json) {
Ok(handle) => {
trace!(
"vcx_out_of_band_receiver_deserialize_cb(command_handle: {}, rc: {}, handle: {})",
Expand All @@ -652,6 +667,7 @@ pub extern "C" fn vcx_out_of_band_receiver_deserialize(
cb(command_handle, error::SUCCESS.code_num, handle);
}
Err(err) => {
let err: VcxError = err.into();
set_current_error_vcx(&err);
error!(
"vcx_out_of_band_receiver_deserialize_cb(command_handle: {}, rc: {}, handle: {})",
Expand Down Expand Up @@ -680,6 +696,7 @@ pub extern "C" fn vcx_out_of_band_sender_release(handle: u32) -> u32 {
error::SUCCESS.code_num
}
Err(err) => {
let err: VcxError = err.into();
error!("vcx_out_of_band_sender_release(handle: {}), rc: {})", handle, err);
err.into()
}
Expand All @@ -700,6 +717,7 @@ pub extern "C" fn vcx_out_of_band_receiver_release(handle: u32) -> u32 {
error::SUCCESS.code_num
}
Err(err) => {
let err: VcxError = err.into();
error!("vcx_out_of_band_receiver_release(handle: {}), rc: {})", handle, err);
err.into()
}
Expand Down
Loading

0 comments on commit 605ae40

Please sign in to comment.