mirror of
https://github.com/Tribufu/TribufuRust
synced 2026-08-08 04:11:07 +00:00
Merge Rust, C and C++ SDKs (#4)
* Generate native bindings from rust crate * Add native instance and runtime statics * Update README.md
This commit is contained in:
parent
738f9c947b
commit
a392dbb119
39 changed files with 1213 additions and 24 deletions
21
src/native/Cargo.toml
Normal file
21
src/native/Cargo.toml
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
[package]
|
||||
name = "tribufu-sdk"
|
||||
version = "0.0.0"
|
||||
description = "Tribufu Native"
|
||||
repository = "https://github.com/tribufu/tribufu-rust"
|
||||
authors = ["Tribufu <contact@tribufu.com>"]
|
||||
license = "UNLICENSED"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "tribufu_sdk"
|
||||
crate-type = ["rlib", "cdylib", "staticlib"]
|
||||
path = "lib.rs"
|
||||
|
||||
[dependencies]
|
||||
tribufu = { path = "../../" }
|
||||
futures = "0.3.31"
|
||||
libc = "0.2.172"
|
||||
once_cell = "1.19.0"
|
||||
tokio = { version = "1.45.1", features = ["full"] }
|
||||
88
src/native/api.rs
Normal file
88
src/native/api.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
use crate::models::{TribufuApiCallbackContext, TribufuApiGetUserInfoCallback};
|
||||
use futures::lock::Mutex;
|
||||
use libc::{c_char, c_void};
|
||||
use once_cell::sync::Lazy;
|
||||
use std::ffi::CString;
|
||||
use std::ptr;
|
||||
use tokio::runtime::Runtime;
|
||||
use tribufu::apis::tribufu_generated_api::TribufuGeneratedApiClient;
|
||||
use tribufu::TribufuApi;
|
||||
|
||||
static INSTANCE: Lazy<Mutex<Option<TribufuGeneratedApiClient>>> = Lazy::new(|| Mutex::new(None));
|
||||
static RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().unwrap());
|
||||
|
||||
/// Gets the version of the Tribufu API.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tribufu_api_get_version() -> *const c_char {
|
||||
CString::new(TribufuApi::get_version()).unwrap().into_raw()
|
||||
}
|
||||
|
||||
/// Gets the user agent string for the Tribufu API.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tribufu_api_get_user_agent() -> *const c_char {
|
||||
CString::new(TribufuApi::get_user_agent())
|
||||
.unwrap()
|
||||
.into_raw()
|
||||
}
|
||||
|
||||
/// Initialize the Tribufu API instance.
|
||||
///
|
||||
/// This must be called before any other API functions.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tribufu_api_initialize() -> bool {
|
||||
let api = TribufuApi::from_env_or_default(None);
|
||||
|
||||
if INSTANCE.try_lock().is_none() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut instance = INSTANCE.try_lock().unwrap();
|
||||
*instance = Some(api);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Shutdown the Tribufu API instance.
|
||||
///
|
||||
/// This must be called when the API is no longer needed.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tribufu_api_shutdown() {
|
||||
if INSTANCE.try_lock().is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut instance = INSTANCE.try_lock().unwrap();
|
||||
*instance = None;
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(unused_variables)]
|
||||
pub extern "C" fn tribufu_api_get_user_info(
|
||||
context: *mut c_void,
|
||||
callback: TribufuApiGetUserInfoCallback,
|
||||
) {
|
||||
let context = TribufuApiCallbackContext(context);
|
||||
|
||||
RUNTIME.spawn(async move {
|
||||
let mut instance = INSTANCE.lock().await;
|
||||
if instance.is_none() {
|
||||
callback(context.as_ptr(), ptr::null());
|
||||
return;
|
||||
}
|
||||
|
||||
let api = instance.as_mut();
|
||||
if api.is_none() {
|
||||
callback(context.as_ptr(), ptr::null());
|
||||
return;
|
||||
}
|
||||
|
||||
let api = api.unwrap();
|
||||
|
||||
// TODO: Implement logic here
|
||||
|
||||
callback(context.as_ptr(), ptr::null());
|
||||
});
|
||||
}
|
||||
8
src/native/lib.rs
Normal file
8
src/native/lib.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod api;
|
||||
pub mod mem;
|
||||
pub mod models;
|
||||
16
src/native/mem.rs
Normal file
16
src/native/mem.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::c_char;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tribufu_free_string(ptr: *mut c_char) {
|
||||
if ptr.is_null() {
|
||||
return;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let _ = CString::from_raw(ptr);
|
||||
}
|
||||
}
|
||||
19
src/native/models.rs
Normal file
19
src/native/models.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use libc::c_void;
|
||||
|
||||
pub struct TribufuApiCallbackContext(pub(crate) *mut c_void);
|
||||
|
||||
impl TribufuApiCallbackContext {
|
||||
pub(crate) fn as_ptr(&self) -> *mut c_void {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for TribufuApiCallbackContext {}
|
||||
unsafe impl Sync for TribufuApiCallbackContext {}
|
||||
|
||||
pub type TribufuApiGetUserInfoCallbackData = c_void;
|
||||
pub type TribufuApiGetUserInfoCallback =
|
||||
extern "C" fn(*mut c_void, *const TribufuApiGetUserInfoCallbackData);
|
||||
Loading…
Add table
Add a link
Reference in a new issue