swift interop

main
Ziyang Hu 2 years ago
parent 7819436522
commit 8bd44aca33

@ -34,13 +34,14 @@
#![allow(clippy::too_many_arguments)]
use itertools::Itertools;
use miette::{bail, IntoDiagnostic, miette, Result};
pub use miette::Error;
use miette::{bail, miette, IntoDiagnostic, Result};
use serde_json::{json, Map};
pub use runtime::db::Db;
pub use runtime::relation::decode_tuple_from_kv;
pub use storage::mem::{new_cozo_mem, MemStorage};
pub use storage::{Storage, StoreTx};
pub use storage::mem::{MemStorage, new_cozo_mem};
#[cfg(feature = "storage-rocksdb")]
pub use storage::rocks::{new_cozo_rocksdb, RocksDbStorage};
#[cfg(feature = "storage-sled")]
@ -49,7 +50,6 @@ pub use storage::sled::{new_cozo_sled, SledStorage};
pub use storage::sqlite::{new_cozo_sqlite, SqliteStorage};
#[cfg(feature = "storage-tikv")]
pub use storage::tikv::{new_cozo_tikv, TiKvStorage};
pub use storage::{Storage, StoreTx};
use crate::data::json::JsonValue;
use crate::runtime::db::{JSON_ERR_HANDLER, TEXT_ERR_HANDLER};
@ -130,6 +130,11 @@ impl DbInstance {
),
})
}
/// Same as [Self::new], but inputs and error messages are all in strings
pub fn new_with_str(kind: &str, path: &str, options: &str) -> std::result::Result<Self, String> {
let options: JsonValue = serde_json::from_str(options).map_err(|e| e.to_string())?;
Self::new(kind, path, options).map_err(|err| err.to_string())
}
/// Dispatcher method. See [crate::Db::run_script].
pub fn run_script(&self, payload: &str, params: &Map<String, JsonValue>) -> Result<JsonValue> {
match self {

@ -54,9 +54,9 @@ pub unsafe extern "C" fn cozo_open_db(
Err(err) => return CString::new(format!("{}", err)).unwrap().into_raw(),
};
let db = match DbInstance::new(engine, path, Default::default()) {
let db = match DbInstance::new_with_str(engine, path, "{}") {
Ok(db) => db,
Err(err) => return CString::new(err.to_string()).unwrap().into_raw(),
Err(err) => return CString::new(err).unwrap().into_raw(),
};
let id = HANDLES.current.fetch_add(1, Ordering::AcqRel);

@ -1,10 +1,28 @@
use cozo::*;
#[swift_bridge::bridge]
mod ffi {
extern "Rust" {
fn hello_rust(name: &str) -> String;
type DbInstance;
fn new_db(kind: &str, path: &str, options: &str) -> Option<DbInstance>;
#[swift_bridge(associated_to = DbInstance)]
fn run_script_str(&self, payload: &str, params: &str) -> String;
fn export_relations_str(&self, relations_str: &str) -> String;
fn import_relation_str(&self, data: &str) -> String;
fn backup_db_str(&self, out_file: &str) -> String;
fn restore_backup_str(&self, in_file: &str) -> String;
}
}
fn hello_rust(name: &str) -> String {
String::from(format!("Hello {} from Rust!", name))
}
fn new_db(kind: &str, path: &str, options: &str) -> Option<DbInstance> {
let options = if options.is_empty() { "{}" } else { options };
match DbInstance::new_with_str(kind, path, options) {
Ok(db) => Some(db),
Err(err) => {
eprintln!("{}", err);
None
}
}
}

Loading…
Cancel
Save