changeset 50860:f50e71fdfcb4

rust: improve the type on DirsMultiset::from_manifest It could only return an HgPathError, but we didn't express this in the type, so we needed some unreachable!()s. Now that is expressed in the type.
author Spencer Baugh <sbaugh@janestreet.com>
date Tue, 08 Aug 2023 11:50:26 -0400
parents 2b4bcdc948e7
children 090658724abf
files rust/hg-core/src/dirstate/dirs_multiset.rs rust/hg-core/src/lib.rs rust/hg-core/src/matchers.rs
diffstat 3 files changed, 23 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate/dirs_multiset.rs	Wed Aug 02 09:59:49 2023 -0400
+++ b/rust/hg-core/src/dirstate/dirs_multiset.rs	Tue Aug 08 11:50:26 2023 -0400
@@ -62,7 +62,7 @@
     /// Initializes the multiset from a manifest.
     pub fn from_manifest(
         manifest: &[impl AsRef<HgPath>],
-    ) -> Result<Self, DirstateMapError> {
+    ) -> Result<Self, HgPathError> {
         let mut multiset = DirsMultiset {
             inner: FastHashMap::default(),
         };
@@ -80,19 +80,17 @@
     pub fn add_path(
         &mut self,
         path: impl AsRef<HgPath>,
-    ) -> Result<(), DirstateMapError> {
+    ) -> Result<(), HgPathError> {
         for subpath in files::find_dirs(path.as_ref()) {
             if subpath.as_bytes().last() == Some(&b'/') {
                 // TODO Remove this once PathAuditor is certified
                 // as the only entrypoint for path data
                 let second_slash_index = subpath.len() - 1;
 
-                return Err(DirstateMapError::InvalidPath(
-                    HgPathError::ConsecutiveSlashes {
-                        bytes: path.as_ref().as_bytes().to_owned(),
-                        second_slash_index,
-                    },
-                ));
+                return Err(HgPathError::ConsecutiveSlashes {
+                    bytes: path.as_ref().as_bytes().to_owned(),
+                    second_slash_index,
+                });
             }
             if let Some(val) = self.inner.get_mut(subpath) {
                 *val += 1;
--- a/rust/hg-core/src/lib.rs	Wed Aug 02 09:59:49 2023 -0400
+++ b/rust/hg-core/src/lib.rs	Tue Aug 08 11:50:26 2023 -0400
@@ -66,6 +66,12 @@
     InvalidPath(HgPathError),
 }
 
+impl From<HgPathError> for DirstateMapError {
+    fn from(error: HgPathError) -> Self {
+        Self::InvalidPath(error)
+    }
+}
+
 impl fmt::Display for DirstateMapError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
@@ -83,6 +89,12 @@
     Common(errors::HgError),
 }
 
+impl From<HgPathError> for DirstateError {
+    fn from(error: HgPathError) -> Self {
+        Self::Map(DirstateMapError::InvalidPath(error))
+    }
+}
+
 impl fmt::Display for DirstateError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
--- a/rust/hg-core/src/matchers.rs	Wed Aug 02 09:59:49 2023 -0400
+++ b/rust/hg-core/src/matchers.rs	Tue Aug 08 11:50:26 2023 -0400
@@ -15,11 +15,10 @@
     },
     utils::{
         files::find_dirs,
-        hg_path::{HgPath, HgPathBuf},
+        hg_path::{HgPath, HgPathBuf, HgPathError},
         Escaped,
     },
-    DirsMultiset, DirstateMapError, FastHashMap, IgnorePattern, PatternError,
-    PatternSyntax,
+    DirsMultiset, FastHashMap, IgnorePattern, PatternError, PatternSyntax,
 };
 
 use crate::dirstate::status::IgnoreFnType;
@@ -177,7 +176,7 @@
 }
 
 impl FileMatcher {
-    pub fn new(files: Vec<HgPathBuf>) -> Result<Self, DirstateMapError> {
+    pub fn new(files: Vec<HgPathBuf>) -> Result<Self, HgPathError> {
         let dirs = DirsMultiset::from_manifest(&files)?;
         Ok(Self {
             files: HashSet::from_iter(files.into_iter()),
@@ -760,20 +759,12 @@
     let mut parents = HashSet::new();
 
     parents.extend(
-        DirsMultiset::from_manifest(&dirs)
-            .map_err(|e| match e {
-                DirstateMapError::InvalidPath(e) => e,
-                _ => unreachable!(),
-            })?
+        DirsMultiset::from_manifest(&dirs)?
             .iter()
             .map(ToOwned::to_owned),
     );
     parents.extend(
-        DirsMultiset::from_manifest(&roots)
-            .map_err(|e| match e {
-                DirstateMapError::InvalidPath(e) => e,
-                _ => unreachable!(),
-            })?
+        DirsMultiset::from_manifest(&roots)?
             .iter()
             .map(ToOwned::to_owned),
     );