diff rust/hg-core/src/revlog/mod.rs @ 52041:652149ed64f0

rust: improve `InvalidRevision` error message I encountered this when debugging earlier and felt like we were losing some information along the way, which we were!
author Rapha?l Gom?s <rgomes@octobus.net>
date Tue, 01 Oct 2024 13:20:40 +0200
parents db7dbe6f7bb2
children 8b7123c8947b
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/mod.rs	Mon Sep 30 17:19:35 2024 +0200
+++ b/rust/hg-core/src/revlog/mod.rs	Tue Oct 01 13:20:40 2024 +0200
@@ -139,6 +139,16 @@
     ParentOutOfRange(Revision),
 }
 
+impl std::fmt::Display for GraphError {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match self {
+            GraphError::ParentOutOfRange(revision) => {
+                write!(f, "parent out of range ({})", revision)
+            }
+        }
+    }
+}
+
 impl<T: Graph> Graph for &T {
     fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
         (*self).parents(rev)
@@ -193,7 +203,8 @@
 
 #[derive(Debug, derive_more::From, derive_more::Display)]
 pub enum RevlogError {
-    InvalidRevision,
+    #[display(fmt = "invalid revision identifier: {}", "_0")]
+    InvalidRevision(String),
     /// Working directory is not supported
     WDirUnsupported,
     /// Found more than one entry whose ID match the requested prefix
@@ -809,7 +820,7 @@
         if let Some(nodemap) = &self.nodemap {
             nodemap
                 .find_bin(&self.index, node)?
-                .ok_or(RevlogError::InvalidRevision)
+                .ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
         } else {
             self.rev_from_node_no_persistent_nodemap(node)
         }
@@ -851,7 +862,8 @@
                 found_by_prefix = Some(rev)
             }
         }
-        found_by_prefix.ok_or(RevlogError::InvalidRevision)
+        found_by_prefix
+            .ok_or(RevlogError::InvalidRevision(format!("{:x}", node)))
     }
 
     /// Returns whether the given revision exists in this revlog.
@@ -960,7 +972,7 @@
         let index_entry = self
             .index
             .get_entry(rev)
-            .ok_or(RevlogError::InvalidRevision)?;
+            .ok_or(RevlogError::InvalidRevision(rev.to_string()))?;
         let offset = index_entry.offset();
         let start = if self.index.is_inline() {
             offset + ((rev.0 as usize + 1) * INDEX_ENTRY_SIZE)