annotate rust/hg-core/src/operations/debugdata.rs @ 46135: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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
1 // debugdata.rs
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
2 //
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
3 // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
4 //
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
7
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
8 use std::path::Path;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
9
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
10 use crate::revlog::revlog::{Revlog, RevlogError};
46033
88e741bf2d93 rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents: 46032
diff changeset
11 use crate::revlog::NodePrefix;
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
12 use crate::revlog::Revision;
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
13
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
14 /// Kind of data to debug
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
15 #[derive(Debug, Copy, Clone)]
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
16 pub enum DebugDataKind {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
17 Changelog,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
18 Manifest,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
19 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
20
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
21 /// Kind of error encountered by DebugData
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
22 #[derive(Debug)]
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
23 pub enum DebugDataErrorKind {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
24 /// Error when reading a `revlog` file.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
25 IoError(std::io::Error),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
26 /// The revision has not been found.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
27 InvalidRevision,
46032
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
28 /// Found more than one revision whose ID match the requested prefix
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
29 AmbiguousPrefix,
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
30 /// A `revlog` file is corrupted.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
31 CorruptedRevlog,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
32 /// The `revlog` format version is not supported.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
33 UnsuportedRevlogVersion(u16),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
34 /// The `revlog` data format is not supported.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
35 UnknowRevlogDataFormat(u8),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
36 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
37
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
38 /// A DebugData error
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
39 #[derive(Debug)]
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
40 pub struct DebugDataError {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
41 /// Kind of error encountered by DebugData
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
42 pub kind: DebugDataErrorKind,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
43 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
44
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
45 impl From<DebugDataErrorKind> for DebugDataError {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
46 fn from(kind: DebugDataErrorKind) -> Self {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
47 DebugDataError { kind }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
48 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
49 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
50
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
51 impl From<std::io::Error> for DebugDataError {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
52 fn from(err: std::io::Error) -> Self {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
53 let kind = DebugDataErrorKind::IoError(err);
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
54 DebugDataError { kind }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
55 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
56 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
57
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
58 impl From<RevlogError> for DebugDataError {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
59 fn from(err: RevlogError) -> Self {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
60 match err {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
61 RevlogError::IoError(err) => DebugDataErrorKind::IoError(err),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
62 RevlogError::UnsuportedVersion(version) => {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
63 DebugDataErrorKind::UnsuportedRevlogVersion(version)
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
64 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
65 RevlogError::InvalidRevision => {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
66 DebugDataErrorKind::InvalidRevision
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
67 }
46032
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
68 RevlogError::AmbiguousPrefix => {
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
69 DebugDataErrorKind::AmbiguousPrefix
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
70 }
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
71 RevlogError::Corrupted => DebugDataErrorKind::CorruptedRevlog,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
72 RevlogError::UnknowDataFormat(format) => {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
73 DebugDataErrorKind::UnknowRevlogDataFormat(format)
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
74 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
75 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
76 .into()
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
77 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
78 }
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
79
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
80 /// Dump the contents data of a revision.
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
81 pub fn debug_data(
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
82 root: &Path,
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
83 rev: &str,
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
84 kind: DebugDataKind,
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
85 ) -> Result<Vec<u8>, DebugDataError> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
86 let index_file = match kind {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
87 DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"),
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
88 DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"),
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
89 };
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
90 let revlog = Revlog::open(&index_file, None)?;
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
91
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
92 let data = match rev.parse::<Revision>() {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
93 Ok(rev) => revlog.get_rev_data(rev)?,
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
94 _ => {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
95 let node = NodePrefix::from_hex(&rev)
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
96 .map_err(|_| DebugDataErrorKind::InvalidRevision)?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
97 let rev = revlog.get_node_rev(node.borrow())?;
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
98 revlog.get_rev_data(rev)?
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
99 }
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
100 };
45813
57dc78861196 rhg: add full node id support for `debugdata` command
Antoine cezar<acezar@chwitlabs.fr>
parents: 45806
diff changeset
101
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
102 Ok(data)
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
103 }