changeset 50769:67faf1bd8acd

rust-config: add devel warning when using undeclared config items This mirrors the Python implementation now that we're done catching up.
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 06 Jul 2023 11:44:30 +0200
parents 067edf5083a1
children d64df6b35007
files rust/hg-core/src/config/mod.rs
diffstat 1 files changed, 32 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/config/mod.rs	Thu Jul 06 12:17:20 2023 +0200
+++ b/rust/hg-core/src/config/mod.rs	Thu Jul 06 11:44:30 2023 +0200
@@ -416,12 +416,43 @@
                 }
                 match self.get_default(section, item)? {
                     Some(default) => Ok(default.try_into()?),
-                    None => Ok(None),
+                    None => {
+                        self.print_devel_warning(section, item)?;
+                        Ok(None)
+                    }
                 }
             }
         }
     }
 
+    fn print_devel_warning(
+        &self,
+        section: &[u8],
+        item: &[u8],
+    ) -> Result<(), HgError> {
+        let warn_all = self.get_bool(b"devel", b"all-warnings")?;
+        let warn_specific = self.get_bool(b"devel", b"warn-config-unknown")?;
+        if !warn_all || !warn_specific {
+            // We technically shouldn't print anything here since it's not
+            // the concern of `hg-core`.
+            //
+            // We're printing directly to stderr since development warnings
+            // are not on by default and surfacing this to consumer crates
+            // (like `rhg`) would be more difficult, probably requiring
+            // something à la `log` crate.
+            //
+            // TODO maybe figure out a way of exposing a "warnings" channel
+            // that consumer crates can hook into. It would be useful for
+            // all other warnings that `hg-core` could expose.
+            eprintln!(
+                "devel-warn: accessing unregistered config item: '{}.{}'",
+                String::from_utf8_lossy(section),
+                String::from_utf8_lossy(item),
+            );
+        }
+        Ok(())
+    }
+
     /// Returns an `Err` if the first value found is not a valid UTF-8 string.
     /// Otherwise, returns an `Ok(value)` if found, or `None`.
     pub fn get_str(