# HG changeset patch # User Spencer Baugh # Date 1691509826 14400 # Node ID f50e71fdfcb4a538114b693fbcde9c60f2cb9165 # Parent 2b4bcdc948e78220e8e23425c964d74c3f0c8f23 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. diff -r 2b4bcdc948e7 -r f50e71fdfcb4 rust/hg-core/src/dirstate/dirs_multiset.rs --- 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], - ) -> Result { + ) -> Result { let mut multiset = DirsMultiset { inner: FastHashMap::default(), }; @@ -80,19 +80,17 @@ pub fn add_path( &mut self, path: impl AsRef, - ) -> 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; diff -r 2b4bcdc948e7 -r f50e71fdfcb4 rust/hg-core/src/lib.rs --- 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 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 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 { diff -r 2b4bcdc948e7 -r f50e71fdfcb4 rust/hg-core/src/matchers.rs --- 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) -> Result { + pub fn new(files: Vec) -> Result { 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), );