@@ -5,6 +5,7 @@ extern crate lazy_static;
55
66mod types;
77
8+ use crate :: types:: libsql_config;
89use tokio:: runtime:: Runtime ;
910use types:: {
1011 blob, libsql_connection, libsql_connection_t, libsql_database, libsql_database_t, libsql_row,
@@ -54,16 +55,16 @@ pub unsafe extern "C" fn libsql_open_sync(
5455 out_db : * mut libsql_database_t ,
5556 out_err_msg : * mut * const std:: ffi:: c_char ,
5657) -> std:: ffi:: c_int {
57- libsql_open_sync_internal (
58+ let config = libsql_config {
5859 db_path,
5960 primary_url,
6061 auth_token,
6162 read_your_writes,
6263 encryption_key,
63- false ,
64- out_db ,
65- out_err_msg ,
66- )
64+ sync_interval : 0 ,
65+ with_webpki : 0 ,
66+ } ;
67+ libsql_open_sync_with_config ( config , out_db , out_err_msg )
6768}
6869
6970#[ no_mangle]
@@ -76,45 +77,41 @@ pub unsafe extern "C" fn libsql_open_sync_with_webpki(
7677 out_db : * mut libsql_database_t ,
7778 out_err_msg : * mut * const std:: ffi:: c_char ,
7879) -> std:: ffi:: c_int {
79- libsql_open_sync_internal (
80+ let config = libsql_config {
8081 db_path,
8182 primary_url,
8283 auth_token,
8384 read_your_writes,
8485 encryption_key,
85- true ,
86- out_db ,
87- out_err_msg ,
88- )
86+ sync_interval : 0 ,
87+ with_webpki : 1 ,
88+ } ;
89+ libsql_open_sync_with_config ( config , out_db , out_err_msg )
8990}
9091
91- unsafe fn libsql_open_sync_internal (
92- db_path : * const std:: ffi:: c_char ,
93- primary_url : * const std:: ffi:: c_char ,
94- auth_token : * const std:: ffi:: c_char ,
95- read_your_writes : std:: ffi:: c_char ,
96- encryption_key : * const std:: ffi:: c_char ,
97- with_webpki : bool ,
92+ #[ no_mangle]
93+ pub unsafe extern "C" fn libsql_open_sync_with_config (
94+ config : libsql_config ,
9895 out_db : * mut libsql_database_t ,
9996 out_err_msg : * mut * const std:: ffi:: c_char ,
10097) -> std:: ffi:: c_int {
101- let db_path = unsafe { std:: ffi:: CStr :: from_ptr ( db_path) } ;
98+ let db_path = unsafe { std:: ffi:: CStr :: from_ptr ( config . db_path ) } ;
10299 let db_path = match db_path. to_str ( ) {
103100 Ok ( url) => url,
104101 Err ( e) => {
105102 set_err_msg ( format ! ( "Wrong URL: {e}" ) , out_err_msg) ;
106103 return 1 ;
107104 }
108105 } ;
109- let primary_url = unsafe { std:: ffi:: CStr :: from_ptr ( primary_url) } ;
106+ let primary_url = unsafe { std:: ffi:: CStr :: from_ptr ( config . primary_url ) } ;
110107 let primary_url = match primary_url. to_str ( ) {
111108 Ok ( url) => url,
112109 Err ( e) => {
113110 set_err_msg ( format ! ( "Wrong URL: {e}" ) , out_err_msg) ;
114111 return 2 ;
115112 }
116113 } ;
117- let auth_token = unsafe { std:: ffi:: CStr :: from_ptr ( auth_token) } ;
114+ let auth_token = unsafe { std:: ffi:: CStr :: from_ptr ( config . auth_token ) } ;
118115 let auth_token = match auth_token. to_str ( ) {
119116 Ok ( token) => token,
120117 Err ( e) => {
@@ -127,29 +124,37 @@ unsafe fn libsql_open_sync_internal(
127124 primary_url. to_string ( ) ,
128125 auth_token. to_string ( ) ,
129126 ) ;
130- if with_webpki {
127+ if config . with_webpki != 0 {
131128 let https = hyper_rustls:: HttpsConnectorBuilder :: new ( )
132129 . with_webpki_roots ( )
133130 . https_or_http ( )
134131 . enable_http1 ( )
135132 . build ( ) ;
136133 builder = builder. connector ( https) ;
137134 }
138- let builder = builder. read_your_writes ( read_your_writes != 0 ) ;
139- let builder = if encryption_key. is_null ( ) {
140- builder
141- } else {
142- let key = unsafe { std:: ffi:: CStr :: from_ptr ( encryption_key) } ;
135+ if config. sync_interval > 0 {
136+ let interval = match config. sync_interval . try_into ( ) {
137+ Ok ( d) => d,
138+ Err ( e) => {
139+ set_err_msg ( format ! ( "Wrong periodic sync interval: {e}" ) , out_err_msg) ;
140+ return 4 ;
141+ }
142+ } ;
143+ builder = builder. sync_interval ( std:: time:: Duration :: from_secs ( interval) ) ;
144+ }
145+ builder = builder. read_your_writes ( config. read_your_writes != 0 ) ;
146+ if !config. encryption_key . is_null ( ) {
147+ let key = unsafe { std:: ffi:: CStr :: from_ptr ( config. encryption_key ) } ;
143148 let key = match key. to_str ( ) {
144149 Ok ( k) => k,
145150 Err ( e) => {
146151 set_err_msg ( format ! ( "Wrong encryption key: {e}" ) , out_err_msg) ;
147- return 4 ;
152+ return 5 ;
148153 }
149154 } ;
150155 let key = bytes:: Bytes :: copy_from_slice ( key. as_bytes ( ) ) ;
151156 let config = libsql:: EncryptionConfig :: new ( libsql:: Cipher :: Aes256Cbc , key) ;
152- builder. encryption_config ( config)
157+ builder = builder . encryption_config ( config)
153158 } ;
154159 match RT . block_on ( builder. build ( ) ) {
155160 Ok ( db) => {
@@ -162,7 +167,7 @@ unsafe fn libsql_open_sync_internal(
162167 format ! ( "Error opening db path {db_path}, primary url {primary_url}: {e}" ) ,
163168 out_err_msg,
164169 ) ;
165- 5
170+ 6
166171 }
167172 }
168173}
0 commit comments