From 59a33a2f35b409655c95c5046c80b95e9c3532bb Mon Sep 17 00:00:00 2001 From: Sho Nakatani Date: Tue, 29 Mar 2022 11:01:25 +0900 Subject: [PATCH] feat: add spring_config_toml --- Cargo.toml | 4 ++-- c_example/example.c | 4 +++- springql.h | 15 +++++++++++++++ src/lib.rs | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1d109a..a4a90f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "springql-client-c" -version = "0.3.0" +version = "0.3.1" authors = ["Sho Nakatani "] license = "MIT OR Apache-2.0" @@ -15,6 +15,6 @@ name = "springql_client" cbindgen = "0.21" [dependencies] -springql-core = "0.3" +springql-core = "0.3.1" log = "0.4" diff --git a/c_example/example.c b/c_example/example.c index 86bca72..97033d7 100644 --- a/c_example/example.c +++ b/c_example/example.c @@ -106,7 +106,9 @@ int main() { SpringErrno ret; - SpringConfig *config = spring_config_default(); + SpringConfig *config = spring_config_toml( + "[memory]\n" + "upper_limit_bytes = 1_000_000\n"); assert_not_null(config); SpringPipeline *pipeline = spring_open(config); diff --git a/springql.h b/springql.h index 94d6e67..e1ef44f 100644 --- a/springql.h +++ b/springql.h @@ -44,9 +44,24 @@ typedef void *SpringRow; /** * See: springql_core::api::spring_config_default + * + * Returned value is not modifiable (it is just a void pointer). + * If you would like to change the default configuration, use `spring_config_toml()` instead. */ SpringConfig *spring_config_default(void); +/** + * See: springql_core::api::spring_config_default + * + * Returned value is not modifiable (it is just a void pointer). + * If you would like to change the default configuration, use `spring_config_toml()` instead. + * + * # Safety + * + * This function is unsafe because it uses raw pointer. + */ +SpringConfig *spring_config_toml(const char *overwrite_config_toml); + /** * # Returns * diff --git a/src/lib.rs b/src/lib.rs index 90fc0cb..180bcf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,9 @@ pub struct SpringPipeline(*mut c_void); pub struct SpringRow(*mut c_void); /// See: springql_core::api::spring_config_default +/// +/// Returned value is not modifiable (it is just a void pointer). +/// If you would like to change the default configuration, use `spring_config_toml()` instead. #[no_mangle] pub extern "C" fn spring_config_default() -> *mut SpringConfig { let config = springql_core::spring_config_default(); @@ -44,6 +47,25 @@ pub extern "C" fn spring_config_default() -> *mut SpringConfig { }))) } +/// See: springql_core::api::spring_config_default +/// +/// Returned value is not modifiable (it is just a void pointer). +/// If you would like to change the default configuration, use `spring_config_toml()` instead. +/// +/// # Safety +/// +/// This function is unsafe because it uses raw pointer. +#[no_mangle] +pub unsafe extern "C" fn spring_config_toml( + overwrite_config_toml: *const c_char, +) -> *mut SpringConfig { + let s = { CStr::from_ptr(overwrite_config_toml) }; + let s = s.to_str().expect("failed to parse TOML string into UTF-8"); + + let config = springql_core::spring_config_toml(s).expect("failed to parse TOML config"); + Box::into_raw(Box::new(SpringConfig(mem::transmute(Box::new(config))))) +} + /// # Returns /// /// - `0`: if there are no recent errors.