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