rust/hg-core/src/operations/list_tracked_files.rs
changeset 45535 72b7d58d6e35
parent 45436 1b3197047f5c
child 45536 639f33f22faf
--- a/rust/hg-core/src/operations/list_tracked_files.rs	Fri Sep 18 16:52:08 2020 +0200
+++ b/rust/hg-core/src/operations/list_tracked_files.rs	Wed Sep 09 12:12:11 2020 +0200
@@ -5,7 +5,6 @@
 // This software may be used and distributed according to the terms of the
 // GNU General Public License version 2 or any later version.
 
-use super::find_root;
 use crate::dirstate::parsers::parse_dirstate;
 use crate::utils::hg_path::HgPath;
 use crate::{DirstateParseError, EntryState};
@@ -13,74 +12,67 @@
 use std::convert::From;
 use std::fmt;
 use std::fs;
-use std::io;
+use std::ops::Deref;
 use std::path::{Path, PathBuf};
 
-/// Kind of error encoutered by ListTrackedFiles
+/// Kind of error encountered by `ListDirstateTrackedFiles`
 #[derive(Debug)]
-pub enum ListTrackedFilesErrorKind {
+pub enum ListDirstateTrackedFilesErrorKind {
+    /// Error when reading the `dirstate` file
+    IoError(std::io::Error),
+    /// Error when parsing the `dirstate` file
     ParseError(DirstateParseError),
 }
 
-/// A ListTrackedFiles error
+/// A `ListDirstateTrackedFiles` error
 #[derive(Debug)]
-pub struct ListTrackedFilesError {
-    /// Kind of error encoutered by ListTrackedFiles
-    pub kind: ListTrackedFilesErrorKind,
+pub struct ListDirstateTrackedFilesError {
+    /// Kind of error encountered by `ListDirstateTrackedFiles`
+    pub kind: ListDirstateTrackedFilesErrorKind,
 }
 
-impl std::error::Error for ListTrackedFilesError {}
+impl std::error::Error for ListDirstateTrackedFilesError {}
 
-impl fmt::Display for ListTrackedFilesError {
+impl fmt::Display for ListDirstateTrackedFilesError {
     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
         unimplemented!()
     }
 }
 
-impl From<ListTrackedFilesErrorKind> for ListTrackedFilesError {
-    fn from(kind: ListTrackedFilesErrorKind) -> Self {
-        ListTrackedFilesError { kind }
+impl From<ListDirstateTrackedFilesErrorKind>
+    for ListDirstateTrackedFilesError
+{
+    fn from(kind: ListDirstateTrackedFilesErrorKind) -> Self {
+        ListDirstateTrackedFilesError { kind }
     }
 }
 
-/// List files under Mercurial control in the working directory
-pub struct ListTrackedFiles {
-    root: PathBuf,
-}
-
-impl ListTrackedFiles {
-    pub fn new() -> Result<Self, find_root::FindRootError> {
-        let root = find_root::FindRoot::new().run()?;
-        Ok(ListTrackedFiles { root })
-    }
-
-    /// Load the tracked files data from disk
-    pub fn load(&self) -> Result<ListDirstateTrackedFiles, io::Error> {
-        let dirstate = &self.root.join(".hg/dirstate");
-        let content = fs::read(&dirstate)?;
-        Ok(ListDirstateTrackedFiles { content })
-    }
-
-    /// Returns the repository root directory
-    /// TODO I think this is a crutch that creates a dependency that should not
-    /// be there. Operations that need the root of the repository should get
-    /// it themselves, probably in a lazy fashion. But this would make the
-    /// current series even larger, so this is simplified for now.
-    pub fn get_root(&self) -> &Path {
-        &self.root
+impl From<std::io::Error> for ListDirstateTrackedFilesError {
+    fn from(err: std::io::Error) -> Self {
+        let kind = ListDirstateTrackedFilesErrorKind::IoError(err);
+        ListDirstateTrackedFilesError { kind }
     }
 }
 
 /// List files under Mercurial control in the working directory
 /// by reading the dirstate
 pub struct ListDirstateTrackedFiles {
+    /// The `dirstate` content.
     content: Vec<u8>,
 }
 
 impl ListDirstateTrackedFiles {
-    pub fn run(&self) -> Result<Vec<&HgPath>, ListTrackedFilesError> {
+    pub fn new(root: &PathBuf) -> Result<Self, ListDirstateTrackedFilesError> {
+        let dirstate = root.join(".hg/dirstate");
+        let content = fs::read(&dirstate)?;
+        Ok(Self { content })
+    }
+
+    pub fn run(
+        &mut self,
+    ) -> Result<Vec<&HgPath>, ListDirstateTrackedFilesError> {
         let (_, entries, _) = parse_dirstate(&self.content)
-            .map_err(ListTrackedFilesErrorKind::ParseError)?;
+            .map_err(ListDirstateTrackedFilesErrorKind::ParseError)?;
         let mut files: Vec<&HgPath> = entries
             .into_iter()
             .filter_map(|(path, entry)| match entry.state {