Show More
@@ -0,0 +1,191 b'' | |||||
|
1 | // Copyright 2019-2020 Georges Racinet <georges.racinet@octobus.net> | |||
|
2 | // | |||
|
3 | // This software may be used and distributed according to the terms of the | |||
|
4 | // GNU General Public License version 2 or any later version. | |||
|
5 | ||||
|
6 | //! Definitions and utilities for Revision nodes | |||
|
7 | //! | |||
|
8 | //! In Mercurial code base, it is customary to call "a node" the binary SHA | |||
|
9 | //! of a revision. | |||
|
10 | ||||
|
11 | use hex::{self, FromHex, FromHexError}; | |||
|
12 | ||||
|
13 | /// The length in bytes of a `Node` | |||
|
14 | /// | |||
|
15 | /// This constant is meant to ease refactors of this module, and | |||
|
16 | /// are private so that calling code does not expect all nodes have | |||
|
17 | /// the same size, should we support several formats concurrently in | |||
|
18 | /// the future. | |||
|
19 | const NODE_BYTES_LENGTH: usize = 20; | |||
|
20 | ||||
|
21 | /// The length in bytes of a `Node` | |||
|
22 | /// | |||
|
23 | /// see also `NODES_BYTES_LENGTH` about it being private. | |||
|
24 | const NODE_NYBBLES_LENGTH: usize = 2 * NODE_BYTES_LENGTH; | |||
|
25 | ||||
|
26 | /// Private alias for readability and to ease future change | |||
|
27 | type NodeData = [u8; NODE_BYTES_LENGTH]; | |||
|
28 | ||||
|
29 | /// Binary revision SHA | |||
|
30 | /// | |||
|
31 | /// ## Future changes of hash size | |||
|
32 | /// | |||
|
33 | /// To accomodate future changes of hash size, Rust callers | |||
|
34 | /// should use the conversion methods at the boundaries (FFI, actual | |||
|
35 | /// computation of hashes and I/O) only, and only if required. | |||
|
36 | /// | |||
|
37 | /// All other callers outside of unit tests should just handle `Node` values | |||
|
38 | /// and never make any assumption on the actual length, using [`nybbles_len`] | |||
|
39 | /// if they need a loop boundary. | |||
|
40 | /// | |||
|
41 | /// All methods that create a `Node` either take a type that enforces | |||
|
42 | /// the size or fail immediately at runtime with [`ExactLengthRequired`]. | |||
|
43 | /// | |||
|
44 | /// [`nybbles_len`]: #method.nybbles_len | |||
|
45 | /// [`ExactLengthRequired`]: struct.NodeError#variant.ExactLengthRequired | |||
|
46 | #[derive(Clone, Debug, PartialEq)] | |||
|
47 | pub struct Node { | |||
|
48 | data: NodeData, | |||
|
49 | } | |||
|
50 | ||||
|
51 | /// The node value for NULL_REVISION | |||
|
52 | pub const NULL_NODE: Node = Node { | |||
|
53 | data: [0; NODE_BYTES_LENGTH], | |||
|
54 | }; | |||
|
55 | ||||
|
56 | impl From<NodeData> for Node { | |||
|
57 | fn from(data: NodeData) -> Node { | |||
|
58 | Node { data } | |||
|
59 | } | |||
|
60 | } | |||
|
61 | ||||
|
62 | #[derive(Debug, PartialEq)] | |||
|
63 | pub enum NodeError { | |||
|
64 | ExactLengthRequired(usize, String), | |||
|
65 | HexError(FromHexError, String), | |||
|
66 | } | |||
|
67 | ||||
|
68 | /// Low level utility function, also for prefixes | |||
|
69 | fn get_nybble(s: &[u8], i: usize) -> u8 { | |||
|
70 | if i % 2 == 0 { | |||
|
71 | s[i / 2] >> 4 | |||
|
72 | } else { | |||
|
73 | s[i / 2] & 0x0f | |||
|
74 | } | |||
|
75 | } | |||
|
76 | ||||
|
77 | impl Node { | |||
|
78 | /// Retrieve the `i`th half-byte of the binary data. | |||
|
79 | /// | |||
|
80 | /// This is also the `i`th hexadecimal digit in numeric form, | |||
|
81 | /// also called a [nybble](https://en.wikipedia.org/wiki/Nibble). | |||
|
82 | pub fn get_nybble(&self, i: usize) -> u8 { | |||
|
83 | get_nybble(&self.data, i) | |||
|
84 | } | |||
|
85 | ||||
|
86 | /// Length of the data, in nybbles | |||
|
87 | pub fn nybbles_len(&self) -> usize { | |||
|
88 | // public exposure as an instance method only, so that we can | |||
|
89 | // easily support several sizes of hashes if needed in the future. | |||
|
90 | NODE_NYBBLES_LENGTH | |||
|
91 | } | |||
|
92 | ||||
|
93 | /// Convert from hexadecimal string representation | |||
|
94 | /// | |||
|
95 | /// Exact length is required. | |||
|
96 | /// | |||
|
97 | /// To be used in FFI and I/O only, in order to facilitate future | |||
|
98 | /// changes of hash format. | |||
|
99 | pub fn from_hex(hex: &str) -> Result<Node, NodeError> { | |||
|
100 | Ok(NodeData::from_hex(hex) | |||
|
101 | .map_err(|e| NodeError::from((e, hex)))? | |||
|
102 | .into()) | |||
|
103 | } | |||
|
104 | ||||
|
105 | /// Convert to hexadecimal string representation | |||
|
106 | /// | |||
|
107 | /// To be used in FFI and I/O only, in order to facilitate future | |||
|
108 | /// changes of hash format. | |||
|
109 | pub fn encode_hex(&self) -> String { | |||
|
110 | hex::encode(self.data) | |||
|
111 | } | |||
|
112 | ||||
|
113 | /// Provide access to binary data | |||
|
114 | /// | |||
|
115 | /// This is needed by FFI layers, for instance to return expected | |||
|
116 | /// binary values to Python. | |||
|
117 | pub fn as_bytes(&self) -> &[u8] { | |||
|
118 | &self.data | |||
|
119 | } | |||
|
120 | } | |||
|
121 | ||||
|
122 | impl From<(FromHexError, &str)> for NodeError { | |||
|
123 | fn from(err_offender: (FromHexError, &str)) -> Self { | |||
|
124 | let (err, offender) = err_offender; | |||
|
125 | match err { | |||
|
126 | FromHexError::InvalidStringLength => { | |||
|
127 | NodeError::ExactLengthRequired( | |||
|
128 | NODE_NYBBLES_LENGTH, | |||
|
129 | offender.to_string(), | |||
|
130 | ) | |||
|
131 | } | |||
|
132 | _ => NodeError::HexError(err, offender.to_string()), | |||
|
133 | } | |||
|
134 | } | |||
|
135 | } | |||
|
136 | ||||
|
137 | #[cfg(test)] | |||
|
138 | mod tests { | |||
|
139 | use super::*; | |||
|
140 | ||||
|
141 | fn sample_node() -> Node { | |||
|
142 | let mut data = [0; NODE_BYTES_LENGTH]; | |||
|
143 | data.copy_from_slice(&[ | |||
|
144 | 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, | |||
|
145 | 0x98, 0x76, 0x54, 0x32, 0x10, 0xde, 0xad, 0xbe, 0xef, | |||
|
146 | ]); | |||
|
147 | data.into() | |||
|
148 | } | |||
|
149 | ||||
|
150 | /// Pad an hexadecimal string to reach `NODE_NYBBLES_LENGTH` | |||
|
151 | /// | |||
|
152 | /// The padding is made with zeros | |||
|
153 | fn hex_pad_right(hex: &str) -> String { | |||
|
154 | let mut res = hex.to_string(); | |||
|
155 | while res.len() < NODE_NYBBLES_LENGTH { | |||
|
156 | res.push('0'); | |||
|
157 | } | |||
|
158 | res | |||
|
159 | } | |||
|
160 | ||||
|
161 | fn sample_node_hex() -> String { | |||
|
162 | hex_pad_right("0123456789abcdeffedcba9876543210deadbeef") | |||
|
163 | } | |||
|
164 | ||||
|
165 | #[test] | |||
|
166 | fn test_node_from_hex() { | |||
|
167 | assert_eq!(Node::from_hex(&sample_node_hex()), Ok(sample_node())); | |||
|
168 | ||||
|
169 | let mut short = hex_pad_right("0123"); | |||
|
170 | short.pop(); | |||
|
171 | short.pop(); | |||
|
172 | assert_eq!( | |||
|
173 | Node::from_hex(&short), | |||
|
174 | Err(NodeError::ExactLengthRequired(NODE_NYBBLES_LENGTH, short)), | |||
|
175 | ); | |||
|
176 | ||||
|
177 | let not_hex = hex_pad_right("012... oops"); | |||
|
178 | assert_eq!( | |||
|
179 | Node::from_hex(¬_hex), | |||
|
180 | Err(NodeError::HexError( | |||
|
181 | FromHexError::InvalidHexCharacter { c: '.', index: 3 }, | |||
|
182 | not_hex, | |||
|
183 | )), | |||
|
184 | ); | |||
|
185 | } | |||
|
186 | ||||
|
187 | #[test] | |||
|
188 | fn test_node_encode_hex() { | |||
|
189 | assert_eq!(sample_node().encode_hex(), sample_node_hex()); | |||
|
190 | } | |||
|
191 | } |
@@ -124,10 +124,16 b' dependencies = [' | |||||
124 | ] |
|
124 | ] | |
125 |
|
125 | |||
126 | [[package]] |
|
126 | [[package]] | |
|
127 | name = "hex" | |||
|
128 | version = "0.4.0" | |||
|
129 | source = "registry+https://github.com/rust-lang/crates.io-index" | |||
|
130 | ||||
|
131 | [[package]] | |||
127 | name = "hg-core" |
|
132 | name = "hg-core" | |
128 | version = "0.1.0" |
|
133 | version = "0.1.0" | |
129 | dependencies = [ |
|
134 | dependencies = [ | |
130 | "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", |
|
135 | "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", | |
|
136 | "hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | |||
131 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", |
|
137 | "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", | |
132 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", |
|
138 | "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", | |
133 | "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", |
|
139 | "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", | |
@@ -483,6 +489,7 b' source = "registry+https://github.com/ru' | |||||
483 | "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" |
|
489 | "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" | |
484 | "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" |
|
490 | "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" | |
485 | "checksum getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571" |
|
491 | "checksum getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "473a1265acc8ff1e808cd0a1af8cee3c2ee5200916058a2ca113c29f2d903571" | |
|
492 | "checksum hex 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "023b39be39e3a2da62a94feb433e91e8bcd37676fbc8bea371daf52b7a769a3e" | |||
486 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" |
|
493 | "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | |
487 | "checksum libc 0.2.64 (registry+https://github.com/rust-lang/crates.io-index)" = "74dfca3d9957906e8d1e6a0b641dc9a59848e793f1da2165889fd4f62d10d79c" |
|
494 | "checksum libc 0.2.64 (registry+https://github.com/rust-lang/crates.io-index)" = "74dfca3d9957906e8d1e6a0b641dc9a59848e793f1da2165889fd4f62d10d79c" | |
488 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" |
|
495 | "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" |
@@ -10,6 +10,7 b' name = "hg"' | |||||
10 |
|
10 | |||
11 | [dependencies] |
|
11 | [dependencies] | |
12 | byteorder = "1.3.1" |
|
12 | byteorder = "1.3.1" | |
|
13 | hex = "0.4.0" | |||
13 | lazy_static = "1.3.0" |
|
14 | lazy_static = "1.3.0" | |
14 | memchr = "2.2.0" |
|
15 | memchr = "2.2.0" | |
15 | rand = "0.6.5" |
|
16 | rand = "0.6.5" |
@@ -5,7 +5,9 b'' | |||||
5 | // GNU General Public License version 2 or any later version. |
|
5 | // GNU General Public License version 2 or any later version. | |
6 | //! Mercurial concepts for handling revision history |
|
6 | //! Mercurial concepts for handling revision history | |
7 |
|
7 | |||
|
8 | pub mod node; | |||
8 | pub mod nodemap; |
|
9 | pub mod nodemap; | |
|
10 | pub use node::{Node, NodeError}; | |||
9 |
|
11 | |||
10 | /// Mercurial revision numbers |
|
12 | /// Mercurial revision numbers | |
11 | /// |
|
13 | /// |
General Comments 0
You need to be logged in to leave comments.
Login now