refactor: get_config_bool_opt(): Return None if only default value exists

And also:
- Make it `pub(crate)`.
- Use it in `should_request_mdns()` as using `config_exists()` there isn't correct because the
  latter doesn't look at environment.
This commit is contained in:
iequidoo
2024-09-09 23:52:15 -03:00
committed by iequidoo
parent 52dcc7e350
commit 2c136f6355

View File

@@ -446,8 +446,8 @@ impl Context {
Ok(self.sql.get_raw_config(key.as_ref()).await?.is_some()) Ok(self.sql.get_raw_config(key.as_ref()).await?.is_some())
} }
/// Get a configuration key. Returns `None` if no value is set, and no default value found. /// Get a config key value. Returns `None` if no value is set.
pub async fn get_config(&self, key: Config) -> Result<Option<String>> { pub(crate) async fn get_config_opt(&self, key: Config) -> Result<Option<String>> {
let env_key = format!("DELTACHAT_{}", key.as_ref().to_uppercase()); let env_key = format!("DELTACHAT_{}", key.as_ref().to_uppercase());
if let Ok(value) = env::var(env_key) { if let Ok(value) = env::var(env_key) {
return Ok(Some(value)); return Ok(Some(value));
@@ -467,7 +467,12 @@ impl Context {
Config::SysConfigKeys => Some(get_config_keys_string()), Config::SysConfigKeys => Some(get_config_keys_string()),
_ => self.sql.get_raw_config(key.as_ref()).await?, _ => self.sql.get_raw_config(key.as_ref()).await?,
}; };
Ok(value)
}
/// Get a config key value if set, or a default value. Returns `None` if no value exists.
pub async fn get_config(&self, key: Config) -> Result<Option<String>> {
let value = self.get_config_opt(key).await?;
if value.is_some() { if value.is_some() {
return Ok(value); return Ok(value);
} }
@@ -484,7 +489,16 @@ impl Context {
Ok(val.map(|s| s.to_string())) Ok(val.map(|s| s.to_string()))
} }
/// Returns Some(T) if a value for the given key exists and was successfully parsed. /// Returns Some(T) if a value for the given key is set and was successfully parsed.
/// Returns None if could not parse.
pub(crate) async fn get_config_opt_parsed<T: FromStr>(&self, key: Config) -> Result<Option<T>> {
self.get_config_opt(key)
.await
.map(|s: Option<String>| s.and_then(|s| s.parse().ok()))
}
/// Returns Some(T) if a value for the given key exists (incl. default value) and was
/// successfully parsed.
/// Returns None if could not parse. /// Returns None if could not parse.
pub async fn get_config_parsed<T: FromStr>(&self, key: Config) -> Result<Option<T>> { pub async fn get_config_parsed<T: FromStr>(&self, key: Config) -> Result<Option<T>> {
self.get_config(key) self.get_config(key)
@@ -512,14 +526,21 @@ impl Context {
Ok(self.get_config_parsed(key).await?.unwrap_or_default()) Ok(self.get_config_parsed(key).await?.unwrap_or_default())
} }
/// Returns boolean configuration value (if any) for the given key. /// Returns boolean configuration value (if set) for the given key.
pub async fn get_config_bool_opt(&self, key: Config) -> Result<Option<bool>> { pub(crate) async fn get_config_bool_opt(&self, key: Config) -> Result<Option<bool>> {
Ok(self.get_config_parsed::<i32>(key).await?.map(|x| x != 0)) Ok(self
.get_config_opt_parsed::<i32>(key)
.await?
.map(|x| x != 0))
} }
/// Returns boolean configuration value for the given key. /// Returns boolean configuration value for the given key.
pub async fn get_config_bool(&self, key: Config) -> Result<bool> { pub async fn get_config_bool(&self, key: Config) -> Result<bool> {
Ok(self.get_config_bool_opt(key).await?.unwrap_or_default()) Ok(self
.get_config_parsed::<i32>(key)
.await?
.map(|x| x != 0)
.unwrap_or_default())
} }
/// Returns true if movebox ("DeltaChat" folder) should be watched. /// Returns true if movebox ("DeltaChat" folder) should be watched.
@@ -547,9 +568,9 @@ impl Context {
/// Returns whether MDNs should be requested. /// Returns whether MDNs should be requested.
pub(crate) async fn should_request_mdns(&self) -> Result<bool> { pub(crate) async fn should_request_mdns(&self) -> Result<bool> {
match self.config_exists(Config::MdnsEnabled).await? { match self.get_config_bool_opt(Config::MdnsEnabled).await? {
true => self.get_config_bool(Config::MdnsEnabled).await, Some(val) => Ok(val),
false => Ok(!self.get_config_bool(Config::Bot).await?), None => Ok(!self.get_config_bool(Config::Bot).await?),
} }
} }
@@ -1017,12 +1038,14 @@ mod tests {
let t = &TestContext::new_alice().await; let t = &TestContext::new_alice().await;
assert!(t.should_request_mdns().await?); assert!(t.should_request_mdns().await?);
assert!(t.should_send_mdns().await?); assert!(t.should_send_mdns().await?);
assert!(t.get_config_bool_opt(Config::MdnsEnabled).await?.is_none());
// The setting should be displayed correctly. // The setting should be displayed correctly.
assert!(t.get_config_bool(Config::MdnsEnabled).await?); assert!(t.get_config_bool(Config::MdnsEnabled).await?);
t.set_config_bool(Config::Bot, true).await?; t.set_config_bool(Config::Bot, true).await?;
assert!(!t.should_request_mdns().await?); assert!(!t.should_request_mdns().await?);
assert!(t.should_send_mdns().await?); assert!(t.should_send_mdns().await?);
assert!(t.get_config_bool_opt(Config::MdnsEnabled).await?.is_none());
assert!(t.get_config_bool(Config::MdnsEnabled).await?); assert!(t.get_config_bool(Config::MdnsEnabled).await?);
Ok(()) Ok(())
} }