From 9e17c9d06d7e280deb08f83e2cab5ddd9088fc8d Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Mon, 2 Sep 2024 15:11:24 +0200 Subject: [PATCH] created rust fn why2_toml_equals check if key is equal to x --- src/chat/config/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/chat/config/src/lib.rs b/src/chat/config/src/lib.rs index 1e79ac5..cb116f9 100644 --- a/src/chat/config/src/lib.rs +++ b/src/chat/config/src/lib.rs @@ -157,6 +157,39 @@ pub extern "C" fn why2_toml_contains(path: *const c_char, key: *const c_char) -> data.get(&key_r).is_some() } +#[no_mangle] +pub extern "C" fn why2_toml_equals(path: *const c_char, key: *const c_char, value: *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() }; + let value_r = unsafe { CStr::from_ptr(value).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).unwrap().to_string() == value_r +} + #[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 {