hopr_primitive_types/
lib.rs

1//! This crate contains basic types used throughout the entire HOPR codebase.
2//! Types from this crate are not necessarily specific only to HOPR.
3
4/// Contains implementations for the token and coin balance types.
5pub mod balance;
6/// Contains various size-bounded types
7pub mod bounded;
8/// Lists all errors in this crate.
9pub mod errors;
10/// Implements the most primitive types, such as [U256](crate::primitives::U256) or
11/// [Address](crate::primitives::Address).
12pub mod primitives;
13/// Defines commonly used traits across the entire code base.
14pub mod traits;
15
16/// Approximately compares two double-precision floats.
17///
18/// This function first tests if the two values relatively differ by at least `epsilon`.
19/// In case they are equal, the second test checks if they differ by at least two representable
20/// units of precision - meaning there can be only two other floats represented in between them.
21/// If both tests pass, the two values are considered (approximately) equal.
22pub fn f64_approx_eq(a: f64, b: f64, epsilon: f64) -> bool {
23    float_cmp::ApproxEq::approx_eq(a, b, (epsilon, 2))
24}
25
26/// Converts the given `data` into a hex string, removing the middle part of the string if it is
27/// longer than `M` of hex characters.
28///
29/// The returned string is at most `M` characters-long.
30pub fn to_hex_shortened<const M: usize>(data: &impl AsRef<[u8]>) -> String {
31    let num_chars = M.max(4);
32    let data = data.as_ref();
33    if data.len() * 2 > M {
34        format!(
35            "{}..{}",
36            hex::encode(&data[0..(num_chars - 2) / 4]),
37            hex::encode(&data[data.len() - (num_chars - 2) / 4..])
38        )
39    } else {
40        hex::encode(data)
41    }
42}
43
44pub mod prelude {
45    pub use chrono::{DateTime, Utc};
46
47    pub use super::{balance::*, errors::GeneralError, f64_approx_eq, primitives::*, to_hex_shortened, traits::*};
48}
49
50#[cfg(test)]
51mod tests {
52    use hex_literal::hex;
53
54    use super::*;
55
56    #[test]
57    fn test_to_hex_shortened() {
58        assert_eq!(&to_hex_shortened::<0>(&hex!("deadbeefcafe")), "..");
59        assert_eq!(&to_hex_shortened::<1>(&hex!("deadbeefcafe")), "..");
60        assert_eq!(&to_hex_shortened::<2>(&hex!("deadbeefcafe")), "..");
61        assert_eq!(&to_hex_shortened::<3>(&hex!("deadbeefcafe")), "..");
62        assert_eq!(&to_hex_shortened::<4>(&hex!("deadbeefcafe")), "..");
63        assert_eq!(&to_hex_shortened::<5>(&hex!("deadbeefcafe")), "..");
64        assert_eq!(&to_hex_shortened::<6>(&hex!("deadbeefcafe")), "de..fe");
65        assert_eq!(&to_hex_shortened::<7>(&hex!("deadbeefcafe")), "de..fe");
66        assert_eq!(&to_hex_shortened::<8>(&hex!("deadbeefcafe")), "de..fe");
67        assert_eq!(&to_hex_shortened::<9>(&hex!("deadbeefcafe")), "de..fe");
68        assert_eq!(&to_hex_shortened::<10>(&hex!("deadbeefcafe")), "dead..cafe");
69        assert_eq!(&to_hex_shortened::<11>(&hex!("deadbeefcafe")), "dead..cafe");
70        assert_eq!(&to_hex_shortened::<12>(&hex!("deadbeefcafe")), "deadbeefcafe");
71    }
72}