comparison rust/rhg/src/commands/debugdata.rs @ 46436:252d1bdba33d

rhg: replace `map_*_error` functions with `From` impls Differential Revision: https://phab.mercurial-scm.org/D9876
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 26 Jan 2021 20:31:26 +0100
parents 3e2d539d0d1a
children a6e4e4650bac
comparison
equal deleted inserted replaced
46435:2e2033081274 46436:252d1bdba33d
1 use crate::commands::Command; 1 use crate::commands::Command;
2 use crate::error::CommandError; 2 use crate::error::CommandError;
3 use crate::ui::utf8_to_local;
4 use crate::ui::Ui; 3 use crate::ui::Ui;
5 use hg::operations::{debug_data, DebugDataError, DebugDataKind}; 4 use hg::operations::{debug_data, DebugDataKind};
6 use hg::repo::Repo; 5 use hg::repo::Repo;
7 use micro_timer::timed; 6 use micro_timer::timed;
8 7
9 pub const HELP_TEXT: &str = " 8 pub const HELP_TEXT: &str = "
10 Dump the contents of a data file revision 9 Dump the contents of a data file revision
24 impl<'a> Command for DebugDataCommand<'a> { 23 impl<'a> Command for DebugDataCommand<'a> {
25 #[timed] 24 #[timed]
26 fn run(&self, ui: &Ui) -> Result<(), CommandError> { 25 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
27 let repo = Repo::find()?; 26 let repo = Repo::find()?;
28 let data = debug_data(&repo, self.rev, self.kind) 27 let data = debug_data(&repo, self.rev, self.kind)
29 .map_err(|e| to_command_error(self.rev, e))?; 28 .map_err(|e| (e, self.rev))?;
30 29
31 let mut stdout = ui.stdout_buffer(); 30 let mut stdout = ui.stdout_buffer();
32 stdout.write_all(&data)?; 31 stdout.write_all(&data)?;
33 stdout.flush()?; 32 stdout.flush()?;
34 33
35 Ok(()) 34 Ok(())
36 } 35 }
37 } 36 }
38
39 /// Convert operation errors to command errors
40 fn to_command_error(rev: &str, err: DebugDataError) -> CommandError {
41 match err {
42 DebugDataError::IoError(err) => CommandError::Abort(Some(
43 utf8_to_local(&format!("abort: {}\n", err)).into(),
44 )),
45 DebugDataError::InvalidRevision => CommandError::Abort(Some(
46 utf8_to_local(&format!(
47 "abort: invalid revision identifier{}\n",
48 rev
49 ))
50 .into(),
51 )),
52 DebugDataError::AmbiguousPrefix => CommandError::Abort(Some(
53 utf8_to_local(&format!(
54 "abort: ambiguous revision identifier{}\n",
55 rev
56 ))
57 .into(),
58 )),
59 DebugDataError::UnsuportedRevlogVersion(version) => {
60 CommandError::Abort(Some(
61 utf8_to_local(&format!(
62 "abort: unsupported revlog version {}\n",
63 version
64 ))
65 .into(),
66 ))
67 }
68 DebugDataError::CorruptedRevlog => {
69 CommandError::Abort(Some("abort: corrupted revlog\n".into()))
70 }
71 DebugDataError::UnknowRevlogDataFormat(format) => {
72 CommandError::Abort(Some(
73 utf8_to_local(&format!(
74 "abort: unknow revlog dataformat {:?}\n",
75 format
76 ))
77 .into(),
78 ))
79 }
80 }
81 }