From 446727b87cbd95051935fbd9815a0ba7800b6dd7 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sat, 31 Aug 2024 12:44:15 +0200 Subject: [PATCH] created rust fn for checking if toml contains key --- src/chat/config/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/chat/config/src/lib.rs b/src/chat/config/src/lib.rs index 69e9ad3..1e79ac5 100644 --- a/src/chat/config/src/lib.rs +++ b/src/chat/config/src/lib.rs @@ -125,6 +125,38 @@ pub extern "C" fn why2_toml_write(path: *const c_char, key: *const c_char, value } } +#[no_mangle] +pub extern "C" fn why2_toml_contains(path: *const c_char, key: *const c_char) -> bool +{ + //CONVERT C STRINGS TO RUST STRINGS + let path_r = unsafe { CStr::from_ptr(path).to_string_lossy().into_owned() }; + let key_r = unsafe { CStr::from_ptr(key).to_string_lossy().into_owned() }; + + //GET FILE CONTENT + let file_raw = match read_to_string(&path_r) + { + Ok(raw) => raw, + Err(e) => + { + eprintln!("Could not read TOML config: {}\n{}", path_r, e); + return false; + }, + }; + + //PARSE FILE + let data: Value = match toml::from_str(&file_raw) + { + Ok(data) => data, + Err(e) => + { + eprintln!("Could not parse TOML config: {}\n{}", path_r, e); + return false; + }, + }; + + data.get(&key_r).is_some() +} + #[no_mangle] pub extern "C" fn why2_toml_read_free(s: *mut c_char) //BECAUSE THIS IS RUST MODULE I HAVE TO CREATE A DEALLOCATING FUNCTION {