Mercurial > public > mercurial-scm > hg-stable
diff rust/hg-core/src/operations/debugdata.rs @ 46136:dca9cb99971c
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
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 14 Dec 2020 14:59:23 +0100 |
parents | 88e741bf2d93 |
children | 8a4914397d02 |
line wrap: on
line diff
--- a/rust/hg-core/src/operations/debugdata.rs Mon Dec 14 13:47:44 2020 +0100 +++ b/rust/hg-core/src/operations/debugdata.rs Mon Dec 14 14:59:23 2020 +0100 @@ -5,7 +5,8 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. -use super::find_root; +use std::path::Path; + use crate::revlog::revlog::{Revlog, RevlogError}; use crate::revlog::NodePrefix; use crate::revlog::Revision; @@ -20,7 +21,6 @@ /// Kind of error encountered by DebugData #[derive(Debug)] pub enum DebugDataErrorKind { - FindRootError(find_root::FindRootError), /// Error when reading a `revlog` file. IoError(std::io::Error), /// The revision has not been found. @@ -48,13 +48,6 @@ } } -impl From<find_root::FindRootError> for DebugDataError { - fn from(err: find_root::FindRootError) -> Self { - let kind = DebugDataErrorKind::FindRootError(err); - DebugDataError { kind } - } -} - impl From<std::io::Error> for DebugDataError { fn from(err: std::io::Error) -> Self { let kind = DebugDataErrorKind::IoError(err); @@ -85,36 +78,26 @@ } /// Dump the contents data of a revision. -pub struct DebugData<'a> { - /// Revision or hash of the revision. - rev: &'a str, - /// Kind of data to debug. +pub fn debug_data( + root: &Path, + rev: &str, kind: DebugDataKind, -} - -impl<'a> DebugData<'a> { - pub fn new(rev: &'a str, kind: DebugDataKind) -> Self { - DebugData { rev, kind } - } +) -> Result<Vec<u8>, DebugDataError> { + let index_file = match kind { + DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"), + DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"), + }; + let revlog = Revlog::open(&index_file, None)?; - pub fn run(&mut self) -> Result<Vec<u8>, DebugDataError> { - let root = find_root::FindRoot::new().run()?; - let index_file = match self.kind { - DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"), - DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"), - }; - let revlog = Revlog::open(&index_file, None)?; + let data = match rev.parse::<Revision>() { + Ok(rev) => revlog.get_rev_data(rev)?, + _ => { + let node = NodePrefix::from_hex(&rev) + .map_err(|_| DebugDataErrorKind::InvalidRevision)?; + let rev = revlog.get_node_rev(node.borrow())?; + revlog.get_rev_data(rev)? + } + }; - let data = match self.rev.parse::<Revision>() { - Ok(rev) => revlog.get_rev_data(rev)?, - _ => { - let node = NodePrefix::from_hex(&self.rev) - .map_err(|_| DebugDataErrorKind::InvalidRevision)?; - let rev = revlog.get_node_rev(node.borrow())?; - revlog.get_rev_data(rev)? - } - }; - - Ok(data) - } + Ok(data) }