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