From 21da3f95b00431f4d10d8b5e67ee402bffde129c Mon Sep 17 00:00:00 2001 From: KyleMiles Date: Tue, 11 May 2021 22:51:38 -0400 Subject: Rust API; More setting support, fix edgecase in open_view_with_options, and fixed using mut for const correctness (mut != ~const) --- rust/src/settings.rs | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 216 insertions(+), 2 deletions(-) (limited to 'rust/src/settings.rs') diff --git a/rust/src/settings.rs b/rust/src/settings.rs index a555cb4c..86904c38 100644 --- a/rust/src/settings.rs +++ b/rust/src/settings.rs @@ -47,7 +47,7 @@ impl Settings { } } - pub fn set_resource_id(&mut self, resource_id: S) { + pub fn set_resource_id(&self, resource_id: S) { let resource_id = resource_id.as_bytes_with_nul(); unsafe { BNSettingsSetResourceId(self.handle, resource_id.as_ref().as_ptr() as *mut _) }; } @@ -74,8 +74,222 @@ impl Settings { unsafe { BNSettingsContains(self.handle, key.as_ref().as_ptr() as *mut _) } } + pub fn get_bool( + &self, + key: S, + view: Option, + scope: Option>, + ) -> bool { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope_ptr = match scope { + Some(mut scope) => scope.as_mut(), + _ => ptr::null_mut() as *mut _, + }; + unsafe { + BNSettingsGetBool( + self.handle, + key.as_ref().as_ptr() as *mut _, + view_handle, + scope_ptr, + ) + } + } + + pub fn get_double( + &self, + key: S, + view: Option, + scope: Option>, + ) -> f64 { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope_ptr = match scope { + Some(mut scope) => scope.as_mut(), + _ => ptr::null_mut() as *mut _, + }; + unsafe { + BNSettingsGetDouble( + self.handle, + key.as_ref().as_ptr() as *mut _, + view_handle, + scope_ptr, + ) + } + } + + pub fn get_integer( + &self, + key: S, + view: Option, + scope: Option>, + ) -> u64 { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope_ptr = match scope { + Some(mut scope) => scope.as_mut(), + _ => ptr::null_mut() as *mut _, + }; + unsafe { + BNSettingsGetUInt64( + self.handle, + key.as_ref().as_ptr() as *mut _, + view_handle, + scope_ptr, + ) + } + } + + pub fn get_string( + &self, + key: S, + view: Option, + scope: Option>, + ) -> BnString { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope_ptr = match scope { + Some(mut scope) => scope.as_mut(), + _ => ptr::null_mut() as *mut _, + }; + unsafe { + BnString::from_raw(BNSettingsGetString( + self.handle, + key.as_ref().as_ptr() as *mut _, + view_handle, + scope_ptr, + )) + } + } + + // TODO : get_string_list + // TODO : get_json + + pub fn set_bool( + &self, + key: S, + value: bool, + view: Option, + scope: Option, + ) { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope = match scope { + Some(scope) => scope, + _ => SettingsScope::SettingsAutoScope, + }; + unsafe { + BNSettingsSetBool( + self.handle, + view_handle, + scope, + key.as_ref().as_ptr() as *mut _, + value, + ); + } + } + + pub fn set_double( + &self, + key: S, + value: f64, + view: Option, + scope: Option, + ) { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope = match scope { + Some(scope) => scope, + _ => SettingsScope::SettingsAutoScope, + }; + unsafe { + BNSettingsSetDouble( + self.handle, + view_handle, + scope, + key.as_ref().as_ptr() as *mut _, + value, + ); + } + } + + pub fn set_integer( + &self, + key: S, + value: u64, + view: Option, + scope: Option, + ) { + let key = key.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope = match scope { + Some(scope) => scope, + _ => SettingsScope::SettingsAutoScope, + }; + unsafe { + BNSettingsSetUInt64( + self.handle, + view_handle, + scope, + key.as_ref().as_ptr() as *mut _, + value, + ); + } + } + + pub fn set_string( + &self, + key: S, + value: S, + view: Option, + scope: Option, + ) { + let key = key.as_bytes_with_nul(); + let value = value.as_bytes_with_nul(); + let view_handle = match view { + Some(view) => view.handle, + _ => ptr::null_mut() as *mut _, + }; + let scope = match scope { + Some(scope) => scope, + _ => SettingsScope::SettingsAutoScope, + }; + unsafe { + BNSettingsSetString( + self.handle, + view_handle, + scope, + key.as_ref().as_ptr() as *mut _, + value.as_ref().as_ptr() as *mut _, + ); + } + } + + // TODO : set_string_list + pub fn set_json( - &mut self, + &self, key: S1, value: S2, view: Option<&BinaryView>, -- cgit v1.3.1