odra_core/utils.rs
1//! General purpose utilities.
2
3use crate::casper_types::bytesrepr::{Bytes, FromBytes};
4use crate::error::EventError;
5use crate::prelude::*;
6use casper_event_standard::casper_types::bytesrepr::ToBytes;
7
8/// Serializes a value implementing the `ToBytes` trait into a `Bytes` object.
9///
10/// # Arguments
11///
12/// * `value` - The value to be serialized.
13///
14/// # Returns
15///
16/// Returns a `Bytes` object containing the serialized representation of the value.
17///
18/// # Panics
19///
20/// Panics if serialization fails.
21pub fn serialize<T: ToBytes>(value: &T) -> Bytes {
22 Bytes::from(value.to_bytes().expect("Couldn't serialize"))
23}
24
25/// Returns the name of the passed event.
26///
27/// # Arguments
28///
29/// * `bytes` - The byte slice containing the event.
30///
31/// # Returns
32///
33/// Returns the name of the event as a `String`.
34///
35/// # Errors
36///
37/// Returns an `EventError` if the name extraction fails or the event name is unexpected.
38pub(crate) fn extract_event_name(bytes: &[u8]) -> Result<String, EventError> {
39 let name: String = FromBytes::from_bytes(bytes)
40 .map_err(|_| EventError::CouldntExtractName)?
41 .0;
42 name.strip_prefix("event_")
43 .map(|s| s.to_string())
44 .ok_or(EventError::UnexpectedType(name))
45}
46
47/// Calculates the absolute position of the event. Accepts both positive and negative indexing.
48///
49/// # Examples
50///
51/// ```
52/// # use odra_core::utils::event_absolute_position;
53///
54/// assert_eq!(event_absolute_position(10, 0), Some(0));
55/// assert_eq!(event_absolute_position(10, -1), Some(9));
56/// assert_eq!(event_absolute_position(10, 10), None);
57/// ```
58pub fn event_absolute_position(len: u32, index: i32) -> Option<u32> {
59 if index.is_negative() {
60 let abs_idx = index.wrapping_abs();
61 if abs_idx > len as i32 {
62 return None;
63 }
64 Some(
65 len.checked_sub(abs_idx as u32)
66 .expect("Checked sub failed, it shouldn't happen")
67 )
68 } else {
69 if index >= len as i32 {
70 return None;
71 }
72 Some(index as u32)
73 }
74}
75
76static TABLE: &[u8] = b"0123456789abcdef";
77
78#[inline]
79fn hex(byte: u8) -> u8 {
80 TABLE[byte as usize]
81}
82
83/// Converts the hexadecimal values from the source byte slice into a more readable form,
84/// representing each byte in hexadecimal form, in the destination byte slice.
85///
86/// * It iterates over the source slice `src` and the destination slice `dst` concurrently.
87/// * For each byte in the source, it calculates the hexadecimal representation.
88/// * It splits the byte into two nibbles (4-bit groups): the higher order 4 bits and the lower order 4 bits.
89/// * It converts each nibble into its corresponding hexadecimal representation.
90/// * It stores the two hexadecimal representations in two consecutive slots of the destination slice.
91///
92/// # Example
93///
94/// ```
95/// # use odra_core::utils::hex_to_slice;
96///
97/// let mut dst = vec![0; 10];
98/// let src = [255, 254, 253, 252, 251];
99/// hex_to_slice(&src, &mut dst);
100/// assert_eq!(&dst, &[102, 102, 102, 101, 102, 100, 102, 99, 102, 98]);
101/// ```
102pub fn hex_to_slice(src: &[u8], dst: &mut [u8]) {
103 for (byte, slots) in src.iter().zip(dst.chunks_exact_mut(2)) {
104 slots[0] = hex((*byte >> 4) & 0xf);
105 slots[1] = hex(*byte & 0xf);
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::event_absolute_position;
112
113 #[test]
114 fn event_absolute_position_works() {
115 assert_eq!(event_absolute_position(0, 1), None);
116 assert_eq!(event_absolute_position(10, 10), None);
117 assert_eq!(event_absolute_position(10, -11), None);
118 assert_eq!(event_absolute_position(10, 0), Some(0));
119 assert_eq!(event_absolute_position(10, 1), Some(1));
120 assert_eq!(event_absolute_position(10, -1), Some(9));
121 assert_eq!(event_absolute_position(10, -2), Some(8));
122 assert_eq!(event_absolute_position(10, -10), Some(0));
123 }
124}