##// END OF EJS Templates
rhg: use `.or(Err(Error))` not `.map_err(|_| Error)` (D9100#inline-15067)...
Antoine cezar -
r46178:db11f8f3 default
parent child Browse files
Show More
@@ -1,99 +1,99
1 1 use crate::commands::Command;
2 2 use crate::error::{CommandError, CommandErrorKind};
3 3 use crate::ui::utf8_to_local;
4 4 use crate::ui::Ui;
5 5 use hg::operations::FindRoot;
6 6 use hg::operations::{CatRev, CatRevError, CatRevErrorKind};
7 7 use hg::utils::hg_path::HgPathBuf;
8 8 use micro_timer::timed;
9 9 use std::convert::TryFrom;
10 10
11 11 pub const HELP_TEXT: &str = "
12 12 Output the current or given revision of files
13 13 ";
14 14
15 15 pub struct CatCommand<'a> {
16 16 rev: Option<&'a str>,
17 17 files: Vec<&'a str>,
18 18 }
19 19
20 20 impl<'a> CatCommand<'a> {
21 21 pub fn new(rev: Option<&'a str>, files: Vec<&'a str>) -> Self {
22 22 Self { rev, files }
23 23 }
24 24
25 25 fn display(&self, ui: &Ui, data: &[u8]) -> Result<(), CommandError> {
26 26 ui.write_stdout(data)?;
27 27 Ok(())
28 28 }
29 29 }
30 30
31 31 impl<'a> Command for CatCommand<'a> {
32 32 #[timed]
33 33 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
34 34 let root = FindRoot::new().run()?;
35 35 let cwd = std::env::current_dir()
36 36 .or_else(|e| Err(CommandErrorKind::CurrentDirNotFound(e)))?;
37 37
38 38 let mut files = vec![];
39 39 for file in self.files.iter() {
40 40 let normalized = cwd.join(&file);
41 41 let stripped = normalized
42 42 .strip_prefix(&root)
43 .map_err(|_| CommandErrorKind::Abort(None))?;
43 .or(Err(CommandErrorKind::Abort(None)))?;
44 44 let hg_file = HgPathBuf::try_from(stripped.to_path_buf())
45 .map_err(|_| CommandErrorKind::Abort(None))?;
45 .or(Err(CommandErrorKind::Abort(None)))?;
46 46 files.push(hg_file);
47 47 }
48 48
49 49 match self.rev {
50 50 Some(rev) => {
51 51 let mut operation = CatRev::new(&root, rev, &files)
52 52 .map_err(|e| map_rev_error(rev, e))?;
53 53 let data =
54 54 operation.run().map_err(|e| map_rev_error(rev, e))?;
55 55 self.display(ui, &data)
56 56 }
57 57 None => Err(CommandErrorKind::Unimplemented.into()),
58 58 }
59 59 }
60 60 }
61 61
62 62 /// Convert `CatRevErrorKind` to `CommandError`
63 63 fn map_rev_error(rev: &str, err: CatRevError) -> CommandError {
64 64 CommandError {
65 65 kind: match err.kind {
66 66 CatRevErrorKind::IoError(err) => CommandErrorKind::Abort(Some(
67 67 utf8_to_local(&format!("abort: {}\n", err)).into(),
68 68 )),
69 69 CatRevErrorKind::InvalidRevision => CommandErrorKind::Abort(Some(
70 70 utf8_to_local(&format!(
71 71 "abort: invalid revision identifier{}\n",
72 72 rev
73 73 ))
74 74 .into(),
75 75 )),
76 76 CatRevErrorKind::UnsuportedRevlogVersion(version) => {
77 77 CommandErrorKind::Abort(Some(
78 78 utf8_to_local(&format!(
79 79 "abort: unsupported revlog version {}\n",
80 80 version
81 81 ))
82 82 .into(),
83 83 ))
84 84 }
85 85 CatRevErrorKind::CorruptedRevlog => CommandErrorKind::Abort(Some(
86 86 "abort: corrupted revlog\n".into(),
87 87 )),
88 88 CatRevErrorKind::UnknowRevlogDataFormat(format) => {
89 89 CommandErrorKind::Abort(Some(
90 90 utf8_to_local(&format!(
91 91 "abort: unknow revlog dataformat {:?}\n",
92 92 format
93 93 ))
94 94 .into(),
95 95 ))
96 96 }
97 97 },
98 98 }
99 99 }
General Comments 0
You need to be logged in to leave comments. Login now