created rust fn why2_toml_equals

check if key is equal to x
This commit is contained in:
Václav Šmejkal 2024-09-02 15:11:24 +02:00
parent 42562013b5
commit 9e17c9d06d
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -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() 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] #[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 pub extern "C" fn why2_toml_read_free(s: *mut c_char) //BECAUSE THIS IS RUST MODULE I HAVE TO CREATE A DEALLOCATING FUNCTION
{ {