##// END OF EJS Templates
rust: replace most "operation" structs with functions...
rust: replace most "operation" structs with functions The hg-core crate has a partially-formed concept of "operation", represented as structs with constructors and a `run` method. Each struct’s contructor takes different parameters, and each `run` has a different return type. Constructors typically don’t do much more than store parameters for `run` to access them. There was a comment about adding an `Operation` trait when the language supports expressing something so general, but it’s hard to imagine how operations with such different APIs could be used in a generic context. This commit starts removing the concept of "operation", since those are pretty much just functions. Differential Revision: https://phab.mercurial-scm.org/D9595

File last commit:

r46751:dca9cb99 default
r46751:dca9cb99 default
Show More
debugdata.rs
91 lines | 2.7 KiB | application/rls-services+xml | RustLexer
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099 use crate::commands::Command;
use crate::error::{CommandError, CommandErrorKind};
use crate::ui::utf8_to_local;
use crate::ui::Ui;
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 use hg::operations::find_root;
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099 use hg::operations::{
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 debug_data, DebugDataError, DebugDataErrorKind, DebugDataKind,
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099 };
Antoine Cezar
rhg: Add debug timing...
r46101 use micro_timer::timed;
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099
pub const HELP_TEXT: &str = "
Dump the contents of a data file revision
";
pub struct DebugDataCommand<'a> {
rev: &'a str,
kind: DebugDataKind,
}
impl<'a> DebugDataCommand<'a> {
pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
DebugDataCommand { rev, kind }
}
}
impl<'a> Command for DebugDataCommand<'a> {
Antoine Cezar
rhg: Add debug timing...
r46101 #[timed]
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
Simon Sapin
rust: replace most "operation" structs with functions...
r46751 let root = find_root()?;
let data = debug_data(&root, self.rev, self.kind)
.map_err(|e| to_command_error(self.rev, e))?;
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099
let mut stdout = ui.stdout_buffer();
stdout.write_all(&data)?;
stdout.flush()?;
Ok(())
}
}
/// Convert operation errors to command errors
fn to_command_error(rev: &str, err: DebugDataError) -> CommandError {
match err.kind {
DebugDataErrorKind::IoError(err) => CommandError {
kind: CommandErrorKind::Abort(Some(
utf8_to_local(&format!("abort: {}\n", err)).into(),
)),
},
DebugDataErrorKind::InvalidRevision => CommandError {
kind: CommandErrorKind::Abort(Some(
utf8_to_local(&format!(
"abort: invalid revision identifier{}\n",
rev
))
.into(),
)),
},
Simon Sapin
rhg: allow specifying a changeset ID prefix...
r46646 DebugDataErrorKind::AmbiguousPrefix => CommandError {
kind: CommandErrorKind::Abort(Some(
utf8_to_local(&format!(
"abort: ambiguous revision identifier{}\n",
rev
))
.into(),
)),
},
Antoine Cezar
rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand...
r46099 DebugDataErrorKind::UnsuportedRevlogVersion(version) => CommandError {
kind: CommandErrorKind::Abort(Some(
utf8_to_local(&format!(
"abort: unsupported revlog version {}\n",
version
))
.into(),
)),
},
DebugDataErrorKind::CorruptedRevlog => CommandError {
kind: CommandErrorKind::Abort(Some(
"abort: corrupted revlog\n".into(),
)),
},
DebugDataErrorKind::UnknowRevlogDataFormat(format) => CommandError {
kind: CommandErrorKind::Abort(Some(
utf8_to_local(&format!(
"abort: unknow revlog dataformat {:?}\n",
format
))
.into(),
)),
},
}
}