rust-config: add devel warning when using undeclared config items
authorRaphaël Gomès <rgomes@octobus.net>
Thu, 06 Jul 2023 11:44:30 +0200
changeset 50808 67faf1bd8acd
parent 50807 067edf5083a1
child 50809 d64df6b35007
rust-config: add devel warning when using undeclared config items This mirrors the Python implementation now that we're done catching up.
rust/hg-core/src/config/mod.rs
--- 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(