##// END OF EJS Templates
hg-core: define a `DebugData` `Operation`...
Antoine Cezar -
r46098:b56df13a default
parent child Browse files
Show More
@@ -0,0 +1,110 b''
1 // debugdata.rs
2 //
3 // Copyright 2020 Antoine Cezar <antoine.cezar@octobus.net>
4 //
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
7
8 use super::find_root;
9 use crate::revlog::revlog::{Revlog, RevlogError};
10 use crate::revlog::Revision;
11
12 /// Kind of data to debug
13 #[derive(Debug, Copy, Clone)]
14 pub enum DebugDataKind {
15 Changelog,
16 Manifest,
17 }
18
19 /// Kind of error encountered by DebugData
20 #[derive(Debug)]
21 pub enum DebugDataErrorKind {
22 FindRootError(find_root::FindRootError),
23 /// Error when reading a `revlog` file.
24 IoError(std::io::Error),
25 /// The revision has not been found.
26 InvalidRevision,
27 /// A `revlog` file is corrupted.
28 CorruptedRevlog,
29 /// The `revlog` format version is not supported.
30 UnsuportedRevlogVersion(u16),
31 /// The `revlog` data format is not supported.
32 UnknowRevlogDataFormat(u8),
33 }
34
35 /// A DebugData error
36 #[derive(Debug)]
37 pub struct DebugDataError {
38 /// Kind of error encountered by DebugData
39 pub kind: DebugDataErrorKind,
40 }
41
42 impl From<DebugDataErrorKind> for DebugDataError {
43 fn from(kind: DebugDataErrorKind) -> Self {
44 DebugDataError { kind }
45 }
46 }
47
48 impl From<find_root::FindRootError> for DebugDataError {
49 fn from(err: find_root::FindRootError) -> Self {
50 let kind = DebugDataErrorKind::FindRootError(err);
51 DebugDataError { kind }
52 }
53 }
54
55 impl From<std::io::Error> for DebugDataError {
56 fn from(err: std::io::Error) -> Self {
57 let kind = DebugDataErrorKind::IoError(err);
58 DebugDataError { kind }
59 }
60 }
61
62 impl From<RevlogError> for DebugDataError {
63 fn from(err: RevlogError) -> Self {
64 match err {
65 RevlogError::IoError(err) => DebugDataErrorKind::IoError(err),
66 RevlogError::UnsuportedVersion(version) => {
67 DebugDataErrorKind::UnsuportedRevlogVersion(version)
68 }
69 RevlogError::InvalidRevision => {
70 DebugDataErrorKind::InvalidRevision
71 }
72 RevlogError::Corrupted => DebugDataErrorKind::CorruptedRevlog,
73 RevlogError::UnknowDataFormat(format) => {
74 DebugDataErrorKind::UnknowRevlogDataFormat(format)
75 }
76 }
77 .into()
78 }
79 }
80
81 /// Dump the contents data of a revision.
82 pub struct DebugData<'a> {
83 /// Revision or hash of the revision.
84 rev: &'a str,
85 /// Kind of data to debug.
86 kind: DebugDataKind,
87 }
88
89 impl<'a> DebugData<'a> {
90 pub fn new(rev: &'a str, kind: DebugDataKind) -> Self {
91 DebugData { rev, kind }
92 }
93
94 pub fn run(&mut self) -> Result<Vec<u8>, DebugDataError> {
95 let rev = self
96 .rev
97 .parse::<Revision>()
98 .or(Err(DebugDataErrorKind::InvalidRevision))?;
99
100 let root = find_root::FindRoot::new().run()?;
101 let index_file = match self.kind {
102 DebugDataKind::Changelog => root.join(".hg/store/00changelog.i"),
103 DebugDataKind::Manifest => root.join(".hg/store/00manifest.i"),
104 };
105 let revlog = Revlog::open(&index_file)?;
106 let data = revlog.get_rev_data(rev)?;
107
108 Ok(data)
109 }
110 }
@@ -1,17 +1,18 b''
1 //! A distinction is made between operations and commands.
1 //! A distinction is made between operations and commands.
2 //! An operation is what can be done whereas a command is what is exposed by
2 //! An operation is what can be done whereas a command is what is exposed by
3 //! the cli. A single command can use several operations to achieve its goal.
3 //! the cli. A single command can use several operations to achieve its goal.
4
4
5 mod debugdata;
5 mod dirstate_status;
6 mod dirstate_status;
6 mod find_root;
7 mod find_root;
7 mod list_tracked_files;
8 mod list_tracked_files;
8 pub use find_root::{FindRoot, FindRootError, FindRootErrorKind};
9 pub use find_root::{FindRoot, FindRootError, FindRootErrorKind};
9 pub use list_tracked_files::{
10 pub use list_tracked_files::{
10 ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
11 ListTrackedFiles, ListTrackedFilesError, ListTrackedFilesErrorKind,
11 };
12 };
12
13
13 // TODO add an `Operation` trait when GAT have landed (rust #44265):
14 // TODO add an `Operation` trait when GAT have landed (rust #44265):
14 // there is no way to currently define a trait which can both return
15 // there is no way to currently define a trait which can both return
15 // references to `self` and to passed data, which is what we would need.
16 // references to `self` and to passed data, which is what we would need.
16 // Generic Associated Types may fix this and allow us to have a unified
17 // Generic Associated Types may fix this and allow us to have a unified
17 // interface.
18 // interface.
General Comments 0
You need to be logged in to leave comments. Login now