Mercurial > hg
annotate rust/hg-core/src/revlog/node.rs @ 46391:cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Differential Revision: https://phab.mercurial-scm.org/D9859
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 25 Jan 2021 11:34:23 +0100 |
parents | 88e741bf2d93 |
children | 6380efb82191 |
rev | line source |
---|---|
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
1 // Copyright 2019-2020 Georges Racinet <georges.racinet@octobus.net> |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
2 // |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
3 // This software may be used and distributed according to the terms of the |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
4 // GNU General Public License version 2 or any later version. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
5 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
6 //! Definitions and utilities for Revision nodes |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
7 //! |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
8 //! In Mercurial code base, it is customary to call "a node" the binary SHA |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
9 //! of a revision. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
10 |
46391
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
11 use bytes_cast::BytesCast; |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
12 use hex::{self, FromHex, FromHexError}; |
46391
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
13 use std::convert::TryFrom; |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
14 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
15 /// The length in bytes of a `Node` |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
16 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
17 /// This constant is meant to ease refactors of this module, and |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
18 /// are private so that calling code does not expect all nodes have |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
19 /// the same size, should we support several formats concurrently in |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
20 /// the future. |
45531
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
21 pub const NODE_BYTES_LENGTH: usize = 20; |
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
22 |
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
23 /// Id of the null node. |
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
24 /// |
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
25 /// Used to indicate the absence of node. |
b0d6309ff50c
hg-core: check data integrity in `Revlog`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
44973
diff
changeset
|
26 pub const NULL_NODE_ID: [u8; NODE_BYTES_LENGTH] = [0u8; NODE_BYTES_LENGTH]; |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
27 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
28 /// The length in bytes of a `Node` |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
29 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
30 /// see also `NODES_BYTES_LENGTH` about it being private. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
31 const NODE_NYBBLES_LENGTH: usize = 2 * NODE_BYTES_LENGTH; |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
32 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
33 /// Private alias for readability and to ease future change |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
34 type NodeData = [u8; NODE_BYTES_LENGTH]; |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
35 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
36 /// Binary revision SHA |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
37 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
38 /// ## Future changes of hash size |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
39 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
40 /// To accomodate future changes of hash size, Rust callers |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
41 /// should use the conversion methods at the boundaries (FFI, actual |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
42 /// computation of hashes and I/O) only, and only if required. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
43 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
44 /// All other callers outside of unit tests should just handle `Node` values |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
45 /// and never make any assumption on the actual length, using [`nybbles_len`] |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
46 /// if they need a loop boundary. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
47 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
48 /// All methods that create a `Node` either take a type that enforces |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
49 /// the size or fail immediately at runtime with [`ExactLengthRequired`]. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
50 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
51 /// [`nybbles_len`]: #method.nybbles_len |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
52 /// [`ExactLengthRequired`]: struct.NodeError#variant.ExactLengthRequired |
46391
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
53 #[derive(Clone, Debug, PartialEq, BytesCast)] |
44502
166349510398
revlog: using two new functions in C capsule from Rust code
Georges Racinet <georges.racinet@octobus.net>
parents:
44388
diff
changeset
|
54 #[repr(transparent)] |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
55 pub struct Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
56 data: NodeData, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
57 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
58 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
59 /// The node value for NULL_REVISION |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
60 pub const NULL_NODE: Node = Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
61 data: [0; NODE_BYTES_LENGTH], |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
62 }; |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
63 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
64 impl From<NodeData> for Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
65 fn from(data: NodeData) -> Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
66 Node { data } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
67 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
68 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
69 |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
70 /// Return an error if the slice has an unexpected length |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
71 impl<'a> TryFrom<&'a [u8]> for &'a Node { |
46391
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
72 type Error = (); |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
73 |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
74 #[inline] |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
75 fn try_from(bytes: &'a [u8]) -> Result<&'a Node, Self::Error> { |
46391
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
76 match Node::from_bytes(bytes) { |
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
77 Ok((node, rest)) if rest.is_empty() => Ok(node), |
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
78 _ => Err(()), |
cfb6c10c08c2
rust: replace an unsafe use of transmute with a safe use of bytes-cast
Simon Sapin <simon.sapin@octobus.net>
parents:
46033
diff
changeset
|
79 } |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
80 } |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
81 } |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
82 |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
83 #[derive(Debug, PartialEq)] |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
84 pub enum NodeError { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
85 ExactLengthRequired(usize, String), |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
86 PrefixTooLong(String), |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
87 HexError(FromHexError, String), |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
88 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
89 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
90 /// Low level utility function, also for prefixes |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
91 fn get_nybble(s: &[u8], i: usize) -> u8 { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
92 if i % 2 == 0 { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
93 s[i / 2] >> 4 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
94 } else { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
95 s[i / 2] & 0x0f |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
96 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
97 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
98 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
99 impl Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
100 /// Retrieve the `i`th half-byte of the binary data. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
101 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
102 /// This is also the `i`th hexadecimal digit in numeric form, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
103 /// also called a [nybble](https://en.wikipedia.org/wiki/Nibble). |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
104 pub fn get_nybble(&self, i: usize) -> u8 { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
105 get_nybble(&self.data, i) |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
106 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
107 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
108 /// Length of the data, in nybbles |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
109 pub fn nybbles_len(&self) -> usize { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
110 // public exposure as an instance method only, so that we can |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
111 // easily support several sizes of hashes if needed in the future. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
112 NODE_NYBBLES_LENGTH |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
113 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
114 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
115 /// Convert from hexadecimal string representation |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
116 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
117 /// Exact length is required. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
118 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
119 /// To be used in FFI and I/O only, in order to facilitate future |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
120 /// changes of hash format. |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
121 pub fn from_hex(hex: impl AsRef<[u8]>) -> Result<Node, NodeError> { |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
122 Ok(NodeData::from_hex(hex.as_ref()) |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
123 .map_err(|e| NodeError::from((e, hex)))? |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
124 .into()) |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
125 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
126 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
127 /// Convert to hexadecimal string representation |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
128 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
129 /// To be used in FFI and I/O only, in order to facilitate future |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
130 /// changes of hash format. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
131 pub fn encode_hex(&self) -> String { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
132 hex::encode(self.data) |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
133 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
134 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
135 /// Provide access to binary data |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
136 /// |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
137 /// This is needed by FFI layers, for instance to return expected |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
138 /// binary values to Python. |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
139 pub fn as_bytes(&self) -> &[u8] { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
140 &self.data |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
141 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
142 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
143 |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
144 impl<T: AsRef<[u8]>> From<(FromHexError, T)> for NodeError { |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
145 fn from(err_offender: (FromHexError, T)) -> Self { |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
146 let (err, offender) = err_offender; |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
147 let offender = String::from_utf8_lossy(offender.as_ref()).into_owned(); |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
148 match err { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
149 FromHexError::InvalidStringLength => { |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
150 NodeError::ExactLengthRequired(NODE_NYBBLES_LENGTH, offender) |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
151 } |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
152 _ => NodeError::HexError(err, offender), |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
153 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
154 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
155 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
156 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
157 /// The beginning of a binary revision SHA. |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
158 /// |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
159 /// Since it can potentially come from an hexadecimal representation with |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
160 /// odd length, it needs to carry around whether the last 4 bits are relevant |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
161 /// or not. |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
162 #[derive(Debug, PartialEq)] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
163 pub struct NodePrefix { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
164 buf: Vec<u8>, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
165 is_odd: bool, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
166 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
167 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
168 impl NodePrefix { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
169 /// Convert from hexadecimal string representation |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
170 /// |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
171 /// Similarly to `hex::decode`, can be used with Unicode string types |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
172 /// (`String`, `&str`) as well as bytes. |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
173 /// |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
174 /// To be used in FFI and I/O only, in order to facilitate future |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
175 /// changes of hash format. |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
176 pub fn from_hex(hex: impl AsRef<[u8]>) -> Result<Self, NodeError> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
177 let hex = hex.as_ref(); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
178 let len = hex.len(); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
179 if len > NODE_NYBBLES_LENGTH { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
180 return Err(NodeError::PrefixTooLong( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
181 String::from_utf8_lossy(hex).to_owned().to_string(), |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
182 )); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
183 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
184 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
185 let is_odd = len % 2 == 1; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
186 let even_part = if is_odd { &hex[..len - 1] } else { hex }; |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
187 let mut buf: Vec<u8> = |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
188 Vec::from_hex(&even_part).map_err(|e| (e, hex))?; |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
189 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
190 if is_odd { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
191 let latest_char = char::from(hex[len - 1]); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
192 let latest_nybble = latest_char.to_digit(16).ok_or_else(|| { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
193 ( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
194 FromHexError::InvalidHexCharacter { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
195 c: latest_char, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
196 index: len - 1, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
197 }, |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
198 hex, |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
199 ) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
200 })? as u8; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
201 buf.push(latest_nybble << 4); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
202 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
203 Ok(NodePrefix { buf, is_odd }) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
204 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
205 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
206 pub fn borrow(&self) -> NodePrefixRef { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
207 NodePrefixRef { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
208 buf: &self.buf, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
209 is_odd: self.is_odd, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
210 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
211 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
212 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
213 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
214 #[derive(Clone, Debug, PartialEq)] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
215 pub struct NodePrefixRef<'a> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
216 buf: &'a [u8], |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
217 is_odd: bool, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
218 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
219 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
220 impl<'a> NodePrefixRef<'a> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
221 pub fn len(&self) -> usize { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
222 if self.is_odd { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
223 self.buf.len() * 2 - 1 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
224 } else { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
225 self.buf.len() * 2 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
226 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
227 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
228 |
44973
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
229 pub fn is_empty(&self) -> bool { |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
230 self.len() == 0 |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
231 } |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
232 |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
233 pub fn is_prefix_of(&self, node: &Node) -> bool { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
234 if self.is_odd { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
235 let buf = self.buf; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
236 let last_pos = buf.len() - 1; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
237 node.data.starts_with(buf.split_at(last_pos).0) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
238 && node.data[last_pos] >> 4 == buf[last_pos] >> 4 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
239 } else { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
240 node.data.starts_with(self.buf) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
241 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
242 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
243 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
244 /// Retrieve the `i`th half-byte from the prefix. |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
245 /// |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
246 /// This is also the `i`th hexadecimal digit in numeric form, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
247 /// also called a [nybble](https://en.wikipedia.org/wiki/Nibble). |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
248 pub fn get_nybble(&self, i: usize) -> u8 { |
44187
be52b7372ec2
rust-node: avoid meaningless read at the end of odd prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44183
diff
changeset
|
249 assert!(i < self.len()); |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
250 get_nybble(self.buf, i) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
251 } |
44388
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
252 |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
253 /// Return the index first nybble that's different from `node` |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
254 /// |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
255 /// If the return value is `None` that means that `self` is |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
256 /// a prefix of `node`, but the current method is a bit slower |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
257 /// than `is_prefix_of`. |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
258 /// |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
259 /// Returned index is as in `get_nybble`, i.e., starting at 0. |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
260 pub fn first_different_nybble(&self, node: &Node) -> Option<usize> { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
261 let buf = self.buf; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
262 let until = if self.is_odd { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
263 buf.len() - 1 |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
264 } else { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
265 buf.len() |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
266 }; |
44973
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
267 for (i, item) in buf.iter().enumerate().take(until) { |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
268 if *item != node.data[i] { |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
269 return if *item & 0xf0 == node.data[i] & 0xf0 { |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
270 Some(2 * i + 1) |
44388
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
271 } else { |
44973
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
272 Some(2 * i) |
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44502
diff
changeset
|
273 }; |
44388
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
274 } |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
275 } |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
276 if self.is_odd && buf[until] & 0xf0 != node.data[until] & 0xf0 { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
277 Some(until * 2) |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
278 } else { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
279 None |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
280 } |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
281 } |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
282 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
283 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
284 /// A shortcut for full `Node` references |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
285 impl<'a> From<&'a Node> for NodePrefixRef<'a> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
286 fn from(node: &'a Node) -> Self { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
287 NodePrefixRef { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
288 buf: &node.data, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
289 is_odd: false, |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
290 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
291 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
292 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
293 |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
294 impl PartialEq<Node> for NodePrefixRef<'_> { |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
295 fn eq(&self, other: &Node) -> bool { |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
296 !self.is_odd && self.buf == other.data |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
297 } |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
298 } |
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
299 |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
300 #[cfg(test)] |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
301 mod tests { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
302 use super::*; |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
303 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
304 fn sample_node() -> Node { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
305 let mut data = [0; NODE_BYTES_LENGTH]; |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
306 data.copy_from_slice(&[ |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
307 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
308 0x98, 0x76, 0x54, 0x32, 0x10, 0xde, 0xad, 0xbe, 0xef, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
309 ]); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
310 data.into() |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
311 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
312 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
313 /// Pad an hexadecimal string to reach `NODE_NYBBLES_LENGTH` |
46033
88e741bf2d93
rust: use NodePrefix::from_hex instead of hex::decode directly
Simon Sapin <simon-commits@exyr.org>
parents:
45531
diff
changeset
|
314 ///check_hash |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
315 /// The padding is made with zeros |
44183
e52401a95b94
rust-nodemap: NodeMap trait with simplest implementation
Georges Racinet <georges.racinet@octobus.net>
parents:
44182
diff
changeset
|
316 pub fn hex_pad_right(hex: &str) -> String { |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
317 let mut res = hex.to_string(); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
318 while res.len() < NODE_NYBBLES_LENGTH { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
319 res.push('0'); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
320 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
321 res |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
322 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
323 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
324 fn sample_node_hex() -> String { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
325 hex_pad_right("0123456789abcdeffedcba9876543210deadbeef") |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
326 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
327 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
328 #[test] |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
329 fn test_node_from_hex() { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
330 assert_eq!(Node::from_hex(&sample_node_hex()), Ok(sample_node())); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
331 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
332 let mut short = hex_pad_right("0123"); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
333 short.pop(); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
334 short.pop(); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
335 assert_eq!( |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
336 Node::from_hex(&short), |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
337 Err(NodeError::ExactLengthRequired(NODE_NYBBLES_LENGTH, short)), |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
338 ); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
339 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
340 let not_hex = hex_pad_right("012... oops"); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
341 assert_eq!( |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
342 Node::from_hex(¬_hex), |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
343 Err(NodeError::HexError( |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
344 FromHexError::InvalidHexCharacter { c: '.', index: 3 }, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
345 not_hex, |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
346 )), |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
347 ); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
348 } |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
349 |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
350 #[test] |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
351 fn test_node_encode_hex() { |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
352 assert_eq!(sample_node().encode_hex(), sample_node_hex()); |
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
353 } |
44182
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
354 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
355 #[test] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
356 fn test_prefix_from_hex() -> Result<(), NodeError> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
357 assert_eq!( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
358 NodePrefix::from_hex("0e1")?, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
359 NodePrefix { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
360 buf: vec![14, 16], |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
361 is_odd: true |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
362 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
363 ); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
364 assert_eq!( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
365 NodePrefix::from_hex("0e1a")?, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
366 NodePrefix { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
367 buf: vec![14, 26], |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
368 is_odd: false |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
369 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
370 ); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
371 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
372 // checking limit case |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
373 let node_as_vec = sample_node().data.iter().cloned().collect(); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
374 assert_eq!( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
375 NodePrefix::from_hex(sample_node_hex())?, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
376 NodePrefix { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
377 buf: node_as_vec, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
378 is_odd: false |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
379 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
380 ); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
381 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
382 Ok(()) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
383 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
384 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
385 #[test] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
386 fn test_prefix_from_hex_errors() { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
387 assert_eq!( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
388 NodePrefix::from_hex("testgr"), |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
389 Err(NodeError::HexError( |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
390 FromHexError::InvalidHexCharacter { c: 't', index: 0 }, |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
391 "testgr".to_string() |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
392 )) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
393 ); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
394 let mut long = NULL_NODE.encode_hex(); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
395 long.push('c'); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
396 match NodePrefix::from_hex(&long) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
397 .expect_err("should be refused as too long") |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
398 { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
399 NodeError::PrefixTooLong(s) => assert_eq!(s, long), |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
400 err => panic!(format!("Should have been TooLong, got {:?}", err)), |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
401 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
402 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
403 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
404 #[test] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
405 fn test_is_prefix_of() -> Result<(), NodeError> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
406 let mut node_data = [0; NODE_BYTES_LENGTH]; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
407 node_data[0] = 0x12; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
408 node_data[1] = 0xca; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
409 let node = Node::from(node_data); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
410 assert!(NodePrefix::from_hex("12")?.borrow().is_prefix_of(&node)); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
411 assert!(!NodePrefix::from_hex("1a")?.borrow().is_prefix_of(&node)); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
412 assert!(NodePrefix::from_hex("12c")?.borrow().is_prefix_of(&node)); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
413 assert!(!NodePrefix::from_hex("12d")?.borrow().is_prefix_of(&node)); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
414 Ok(()) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
415 } |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
416 |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
417 #[test] |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
418 fn test_get_nybble() -> Result<(), NodeError> { |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
419 let prefix = NodePrefix::from_hex("dead6789cafe")?; |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
420 assert_eq!(prefix.borrow().get_nybble(0), 13); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
421 assert_eq!(prefix.borrow().get_nybble(7), 9); |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
422 Ok(()) |
9896a8d0d3d2
rust-node: handling binary Node prefix
Georges Racinet <georges.racinet@octobus.net>
parents:
44143
diff
changeset
|
423 } |
44388
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
424 |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
425 #[test] |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
426 fn test_first_different_nybble_even_prefix() { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
427 let prefix = NodePrefix::from_hex("12ca").unwrap(); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
428 let prefref = prefix.borrow(); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
429 let mut node = Node::from([0; NODE_BYTES_LENGTH]); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
430 assert_eq!(prefref.first_different_nybble(&node), Some(0)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
431 node.data[0] = 0x13; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
432 assert_eq!(prefref.first_different_nybble(&node), Some(1)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
433 node.data[0] = 0x12; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
434 assert_eq!(prefref.first_different_nybble(&node), Some(2)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
435 node.data[1] = 0xca; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
436 // now it is a prefix |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
437 assert_eq!(prefref.first_different_nybble(&node), None); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
438 } |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
439 |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
440 #[test] |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
441 fn test_first_different_nybble_odd_prefix() { |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
442 let prefix = NodePrefix::from_hex("12c").unwrap(); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
443 let prefref = prefix.borrow(); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
444 let mut node = Node::from([0; NODE_BYTES_LENGTH]); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
445 assert_eq!(prefref.first_different_nybble(&node), Some(0)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
446 node.data[0] = 0x13; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
447 assert_eq!(prefref.first_different_nybble(&node), Some(1)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
448 node.data[0] = 0x12; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
449 assert_eq!(prefref.first_different_nybble(&node), Some(2)); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
450 node.data[1] = 0xca; |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
451 // now it is a prefix |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
452 assert_eq!(prefref.first_different_nybble(&node), None); |
5ac1eecc9c64
rust-nodemap: core implementation for shortest
Georges Racinet <georges.racinet@octobus.net>
parents:
44187
diff
changeset
|
453 } |
44143
7f86426fdd2c
rust-node: binary Node ID and conversion utilities
Georges Racinet <georges.racinet@octobus.net>
parents:
diff
changeset
|
454 } |
44183
e52401a95b94
rust-nodemap: NodeMap trait with simplest implementation
Georges Racinet <georges.racinet@octobus.net>
parents:
44182
diff
changeset
|
455 |
e52401a95b94
rust-nodemap: NodeMap trait with simplest implementation
Georges Racinet <georges.racinet@octobus.net>
parents:
44182
diff
changeset
|
456 #[cfg(test)] |
e52401a95b94
rust-nodemap: NodeMap trait with simplest implementation
Georges Racinet <georges.racinet@octobus.net>
parents:
44182
diff
changeset
|
457 pub use tests::hex_pad_right; |