Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/operations/find_root.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 | dca9cb99971c |
children |
comparison
equal
deleted
inserted
replaced
46433:4b381dbbf8b7 | 46434:3e2d539d0d1a |
---|---|
1 use std::fmt; | |
2 use std::path::{Path, PathBuf}; | 1 use std::path::{Path, PathBuf}; |
3 | 2 |
4 /// Kind of error encoutered by FindRoot | 3 /// Error type for `find_root` |
5 #[derive(Debug)] | 4 #[derive(Debug)] |
6 pub enum FindRootErrorKind { | 5 pub enum FindRootError { |
7 /// Root of the repository has not been found | 6 /// Root of the repository has not been found |
8 /// Contains the current directory used by FindRoot | 7 /// Contains the current directory used by FindRoot |
9 RootNotFound(PathBuf), | 8 RootNotFound(PathBuf), |
10 /// The current directory does not exists or permissions are insufficient | 9 /// The current directory does not exists or permissions are insufficient |
11 /// to get access to it | 10 /// to get access to it |
12 GetCurrentDirError(std::io::Error), | 11 GetCurrentDirError(std::io::Error), |
13 } | 12 } |
14 | 13 |
15 /// A FindRoot error | |
16 #[derive(Debug)] | |
17 pub struct FindRootError { | |
18 /// Kind of error encoutered by FindRoot | |
19 pub kind: FindRootErrorKind, | |
20 } | |
21 | |
22 impl std::error::Error for FindRootError {} | |
23 | |
24 impl fmt::Display for FindRootError { | |
25 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
26 unimplemented!() | |
27 } | |
28 } | |
29 | |
30 /// Find the root of the repository | 14 /// Find the root of the repository |
31 /// by searching for a .hg directory in the process’ current directory and its | 15 /// by searching for a .hg directory in the process’ current directory and its |
32 /// ancestors | 16 /// ancestors |
33 pub fn find_root() -> Result<PathBuf, FindRootError> { | 17 pub fn find_root() -> Result<PathBuf, FindRootError> { |
34 let current_dir = std::env::current_dir().map_err(|e| FindRootError { | 18 let current_dir = std::env::current_dir() |
35 kind: FindRootErrorKind::GetCurrentDirError(e), | 19 .map_err(|e| FindRootError::GetCurrentDirError(e))?; |
36 })?; | |
37 Ok(find_root_from_path(¤t_dir)?.into()) | 20 Ok(find_root_from_path(¤t_dir)?.into()) |
38 } | 21 } |
39 | 22 |
40 /// Find the root of the repository | 23 /// Find the root of the repository |
41 /// by searching for a .hg directory in the given directory and its ancestors | 24 /// by searching for a .hg directory in the given directory and its ancestors |
46 for ancestor in start.ancestors() { | 29 for ancestor in start.ancestors() { |
47 if ancestor.join(".hg").exists() { | 30 if ancestor.join(".hg").exists() { |
48 return Ok(ancestor); | 31 return Ok(ancestor); |
49 } | 32 } |
50 } | 33 } |
51 Err(FindRootError { | 34 Err(FindRootError::RootNotFound(start.into())) |
52 kind: FindRootErrorKind::RootNotFound(start.into()), | |
53 }) | |
54 } | 35 } |
55 | 36 |
56 #[cfg(test)] | 37 #[cfg(test)] |
57 mod tests { | 38 mod tests { |
58 use super::*; | 39 use super::*; |
66 | 47 |
67 let err = find_root_from_path(&path).unwrap_err(); | 48 let err = find_root_from_path(&path).unwrap_err(); |
68 | 49 |
69 // TODO do something better | 50 // TODO do something better |
70 assert!(match err { | 51 assert!(match err { |
71 FindRootError { kind } => match kind { | 52 FindRootError::RootNotFound(p) => p == path.to_path_buf(), |
72 FindRootErrorKind::RootNotFound(p) => p == path.to_path_buf(), | 53 _ => false, |
73 _ => false, | |
74 }, | |
75 }) | 54 }) |
76 } | 55 } |
77 | 56 |
78 #[test] | 57 #[test] |
79 fn dot_hg_in_current_path() { | 58 fn dot_hg_in_current_path() { |