better_assert/
lib.rs

1// better-assert/src/lib.rs
2
3// cargo build --features native
4// cargo build --release --features native
5// cargo build --target wasm32-unknown-unknown --features wasm
6// cargo build --release --target wasm32-unknown-unknown --features wasm
7
8#[cfg(any(feature = "native", feature = "wasm"))]
9use better_logger::logger;
10#[cfg(any(feature = "native", feature = "wasm"))]
11use std::fmt::Debug;
12#[cfg(any(feature = "native", feature = "wasm"))]
13use std::panic::Location;
14#[cfg(any(feature = "native", feature = "wasm"))]
15use std::any::type_name;
16
17///0
18///1
19///2
20///3
21///4
22///5
23///6
24///7
25///8
26///9
27
28#[cfg(any(feature = "native", feature = "wasm"))]
29#[track_caller]
30pub fn log_assert_eq<G>(left: &G, right: &G) 
31where
32    G: PartialEq + Debug,
33{
34    if !(left == right) {
35        let location = Location::caller();
36        let type_of_g = type_name::<G>();
37        logger::error!(r#"Assertion Failed: left "{:?}" != right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
38        panic!(r#"Assertion Failed: left "{:?}" != right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
39    }
40}
41
42#[cfg(any(feature = "native", feature = "wasm"))]
43#[track_caller]
44pub fn log_assert_ne<G>(left: &G, right: &G) 
45where
46    G: PartialEq + Debug,
47{
48    if !(left != right) {
49        let location = Location::caller();
50        let type_of_g = type_name::<G>();
51        logger::error!(r#"Assertion Failed: left "{:?}" == right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
52        panic!(r#"Assertion Failed: left "{:?}" == right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
53    }
54}
55
56///0
57///1
58///2
59///3
60///4
61///5
62///6
63///7
64///8
65///9
66
67#[cfg(any(feature = "native", feature = "wasm"))]
68#[cfg(debug_assertions)]
69#[track_caller]
70pub fn debug_log_assert_eq<G>(left: &G, right: &G) 
71where
72    G: PartialEq + Debug,
73{
74    if !(left == right) {
75        let location = Location::caller();
76        let type_of_g = type_name::<G>();
77        logger::error!(r#"Assertion Failed: left "{:?}" != right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
78        panic!(r#"Assertion Failed: left "{:?}" != right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
79    }
80}
81
82#[cfg(any(feature = "native", feature = "wasm"))]
83#[cfg(not(debug_assertions))]
84#[track_caller]
85pub fn debug_log_assert_eq<G>(_left: &G, _right: &G) 
86where
87    G: PartialEq + Debug,
88{
89
90}
91
92#[cfg(any(feature = "native", feature = "wasm"))]
93#[cfg(debug_assertions)]
94#[track_caller]
95pub fn debug_log_assert_ne<G>(left: &G, right: &G) 
96where
97    G: PartialEq + Debug,
98{
99    if !(left != right) {
100        let location = Location::caller();
101        let type_of_g = type_name::<G>();
102        logger::error!(r#"Assertion Failed: left "{:?}" == right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
103        panic!(r#"Assertion Failed: left "{:?}" == right "{:?}" (Location: {}:{}:{}) (Type: {})"#, left, right, location.file(), location.line(), location.column(), type_of_g);
104    }
105}
106
107#[cfg(any(feature = "native", feature = "wasm"))]
108#[cfg(not(debug_assertions))]
109#[track_caller]
110pub fn debug_log_assert_ne<G>(_left: &G, _right: &G) 
111where
112    G: PartialEq + Debug,
113{
114
115}
116
117///0
118///1
119///2
120///3
121///4
122///5
123///6
124///7
125///8
126///9
127
128#[cfg(any(feature = "native", feature = "wasm"))]
129#[track_caller]
130pub fn log_panic() {
131    let location = Location::caller();
132    logger::error!(r#"Panic (Location: {}:{}:{})"#, location.file(), location.line(), location.column());
133    panic!(r#"Panic (Location: {}:{}:{})"#, location.file(), location.line(), location.column());
134}