odra_utils/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3extern crate alloc;
4
5#[cfg(not(feature = "std"))]
6use alloc::string::String;
7
8#[cfg(feature = "std")]
9use convert_case::{Boundary, Case, Casing};
10
11/// Converts a camel-cased &str to String.
12///
13/// # Example
14///
15/// ```
16/// use odra_utils::camel_to_snake;
17///
18/// let camel = "ContractName";
19/// let result = camel_to_snake(camel);
20///
21/// assert_eq!(&result, "contract_name");
22/// ```
23#[cfg(feature = "std")]
24pub fn camel_to_snake(text: &str) -> String {
25    text.from_case(Case::UpperCamel)
26        .without_boundaries(&[Boundary::UpperDigit, Boundary::LowerDigit])
27        .to_case(Case::Snake)
28}
29
30/// Calculates the absolute position of the event. Accepts both positive and negative indexing.
31///
32/// # Examples
33///
34/// ```
35/// use odra_utils::event_absolute_position;
36///
37/// assert_eq!(event_absolute_position(10, 0), Some(0));
38/// assert_eq!(event_absolute_position(10, -1), Some(9));
39/// assert_eq!(event_absolute_position(10, 10), None);
40/// ```
41pub fn event_absolute_position(len: usize, index: i32) -> Option<usize> {
42    if index.is_negative() {
43        let abs_idx = index.wrapping_abs() as usize;
44        if abs_idx > len {
45            return None;
46        }
47        Some(len - abs_idx)
48    } else {
49        if index as usize >= len {
50            return None;
51        }
52        Some(index as usize)
53    }
54}
55
56pub static KEY_DELIMITER: &str = "#";
57
58static TABLE: &[u8] = b"0123456789abcdef";
59
60#[inline]
61fn hex(byte: u8) -> u8 {
62    TABLE[byte as usize]
63}
64
65/// Converts the hexadecimal values from the source byte slice into a more readable form,
66/// representing each byte in hexadecimal form, in the destination byte slice.
67///
68/// * It iterates over the source slice `src` and the destination slice `dst` concurrently.
69/// * For each byte in the source, it calculates the hexadecimal representation.
70/// * It splits the byte into two nibbles (4-bit groups): the higher order 4 bits and the lower order 4 bits.
71/// * It converts each nibble into its corresponding hexadecimal representation.
72/// * It stores the two hexadecimal representations in two consecutive slots of the destination slice.
73///
74/// # Example
75///
76/// ```
77/// # use odra_utils::hex_to_slice;
78///
79/// let mut dst = vec![0; 10];
80/// let src = [255, 254, 253, 252, 251];
81/// hex_to_slice(&src, &mut dst);
82/// assert_eq!(&dst, &[102, 102, 102, 101, 102, 100, 102, 99, 102, 98]);
83/// ```
84pub fn hex_to_slice(src: &[u8], dst: &mut [u8]) {
85    for (byte, slots) in src.iter().zip(dst.chunks_exact_mut(2)) {
86        slots[0] = hex((*byte >> 4) & 0xf);
87        slots[1] = hex(*byte & 0xf);
88    }
89}
90
91/// Joins two parts of a key with the [`KEY_DELIMITER`].
92#[inline]
93pub fn create_key(left: &str, right: &str) -> String {
94    alloc::format!("{}{}{}", left, KEY_DELIMITER, right)
95}
96
97#[cfg(test)]
98mod tests {
99    use crate::{camel_to_snake, event_absolute_position};
100
101    #[test]
102    fn camel_to_snake_works() {
103        assert_eq!("owned_token", camel_to_snake("OwnedToken"));
104        assert_eq!("ownable", camel_to_snake("Ownable"));
105        assert_eq!("erc20", camel_to_snake("Erc20"));
106        assert_eq!(
107            "erc20_implementation",
108            camel_to_snake("Erc20Implementation")
109        );
110    }
111
112    #[test]
113    fn event_absolute_position_works() {
114        assert_eq!(event_absolute_position(0, 1), None);
115        assert_eq!(event_absolute_position(10, 10), None);
116        assert_eq!(event_absolute_position(10, -11), None);
117        assert_eq!(event_absolute_position(10, 0), Some(0));
118        assert_eq!(event_absolute_position(10, 1), Some(1));
119        assert_eq!(event_absolute_position(10, -1), Some(9));
120        assert_eq!(event_absolute_position(10, -2), Some(8));
121        assert_eq!(event_absolute_position(10, -10), Some(0));
122    }
123}