diff rust/hg-core/src/config/config_items.rs @ 50978:58390f59826f

rust-config: fix fallback to default not parsing the default value When a config item's default is a string, it sometimes needs to be parsed into another type, like in the case of `cmdserver.max-log-size`, that returns a number of bytes from a human-readable amount like `25MB`. The logic for the fix is explained inline.
author Rapha?l Gom?s <rgomes@octobus.net>
date Wed, 09 Aug 2023 15:41:18 +0200
parents 7f8f6fe13fa9
children 8343947af6a7
line wrap: on
line diff
--- a/rust/hg-core/src/config/config_items.rs	Tue Aug 08 14:14:00 2023 +0200
+++ b/rust/hg-core/src/config/config_items.rs	Wed Aug 09 15:41:18 2023 +0200
@@ -140,6 +140,39 @@
     }
 }
 
+impl<'a> TryFrom<&'a DefaultConfigItem> for Option<&'a [u8]> {
+    type Error = HgError;
+
+    fn try_from(
+        value: &'a DefaultConfigItem,
+    ) -> Result<Option<&'a [u8]>, 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 bytes, type of default is '{}', \
+                        which cannot be interpreted as bytes",
+                        default.type_str()
+                    )),
+                );
+                match default {
+                    DefaultConfigItemType::Primitive(p) => {
+                        Ok(p.as_str().map(str::as_bytes))
+                    }
+                    _ => Err(err),
+                }
+            }
+            None => Ok(None),
+        }
+    }
+}
+
 impl TryFrom<&DefaultConfigItem> for Option<bool> {
     type Error = HgError;