rust/hg-core/src/operations/debugdata.rs
author Simon Sapin <simon.sapin@octobus.net>
Tue, 26 Jan 2021 20:05:37 +0100
changeset 46435 2e2033081274
parent 46434 3e2d539d0d1a
child 46437 b274aa2f20fd
permissions -rw-r--r--
rust: replace trivial `impl From …` with `#[derive(derive_more::From)]` Crate docs: https://jeltef.github.io/derive_more/derive_more/from.html Differential Revision: https://phab.mercurial-scm.org/D9875
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
     8
use crate::repo::Repo;
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     9
use crate::revlog::revlog::{Revlog, RevlogError};
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    10
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    11
/// Kind of data to debug
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    12
#[derive(Debug, Copy, Clone)]
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    13
pub enum DebugDataKind {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    14
    Changelog,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    15
    Manifest,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    16
}
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    17
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    18
/// Error type for `debug_data`
46435
2e2033081274 rust: replace trivial `impl From …` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents: 46434
diff changeset
    19
#[derive(Debug, derive_more::From)]
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    20
pub enum DebugDataError {
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    21
    /// Error when reading a `revlog` file.
46435
2e2033081274 rust: replace trivial `impl From …` with `#[derive(derive_more::From)]`
Simon Sapin <simon.sapin@octobus.net>
parents: 46434
diff changeset
    22
    #[from]
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    23
    IoError(std::io::Error),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
    /// The revision has not been found.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    25
    InvalidRevision,
46032
8d6164098782 rhg: allow specifying a changeset ID prefix
Simon Sapin <simon-commits@exyr.org>
parents: 45813
diff changeset
    26
    /// 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
    27
    AmbiguousPrefix,
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    28
    /// A `revlog` file is corrupted.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    29
    CorruptedRevlog,
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    30
    /// The `revlog` format version is not supported.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    31
    UnsuportedRevlogVersion(u16),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    32
    /// The `revlog` data format is not supported.
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    33
    UnknowRevlogDataFormat(u8),
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    34
}
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    35
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    36
impl From<RevlogError> for DebugDataError {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    37
    fn from(err: RevlogError) -> Self {
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    38
        match err {
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    39
            RevlogError::IoError(err) => DebugDataError::IoError(err),
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    40
            RevlogError::UnsuportedVersion(version) => {
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    41
                DebugDataError::UnsuportedRevlogVersion(version)
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    42
            }
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    43
            RevlogError::InvalidRevision => DebugDataError::InvalidRevision,
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    44
            RevlogError::AmbiguousPrefix => DebugDataError::AmbiguousPrefix,
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    45
            RevlogError::Corrupted => DebugDataError::CorruptedRevlog,
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    46
            RevlogError::UnknowDataFormat(format) => {
46434
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46433
diff changeset
    47
                DebugDataError::UnknowRevlogDataFormat(format)
45527
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
}
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    52
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    53
/// 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
    54
pub fn debug_data(
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    55
    repo: &Repo,
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    56
    revset: &str,
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    57
    kind: DebugDataKind,
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    58
) -> Result<Vec<u8>, DebugDataError> {
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    59
    let index_file = match kind {
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    60
        DebugDataKind::Changelog => "00changelog.i",
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    61
        DebugDataKind::Manifest => "00manifest.i",
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    62
    };
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46135
diff changeset
    63
    let revlog = Revlog::open(repo, index_file, None)?;
46433
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    64
    let rev =
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    65
        crate::revset::resolve_rev_number_or_hex_prefix(revset, &revlog)?;
4b381dbbf8b7 rhg: centralize parsing of `--rev` CLI arguments
Simon Sapin <simon.sapin@octobus.net>
parents: 46431
diff changeset
    66
    let data = revlog.get_rev_data(rev)?;
46135
dca9cb99971c rust: replace most "operation" structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46033
diff changeset
    67
    Ok(data)
45527
b56df13a0450 hg-core: define a `DebugData` `Operation`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    68
}