diff --git a/src/config.rs b/src/config.rs
index 408ea8eb2..e547e6b00 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -446,8 +446,8 @@ impl Context {
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.
- pub async fn get_config(&self, key: Config) -> Result> {
+ /// Get a config key value. Returns `None` if no value is set.
+ pub(crate) async fn get_config_opt(&self, key: Config) -> Result > {
let env_key = format!("DELTACHAT_{}", key.as_ref().to_uppercase());
if let Ok(value) = env::var(env_key) {
return Ok(Some(value));
@@ -467,7 +467,12 @@ impl Context {
Config::SysConfigKeys => Some(get_config_keys_string()),
_ => 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 > {
+ let value = self.get_config_opt(key).await?;
if value.is_some() {
return Ok(value);
}
@@ -484,7 +489,16 @@ impl Context {
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(&self, key: Config) -> Result> {
+ self.get_config_opt(key)
+ .await
+ .map(|s: Option| 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.
pub async fn get_config_parsed(&self, key: Config) -> Result> {
self.get_config(key)
@@ -512,14 +526,21 @@ impl Context {
Ok(self.get_config_parsed(key).await?.unwrap_or_default())
}
- /// Returns boolean configuration value (if any) for the given key.
- pub async fn get_config_bool_opt(&self, key: Config) -> Result > {
- Ok(self.get_config_parsed::(key).await?.map(|x| x != 0))
+ /// Returns boolean configuration value (if set) for the given key.
+ pub(crate) async fn get_config_bool_opt(&self, key: Config) -> Result> {
+ Ok(self
+ .get_config_opt_parsed::(key)
+ .await?
+ .map(|x| x != 0))
}
/// Returns boolean configuration value for the given key.
pub async fn get_config_bool(&self, key: Config) -> Result {
- Ok(self.get_config_bool_opt(key).await?.unwrap_or_default())
+ Ok(self
+ .get_config_parsed::(key)
+ .await?
+ .map(|x| x != 0)
+ .unwrap_or_default())
}
/// Returns true if movebox ("DeltaChat" folder) should be watched.
@@ -547,9 +568,9 @@ impl Context {
/// Returns whether MDNs should be requested.
pub(crate) async fn should_request_mdns(&self) -> Result {
- match self.config_exists(Config::MdnsEnabled).await? {
- true => self.get_config_bool(Config::MdnsEnabled).await,
- false => Ok(!self.get_config_bool(Config::Bot).await?),
+ match self.get_config_bool_opt(Config::MdnsEnabled).await? {
+ Some(val) => Ok(val),
+ None => Ok(!self.get_config_bool(Config::Bot).await?),
}
}
@@ -1017,12 +1038,14 @@ mod tests {
let t = &TestContext::new_alice().await;
assert!(t.should_request_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.
assert!(t.get_config_bool(Config::MdnsEnabled).await?);
t.set_config_bool(Config::Bot, true).await?;
assert!(!t.should_request_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?);
Ok(())
}