hopr_primitive_types/
lib.rs1pub mod balance;
6pub mod bounded;
8pub mod errors;
10pub mod primitives;
13pub mod traits;
15
16pub fn f64_approx_eq(a: f64, b: f64, epsilon: f64) -> bool {
23 float_cmp::ApproxEq::approx_eq(a, b, (epsilon, 2))
24}
25
26pub 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}