From 603641438b988679f9b0ecaff4da9b26bbc7b223 Mon Sep 17 00:00:00 2001 From: Matthias Schwarz Date: Thu, 6 Feb 2020 22:41:28 +0100 Subject: [PATCH] Add support for non defined value in ConfigEntry --- src/config.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/config.rs b/src/config.rs index cc190899eb..ddda1d4fe2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -449,15 +449,30 @@ impl<'cfg> ConfigEntry<'cfg> { /// Gets the value of this entry. /// /// May return `None` if the value is not valid utf-8 + /// + /// # Panics + /// + /// Panics when no value is defined. pub fn value(&self) -> Option<&str> { str::from_utf8(self.value_bytes()).ok() } /// Gets the value of this entry as a byte slice. + /// + /// # Panics + /// + /// Panics when no value is defined. pub fn value_bytes(&self) -> &[u8] { unsafe { crate::opt_bytes(self, (*self.raw).value).unwrap() } } + /// Returns `true` when a value is defined otherwise `false`. + /// + /// No value defined is a short-hand to represent a Boolean `true`. + pub fn has_value(&self) -> bool { + unsafe { !(*self.raw).value.is_null() } + } + /// Gets the configuration level of this entry. pub fn level(&self) -> ConfigLevel { unsafe { ConfigLevel::from_raw((*self.raw).level) }