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