changeset 44283:934a79697c36

rust-dirs-multiset: improve temporary error message While we wait on a future patch that could verify that the paths passed to `DirsMultiset` have been audited, we still need to handle this error. This patch makes it easier to bubble up and makes the error clearer. Also, this patch introduces the `subslice_index` function that could be useful for other - albeit niche - purposes. Differential Revision: https://phab.mercurial-scm.org/D7921
author Raphaël Gomès <rgomes@octobus.net>
date Fri, 24 Jan 2020 11:10:07 +0100
parents 877805928f85
children e786d69c665d
files rust/hg-core/src/dirstate/dirs_multiset.rs rust/hg-core/src/lib.rs
diffstat 2 files changed, 15 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate/dirs_multiset.rs	Wed Jan 22 12:11:35 2020 -0500
+++ b/rust/hg-core/src/dirstate/dirs_multiset.rs	Fri Jan 24 11:10:07 2020 +0100
@@ -12,7 +12,7 @@
     dirstate::EntryState,
     utils::{
         files,
-        hg_path::{HgPath, HgPathBuf},
+        hg_path::{HgPath, HgPathBuf, HgPathError},
     },
     DirstateEntry, DirstateMapError, FastHashMap,
 };
@@ -78,7 +78,14 @@
             if subpath.as_bytes().last() == Some(&b'/') {
                 // TODO Remove this once PathAuditor is certified
                 // as the only entrypoint for path data
-                return Err(DirstateMapError::ConsecutiveSlashes);
+                let second_slash_index = subpath.len() - 1;
+
+                return Err(DirstateMapError::InvalidPath(
+                    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 Jan 22 12:11:35 2020 -0500
+++ b/rust/hg-core/src/lib.rs	Fri Jan 24 11:10:07 2020 +0100
@@ -23,7 +23,7 @@
 pub use revlog::*;
 pub mod utils;
 
-use crate::utils::hg_path::HgPathBuf;
+use crate::utils::hg_path::{HgPathBuf, HgPathError};
 pub use filepatterns::{
     build_single_regex, read_pattern_file, PatternSyntax, PatternTuple,
 };
@@ -79,18 +79,17 @@
 pub enum DirstateMapError {
     PathNotFound(HgPathBuf),
     EmptyPath,
-    ConsecutiveSlashes,
+    InvalidPath(HgPathError),
 }
 
 impl ToString for DirstateMapError {
     fn to_string(&self) -> String {
-        use crate::DirstateMapError::*;
         match self {
-            PathNotFound(_) => "expected a value, found none".to_string(),
-            EmptyPath => "Overflow in dirstate.".to_string(),
-            ConsecutiveSlashes => {
-                "found invalid consecutive slashes in path".to_string()
+            DirstateMapError::PathNotFound(_) => {
+                "expected a value, found none".to_string()
             }
+            DirstateMapError::EmptyPath => "Overflow in dirstate.".to_string(),
+            DirstateMapError::InvalidPath(e) => e.to_string(),
         }
     }
 }