From 8bd44aca33efce06b26a5e0f6a8d4371dc59ca07 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sat, 19 Nov 2022 17:38:37 +0800 Subject: [PATCH] swift interop --- cozo-core/src/lib.rs | 11 ++++++++--- cozo-lib-c/src/lib.rs | 4 ++-- cozo-lib-swift/src/lib.rs | 26 ++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cozo-core/src/lib.rs b/cozo-core/src/lib.rs index 921f155a..fc1fdc89 100644 --- a/cozo-core/src/lib.rs +++ b/cozo-core/src/lib.rs @@ -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 { + 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) -> Result { match self { diff --git a/cozo-lib-c/src/lib.rs b/cozo-lib-c/src/lib.rs index 742fdf50..6cc855fe 100644 --- a/cozo-lib-c/src/lib.rs +++ b/cozo-lib-c/src/lib.rs @@ -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); diff --git a/cozo-lib-swift/src/lib.rs b/cozo-lib-swift/src/lib.rs index 0ad74fcb..ef3bfae7 100644 --- a/cozo-lib-swift/src/lib.rs +++ b/cozo-lib-swift/src/lib.rs @@ -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; + + #[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)) -} \ No newline at end of file +fn new_db(kind: &str, path: &str, options: &str) -> Option { + 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 + } + } +}