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

clib example #40

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build wasm
run: cargo build -F wasm
run: make build-wasm
- name: Build tokio
run: cargo build -F tokio
run: make build-tokio
- name: Run tests
run: cargo test -F tokio
run: |
make test-tokio
make test-capi
env:
NAVABILITY_USERLABEL: "[email protected]"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/test/build
64 changes: 43 additions & 21 deletions Cargo.lock

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

15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
serde = "^1.0.216"
serde_json = "^1.0.133"
serde = "^1.0.217"
serde_json = "^1.0.135"
chrono = "^0.4.39"
log = "^0.4"
uuid = { version ="^1.11", features = ["v4","v5"] }
log = "^0.4.25"
uuid = { version ="^1.11.1", features = ["v4","v5"] }
tracing = "^0.1.41"
graphql_client = "^0.14"
reqwest = {version = "^0.12.9", optional=true, features = [
reqwest = {version = "^0.12.12", optional=true, features = [
"json",
# "multipart",
] }
Expand All @@ -24,15 +24,16 @@ reqwest = {version = "^0.12.9", optional=true, features = [


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "^1.40", default-features = false, optional=true}
tokio = { version = "^1.43", default-features = false, optional=true}
# libc = { version = "^0.2.85", default-features = false, optional=true}


[target.'cfg(target_arch = "wasm32")'.dependencies]
gloo-console = { version = "^0.3", optional = true }


[features]
tokio = ["dep:tokio", "dep:reqwest"]
tokio = ["dep:tokio", "dep:reqwest"] #, "dep:libc"]
blocking = ["graphql_client/reqwest","graphql_client/reqwest-blocking"]
wasm = ["dep:reqwest"]
wasm-dev = ["dep:gloo-console"]
Expand Down
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

clean:
cargo clean

build-wasm:
cargo build -F wasm

build-tokio:
cargo build -F tokio

build-lib: build-tokio

test-tokio:
cargo test -F tokio

test-capi: build-lib
cd test && $(MAKE)
26 changes: 26 additions & 0 deletions include/NavAbilitySDK.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef NAVABILITYSDK
#define NAVABILITYSDK

// File: NavAbilitySDK.h

// Lets use some types which we can easily pair with rust types.
#include <stdint.h>


// Opaque type for some Struct
typedef struct _nvaclient NavAbilityClient;


NavAbilityClient* NvaClient(void);
void free_NvaClient(NavAbilityClient *n);

char* get_apiurl(const NavAbilityClient *n);


// typedef struct _custom_error TestType;
// // Free function which takes a pointer to a pointer for freeing
// // the memory, returns error code based on ERRNO.
// int32_t error_free_with_result(TestType **o);


#endif
84 changes: 84 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,90 @@ fn to_console_error(



use std::ffi::{
CString,
CStr
};
use std::os::raw::c_char;


pub unsafe fn convert_str(input: &str) -> *mut c_char {
let c_str = CString::new(input).unwrap().into_raw();
return c_str;
}

pub unsafe fn cstr_to_str(c_buf: *const i8) -> &'static str {
let cstr = CStr::from_ptr(c_buf);
return cstr.to_str().expect("success");
}


// ref. https://doc.rust-lang.org/std/boxed/
#[no_mangle]
pub unsafe extern "C" fn NvaClient(
) -> Box<NavAbilityClient> {
return Box::new(NavAbilityClient::new(
&"https://api.navability.io/graphql".to_owned(),
&"".to_owned(),
&"".to_owned(),
))
}

impl Drop for NavAbilityClient {
fn drop(&mut self) {
println!("Dropping NavAbilityClient.");
}
}

// Take ownership via passing by value, i.e. runs drop on fn exit. Option for null case.
#[no_mangle]
pub extern "C" fn free_NvaClient(
_: Option<Box<NavAbilityClient>>
) {}

#[no_mangle]
pub unsafe extern "C" fn get_apiurl(n: &NavAbilityClient) -> *mut c_char {
return convert_str(&n.apiurl)
}

#[no_mangle]
pub unsafe extern "C" fn drop_cstr2(pointer: *mut c_char) -> () {
drop(CString::from_raw(pointer));
}


// pub struct TestType {
// code: isize,
// }
// #[no_mangle]
// pub unsafe extern "C" fn error_create_with_result(_e: *mut *mut TestType) -> isize {
// let e = Box::new(example_error());
// *_e = Box::into_raw(e);
// 0
// }
// #[no_mangle]
// pub unsafe extern "C" fn error_free_with_result(e: *mut *mut TestType) -> i32 {
// if e.is_null() || (*e).is_null() {
// return libc::EINVAL;
// }
// // Reconstruct the Error into a box and then drop it so that it's freed.
// drop(Box::from_raw(*e));
// *e = 0 as *mut TestType;
// 0
// }
// // Our example "getter" methods which work on the Error type. The value
// // returned is only valid as long as the Error has not been freed. If C
// // caller needs a longer lifetime they need to copy the value.
// #[no_mangle]
// pub extern "C" fn error_code_get(e: &TestType) -> isize {
// e.code
// }
// use libc;








Expand Down
11 changes: 11 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

.DEFAULT_GOAL := run-testc

build-testc: test.c
mkdir -p build
gcc -Wall -g -O0 test.c -o build/a.out -I../include/ -L../target/debug/ -lnavabilitysdk
.PHONY: build-testc

run-testc: build-testc
LD_LIBRARY_PATH=../target/debug ./build/a.out
.PHONY: run-testc
Loading
Loading