comparison rust/hg-core/src/operations/list_tracked_files.rs @ 46434:3e2d539d0d1a

rust: remove `FooError` structs with only `kind: FooErrorKind` enum field Use the enum directly as `FooError` instead. Differential Revision: https://phab.mercurial-scm.org/D9874
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 26 Jan 2021 19:07:24 +0100
parents 4b381dbbf8b7
children 2e2033081274
comparison
equal deleted inserted replaced
46433:4b381dbbf8b7 46434:3e2d539d0d1a
14 use crate::utils::hg_path::HgPath; 14 use crate::utils::hg_path::HgPath;
15 use crate::{DirstateParseError, EntryState}; 15 use crate::{DirstateParseError, EntryState};
16 use rayon::prelude::*; 16 use rayon::prelude::*;
17 use std::convert::From; 17 use std::convert::From;
18 18
19 /// Kind of error encountered by `ListDirstateTrackedFiles` 19 /// Error type for `Dirstate` methods
20 #[derive(Debug)] 20 #[derive(Debug)]
21 pub enum ListDirstateTrackedFilesErrorKind { 21 pub enum ListDirstateTrackedFilesError {
22 /// Error when reading the `dirstate` file 22 /// Error when reading the `dirstate` file
23 IoError(std::io::Error), 23 IoError(std::io::Error),
24 /// Error when parsing the `dirstate` file 24 /// Error when parsing the `dirstate` file
25 ParseError(DirstateParseError), 25 ParseError(DirstateParseError),
26 } 26 }
27 27
28 /// A `ListDirstateTrackedFiles` error
29 #[derive(Debug)]
30 pub struct ListDirstateTrackedFilesError {
31 /// Kind of error encountered by `ListDirstateTrackedFiles`
32 pub kind: ListDirstateTrackedFilesErrorKind,
33 }
34
35 impl From<ListDirstateTrackedFilesErrorKind>
36 for ListDirstateTrackedFilesError
37 {
38 fn from(kind: ListDirstateTrackedFilesErrorKind) -> Self {
39 ListDirstateTrackedFilesError { kind }
40 }
41 }
42
43 impl From<std::io::Error> for ListDirstateTrackedFilesError { 28 impl From<std::io::Error> for ListDirstateTrackedFilesError {
44 fn from(err: std::io::Error) -> Self { 29 fn from(err: std::io::Error) -> Self {
45 let kind = ListDirstateTrackedFilesErrorKind::IoError(err); 30 ListDirstateTrackedFilesError::IoError(err)
46 ListDirstateTrackedFilesError { kind }
47 } 31 }
48 } 32 }
49 33
50 /// List files under Mercurial control in the working directory 34 /// List files under Mercurial control in the working directory
51 /// by reading the dirstate 35 /// by reading the dirstate
62 46
63 pub fn tracked_files( 47 pub fn tracked_files(
64 &self, 48 &self,
65 ) -> Result<Vec<&HgPath>, ListDirstateTrackedFilesError> { 49 ) -> Result<Vec<&HgPath>, ListDirstateTrackedFilesError> {
66 let (_, entries, _) = parse_dirstate(&self.content) 50 let (_, entries, _) = parse_dirstate(&self.content)
67 .map_err(ListDirstateTrackedFilesErrorKind::ParseError)?; 51 .map_err(ListDirstateTrackedFilesError::ParseError)?;
68 let mut files: Vec<&HgPath> = entries 52 let mut files: Vec<&HgPath> = entries
69 .into_iter() 53 .into_iter()
70 .filter_map(|(path, entry)| match entry.state { 54 .filter_map(|(path, entry)| match entry.state {
71 EntryState::Removed => None, 55 EntryState::Removed => None,
72 _ => Some(path), 56 _ => Some(path),
75 files.par_sort_unstable(); 59 files.par_sort_unstable();
76 Ok(files) 60 Ok(files)
77 } 61 }
78 } 62 }
79 63
80 /// Kind of error encountered by `ListRevTrackedFiles` 64 /// Error type `list_rev_tracked_files`
81 #[derive(Debug)] 65 #[derive(Debug)]
82 pub enum ListRevTrackedFilesErrorKind { 66 pub enum ListRevTrackedFilesError {
83 /// Error when reading a `revlog` file. 67 /// Error when reading a `revlog` file.
84 IoError(std::io::Error), 68 IoError(std::io::Error),
85 /// The revision has not been found. 69 /// The revision has not been found.
86 InvalidRevision, 70 InvalidRevision,
87 /// Found more than one revision whose ID match the requested prefix 71 /// Found more than one revision whose ID match the requested prefix
92 UnsuportedRevlogVersion(u16), 76 UnsuportedRevlogVersion(u16),
93 /// The `revlog` data format is not supported. 77 /// The `revlog` data format is not supported.
94 UnknowRevlogDataFormat(u8), 78 UnknowRevlogDataFormat(u8),
95 } 79 }
96 80
97 /// A `ListRevTrackedFiles` error
98 #[derive(Debug)]
99 pub struct ListRevTrackedFilesError {
100 /// Kind of error encountered by `ListRevTrackedFiles`
101 pub kind: ListRevTrackedFilesErrorKind,
102 }
103
104 impl From<ListRevTrackedFilesErrorKind> for ListRevTrackedFilesError {
105 fn from(kind: ListRevTrackedFilesErrorKind) -> Self {
106 ListRevTrackedFilesError { kind }
107 }
108 }
109
110 impl From<RevlogError> for ListRevTrackedFilesError { 81 impl From<RevlogError> for ListRevTrackedFilesError {
111 fn from(err: RevlogError) -> Self { 82 fn from(err: RevlogError) -> Self {
112 match err { 83 match err {
113 RevlogError::IoError(err) => { 84 RevlogError::IoError(err) => {
114 ListRevTrackedFilesErrorKind::IoError(err) 85 ListRevTrackedFilesError::IoError(err)
115 } 86 }
116 RevlogError::UnsuportedVersion(version) => { 87 RevlogError::UnsuportedVersion(version) => {
117 ListRevTrackedFilesErrorKind::UnsuportedRevlogVersion(version) 88 ListRevTrackedFilesError::UnsuportedRevlogVersion(version)
118 } 89 }
119 RevlogError::InvalidRevision => { 90 RevlogError::InvalidRevision => {
120 ListRevTrackedFilesErrorKind::InvalidRevision 91 ListRevTrackedFilesError::InvalidRevision
121 } 92 }
122 RevlogError::AmbiguousPrefix => { 93 RevlogError::AmbiguousPrefix => {
123 ListRevTrackedFilesErrorKind::AmbiguousPrefix 94 ListRevTrackedFilesError::AmbiguousPrefix
124 } 95 }
125 RevlogError::Corrupted => { 96 RevlogError::Corrupted => {
126 ListRevTrackedFilesErrorKind::CorruptedRevlog 97 ListRevTrackedFilesError::CorruptedRevlog
127 } 98 }
128 RevlogError::UnknowDataFormat(format) => { 99 RevlogError::UnknowDataFormat(format) => {
129 ListRevTrackedFilesErrorKind::UnknowRevlogDataFormat(format) 100 ListRevTrackedFilesError::UnknowRevlogDataFormat(format)
130 } 101 }
131 } 102 }
132 .into()
133 } 103 }
134 } 104 }
135 105
136 /// List files under Mercurial control at a given revision. 106 /// List files under Mercurial control at a given revision.
137 pub fn list_rev_tracked_files( 107 pub fn list_rev_tracked_files(
141 let rev = crate::revset::resolve_single(revset, repo)?; 111 let rev = crate::revset::resolve_single(revset, repo)?;
142 let changelog = Changelog::open(repo)?; 112 let changelog = Changelog::open(repo)?;
143 let manifest = Manifest::open(repo)?; 113 let manifest = Manifest::open(repo)?;
144 let changelog_entry = changelog.get_rev(rev)?; 114 let changelog_entry = changelog.get_rev(rev)?;
145 let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?) 115 let manifest_node = Node::from_hex(&changelog_entry.manifest_node()?)
146 .or(Err(ListRevTrackedFilesErrorKind::CorruptedRevlog))?; 116 .or(Err(ListRevTrackedFilesError::CorruptedRevlog))?;
147 let manifest_entry = manifest.get_node(manifest_node.into())?; 117 let manifest_entry = manifest.get_node(manifest_node.into())?;
148 Ok(FilesForRev(manifest_entry)) 118 Ok(FilesForRev(manifest_entry))
149 } 119 }
150 120
151 pub struct FilesForRev(ManifestEntry); 121 pub struct FilesForRev(ManifestEntry);