diff rust/hg-core/src/config/config_items.rs @ 51889:0dbf6a5ccf5f

rust-config: add more ways of reading the config These will be needed for future patches of this series to interpret more complex/different config values.
author Rapha?l Gom?s <rgomes@octobus.net>
date Wed, 19 Jun 2024 12:00:55 +0200
parents 8343947af6a7
children a876ab6c3fd5
line wrap: on
line diff
--- a/rust/hg-core/src/config/config_items.rs	Tue Mar 26 15:51:31 2024 +0000
+++ b/rust/hg-core/src/config/config_items.rs	Wed Jun 19 12:00:55 2024 +0200
@@ -267,6 +267,66 @@
     }
 }
 
+impl TryFrom<&DefaultConfigItem> for Option<i64> {
+    type Error = HgError;
+
+    fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> {
+        match &value.default {
+            Some(default) => {
+                let err = HgError::abort(
+                    format!(
+                        "programming error: wrong query on config item '{}.{}'",
+                        value.section,
+                        value.name
+                    ),
+                    exit_codes::ABORT,
+                    Some(format!(
+                        "asked for 'i64', type of default is '{}'",
+                        default.type_str()
+                    )),
+                );
+                match default {
+                    DefaultConfigItemType::Primitive(
+                        toml::Value::Integer(b),
+                    ) => Ok(Some(*b)),
+                    _ => Err(err),
+                }
+            }
+            None => Ok(None),
+        }
+    }
+}
+
+impl TryFrom<&DefaultConfigItem> for Option<f64> {
+    type Error = HgError;
+
+    fn try_from(value: &DefaultConfigItem) -> Result<Self, Self::Error> {
+        match &value.default {
+            Some(default) => {
+                let err = HgError::abort(
+                    format!(
+                        "programming error: wrong query on config item '{}.{}'",
+                        value.section,
+                        value.name
+                    ),
+                    exit_codes::ABORT,
+                    Some(format!(
+                        "asked for 'f64', type of default is '{}'",
+                        default.type_str()
+                    )),
+                );
+                match default {
+                    DefaultConfigItemType::Primitive(toml::Value::Float(
+                        b,
+                    )) => Ok(Some(*b)),
+                    _ => Err(err),
+                }
+            }
+            None => Ok(None),
+        }
+    }
+}
+
 /// Allows abstracting over more complex default values than just primitives.
 /// The former `configitems.py` contained some dynamic code that is encoded
 /// in this enum.