rust: Return HgError instead of RevlogError in revlog constructors
authorSimon Sapin <simon.sapin@octobus.net>
Mon, 13 Sep 2021 18:02:45 +0200
changeset 47963 001d747c2baf
parent 47962 8c29af0f6d6e
child 47964 796206e74b10
rust: Return HgError instead of RevlogError in revlog constructors This leaves fewer cases for callers to handle, as RevlogError is more general Differential Revision: https://phab.mercurial-scm.org/D11410
rust/hg-core/src/repo.rs
rust/hg-core/src/revlog/changelog.rs
rust/hg-core/src/revlog/filelog.rs
rust/hg-core/src/revlog/index.rs
rust/hg-core/src/revlog/manifest.rs
rust/hg-core/src/revlog/nodemap_docket.rs
rust/hg-core/src/revlog/revlog.rs
--- a/rust/hg-core/src/repo.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/repo.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -29,8 +29,8 @@
     // None means not known/initialized yet
     dirstate_parents: Cell<Option<DirstateParents>>,
     dirstate_map: LazyCell<OwningDirstateMap, DirstateError>,
-    changelog: LazyCell<Changelog, RevlogError>,
-    manifestlog: LazyCell<Manifestlog, RevlogError>,
+    changelog: LazyCell<Changelog, HgError>,
+    manifestlog: LazyCell<Manifestlog, HgError>,
 }
 
 #[derive(Debug, derive_more::From)]
@@ -320,19 +320,19 @@
         self.dirstate_map.get_mut_or_init(self)
     }
 
-    pub fn changelog(&self) -> Result<Ref<Changelog>, RevlogError> {
+    pub fn changelog(&self) -> Result<Ref<Changelog>, HgError> {
         self.changelog.get_or_init(self)
     }
 
-    pub fn changelog_mut(&self) -> Result<RefMut<Changelog>, RevlogError> {
+    pub fn changelog_mut(&self) -> Result<RefMut<Changelog>, HgError> {
         self.changelog.get_mut_or_init(self)
     }
 
-    pub fn manifestlog(&self) -> Result<Ref<Manifestlog>, RevlogError> {
+    pub fn manifestlog(&self) -> Result<Ref<Manifestlog>, HgError> {
         self.manifestlog.get_or_init(self)
     }
 
-    pub fn manifestlog_mut(&self) -> Result<RefMut<Manifestlog>, RevlogError> {
+    pub fn manifestlog_mut(&self) -> Result<RefMut<Manifestlog>, HgError> {
         self.manifestlog.get_mut_or_init(self)
     }
 
@@ -349,7 +349,7 @@
         manifest.get_node(manifest_node.into())
     }
 
-    pub fn filelog(&self, path: &HgPath) -> Result<Filelog, RevlogError> {
+    pub fn filelog(&self, path: &HgPath) -> Result<Filelog, HgError> {
         Filelog::open(self, path)
     }
 }
--- a/rust/hg-core/src/revlog/changelog.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/changelog.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -12,7 +12,7 @@
 
 impl Changelog {
     /// Open the `changelog` of a repository given by its root.
-    pub fn open(repo: &Repo) -> Result<Self, RevlogError> {
+    pub fn open(repo: &Repo) -> Result<Self, HgError> {
         let revlog = Revlog::open(repo, "00changelog.i", None)?;
         Ok(Self { revlog })
     }
--- a/rust/hg-core/src/revlog/filelog.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/filelog.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -17,7 +17,7 @@
 }
 
 impl Filelog {
-    pub fn open(repo: &Repo, file_path: &HgPath) -> Result<Self, RevlogError> {
+    pub fn open(repo: &Repo, file_path: &HgPath) -> Result<Self, HgError> {
         let index_path = store_path(file_path, b".i");
         let data_path = store_path(file_path, b".d");
         let revlog = Revlog::open(repo, index_path, Some(&data_path))?;
--- a/rust/hg-core/src/revlog/index.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/index.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -5,7 +5,6 @@
 
 use crate::errors::HgError;
 use crate::revlog::node::Node;
-use crate::revlog::revlog::RevlogError;
 use crate::revlog::{Revision, NULL_REVISION};
 
 pub const INDEX_ENTRY_SIZE: usize = 64;
@@ -23,7 +22,7 @@
     /// Calculate the start of each entry when is_inline is true.
     pub fn new(
         bytes: Box<dyn Deref<Target = [u8]> + Send>,
-    ) -> Result<Self, RevlogError> {
+    ) -> Result<Self, HgError> {
         if is_inline(&bytes) {
             let mut offset: usize = 0;
             let mut offsets = Vec::new();
--- a/rust/hg-core/src/revlog/manifest.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/manifest.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -1,3 +1,4 @@
+use crate::errors::HgError;
 use crate::repo::Repo;
 use crate::revlog::revlog::{Revlog, RevlogError};
 use crate::revlog::NodePrefix;
@@ -12,7 +13,7 @@
 
 impl Manifestlog {
     /// Open the `manifest` of a repository given by its root.
-    pub fn open(repo: &Repo) -> Result<Self, RevlogError> {
+    pub fn open(repo: &Repo) -> Result<Self, HgError> {
         let revlog = Revlog::open(repo, "00manifest.i", None)?;
         Ok(Self { revlog })
     }
--- a/rust/hg-core/src/revlog/nodemap_docket.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/nodemap_docket.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -4,7 +4,6 @@
 use memmap2::Mmap;
 use std::path::{Path, PathBuf};
 
-use super::revlog::RevlogError;
 use crate::repo::Repo;
 use crate::utils::strip_suffix;
 
@@ -38,7 +37,7 @@
     pub fn read_from_file(
         repo: &Repo,
         index_path: &Path,
-    ) -> Result<Option<(Self, Mmap)>, RevlogError> {
+    ) -> Result<Option<(Self, Mmap)>, HgError> {
         if !repo
             .requirements()
             .contains(requirements::NODEMAP_REQUIREMENT)
@@ -65,10 +64,9 @@
         };
 
         /// Treat any error as a parse error
-        fn parse<T, E>(result: Result<T, E>) -> Result<T, RevlogError> {
-            result.map_err(|_| {
-                HgError::corrupted("nodemap docket parse error").into()
-            })
+        fn parse<T, E>(result: Result<T, E>) -> Result<T, HgError> {
+            result
+                .map_err(|_| HgError::corrupted("nodemap docket parse error"))
         }
 
         let (header, rest) = parse(DocketHeader::from_bytes(input))?;
@@ -94,7 +92,7 @@
             if mmap.len() >= data_length {
                 Ok(Some((docket, mmap)))
             } else {
-                Err(HgError::corrupted("persistent nodemap too short").into())
+                Err(HgError::corrupted("persistent nodemap too short"))
             }
         } else {
             // Even if .hg/requires opted in, some revlogs are deemed small
--- a/rust/hg-core/src/revlog/revlog.rs	Mon Sep 13 17:23:42 2021 +0200
+++ b/rust/hg-core/src/revlog/revlog.rs	Mon Sep 13 18:02:45 2021 +0200
@@ -68,14 +68,14 @@
         repo: &Repo,
         index_path: impl AsRef<Path>,
         data_path: Option<&Path>,
-    ) -> Result<Self, RevlogError> {
+    ) -> Result<Self, HgError> {
         let index_path = index_path.as_ref();
         let index_mmap = repo.store_vfs().mmap_open(&index_path)?;
 
         let version = get_version(&index_mmap);
         if version != 1 {
             // A proper new version should have had a repo/store requirement.
-            return Err(RevlogError::corrupted());
+            return Err(HgError::corrupted("corrupted revlog"));
         }
 
         let index = Index::new(Box::new(index_mmap))?;