diff rust/hg-core/src/config/mod.rs @ 50992: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 d7b2701f17fa
children 10e57e3f7276
line wrap: on
line diff
--- a/rust/hg-core/src/config/mod.rs	Tue Aug 08 14:14:00 2023 +0200
+++ b/rust/hg-core/src/config/mod.rs	Wed Aug 09 15:41:18 2023 +0200
@@ -422,7 +422,30 @@
                     return Ok(None);
                 }
                 match self.get_default(section, item)? {
-                    Some(default) => Ok(default.try_into()?),
+                    Some(default) => {
+                        // Defaults are TOML values, so they're not in the same
+                        // shape as in the config files.
+                        // First try to convert directly to the expected type
+                        let as_t = default.try_into();
+                        match as_t {
+                            Ok(t) => Ok(t),
+                            Err(e) => {
+                                // If it fails, it means that...
+                                let as_bytes: Result<Option<&[u8]>, _> =
+                                    default.try_into();
+                                match as_bytes {
+                                    Ok(bytes_opt) => {
+                                        if let Some(bytes) = bytes_opt {
+                                            // ...we should be able to parse it
+                                            return Ok(parse(bytes));
+                                        }
+                                        Err(e)
+                                    }
+                                    Err(_) => Err(e),
+                                }
+                            }
+                        }
+                    }
                     None => {
                         self.print_devel_warning(section, item)?;
                         Ok(None)
@@ -779,7 +802,6 @@
         let config = Config::load_from_explicit_sources(vec![])
             .expect("expected valid config");
         let ret = config.get_byte_size(b"cmdserver", b"max-log-size");
-        // FIXME should be `is_ok`
-        assert!(ret.is_err(), "{:?}", ret);
+        assert!(ret.is_ok(), "{:?}", ret);
     }
 }