rust/hg-core/src/lib.rs
author Raphaël Gomès <rgomes@octobus.net>
Mon, 06 May 2019 22:50:34 +0200
changeset 42303 e240bec26626
parent 42302 d1786c1d34fa
child 42327 e8f3740cc067
permissions -rw-r--r--
rust-dirstate: add rust-cpython bindings to the new parse/pack functions This allows for Python code to call `parse/pack_dirstate` transparently. These bindings are heavy given the relatively simple task, as they are bound to implementation details of both the C and Python code. They will be slimmed down in future patches and eventually completely removed once more of the dirstate code has been refactored/rewritten in Rust. Both functions emulate the mutate-on-loop style of the Python and C implementations by looping over changed items in the compatibility layer, instead of at the core functions. Differential Revision: https://phab.mercurial-scm.org/D6349
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     1
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     2
//
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
     5
extern crate byteorder;
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
     6
extern crate memchr;
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
     7
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     8
mod ancestors;
41242
47881d2a9d99 rust: dagop.headrevs() Rust counterparts
Georges Racinet on ishtar.racinet.fr <georges@racinet.fr>
parents: 41241
diff changeset
     9
pub mod dagops;
41054
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 40959
diff changeset
    10
pub use ancestors::{AncestorsIterator, LazyAncestors, MissingAncestors};
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    11
mod dirstate;
42178
10b465d61556 rust-discovery: starting core implementation
Georges Racinet <georges.racinet@octobus.net>
parents: 41717
diff changeset
    12
pub mod discovery;
42303
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    13
pub mod testing; // unconditionally built, for use from integration tests
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    14
pub use dirstate::{
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    15
    pack_dirstate, parse_dirstate, CopyVec, CopyVecEntry, DirstateEntry,
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    16
    DirstateParents, DirstateVec,
e240bec26626 rust-dirstate: add rust-cpython bindings to the new parse/pack functions
Raphaël Gomès <rgomes@octobus.net>
parents: 42302
diff changeset
    17
};
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    18
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    19
/// Mercurial revision numbers
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    20
///
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    21
/// As noted in revlog.c, revision numbers are actually encoded in
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    22
/// 4 bytes, and are liberally converted to ints, whence the i32
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    23
pub type Revision = i32;
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    24
41717
9060af281be7 rust: itering less on MissingAncestors.bases for max()
Georges Racinet <georges.racinet@octobus.net>
parents: 41692
diff changeset
    25
/// Marker expressing the absence of a parent
9060af281be7 rust: itering less on MissingAncestors.bases for max()
Georges Racinet <georges.racinet@octobus.net>
parents: 41692
diff changeset
    26
///
9060af281be7 rust: itering less on MissingAncestors.bases for max()
Georges Racinet <georges.racinet@octobus.net>
parents: 41692
diff changeset
    27
/// Independently of the actual representation, `NULL_REVISION` is guaranteed
9060af281be7 rust: itering less on MissingAncestors.bases for max()
Georges Racinet <georges.racinet@octobus.net>
parents: 41692
diff changeset
    28
/// to be smaller that all existing revisions.
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    29
pub const NULL_REVISION: Revision = -1;
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    30
41348
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    31
/// Same as `mercurial.node.wdirrev`
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    32
///
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    33
/// This is also equal to `i32::max_value()`, but it's better to spell
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    34
/// it out explicitely, same as in `mercurial.node`
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    35
pub const WORKING_DIRECTORY_REVISION: Revision = 0x7fffffff;
2f54f31c41aa rust: working directory revision number constant
Georges Racinet <georges.racinet@octobus.net>
parents: 41242
diff changeset
    36
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    37
/// The simplest expression of what we need of Mercurial DAGs.
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    38
pub trait Graph {
40959
d097dd0afc19 rust: translation of missingancestors
Georges Racinet <gracinet@anybox.fr>
parents: 40933
diff changeset
    39
    /// Return the two parents of the given `Revision`.
d097dd0afc19 rust: translation of missingancestors
Georges Racinet <gracinet@anybox.fr>
parents: 40933
diff changeset
    40
    ///
d097dd0afc19 rust: translation of missingancestors
Georges Racinet <gracinet@anybox.fr>
parents: 40933
diff changeset
    41
    /// Each of the parents can be independently `NULL_REVISION`
40933
18513d6ef7d4 rust: changed Graph.parents to return [Revision; 2]
Georges Racinet <gracinet@anybox.fr>
parents: 40271
diff changeset
    42
    fn parents(&self, Revision) -> Result<[Revision; 2], GraphError>;
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    43
}
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    44
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    45
#[derive(Clone, Debug, PartialEq)]
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    46
pub enum GraphError {
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    47
    ParentOutOfRange(Revision),
41349
ee943a920606 rust: error for WdirUnsupported with cpython conversion as exception
Georges Racinet <georges.racinet@octobus.net>
parents: 41348
diff changeset
    48
    WorkingDirectoryUnsupported,
40271
dbc28c91f7ff rust: pure Rust lazyancestors iterator
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    49
}
42302
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    50
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    51
#[derive(Clone, Debug, PartialEq)]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    52
pub enum DirstateParseError {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    53
    TooLittleData,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    54
    Overflow,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    55
    CorruptedEntry(String),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    56
}
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    57
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    58
#[derive(Debug, PartialEq)]
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    59
pub enum DirstatePackError {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    60
    CorruptedEntry(String),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    61
    CorruptedParent,
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    62
    BadSize(usize, usize),
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    63
}
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    64
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    65
impl From<std::io::Error> for DirstatePackError {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    66
    fn from(e: std::io::Error) -> Self {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    67
        DirstatePackError::CorruptedEntry(e.to_string())
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    68
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    69
}
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    70
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    71
impl From<std::io::Error> for DirstateParseError {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    72
    fn from(e: std::io::Error) -> Self {
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    73
        DirstateParseError::CorruptedEntry(e.to_string())
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    74
    }
d1786c1d34fa rust-dirstate: add rust implementation of `parse_dirstate` and `pack_dirstate`
Raphaël Gomès <rgomes@octobus.net>
parents: 42178
diff changeset
    75
}