android_logcat/
expect_log.rs

1use std::fmt::Debug;
2
3use crate::logcat::Log;
4
5pub trait ExpectLogcat<T> {
6    /// Expect with Log.e
7    /// If the Result is Err, log the error message using Log.e and panic with the same message.
8    /// If the Result is Ok, return the value.
9    fn expect_log(self, msg: &str) -> T;
10}
11
12/// Implement ExpectLogcat for Result<T, E> where E: Debug
13impl<T, E: Debug> ExpectLogcat<T> for Result<T, E> {
14    fn expect_log(self, msg: &str) -> T {
15        match self {
16            Ok(v) => v,
17            Err(e) => {
18                Log::e(&format!("{}: {:?}", msg, e));
19                panic!("{}: {:?}", msg, e);
20            }
21        }
22    }
23}
24
25/// Implement ExpectLogcat for Option<T> to handle None case
26impl<T> ExpectLogcat<T> for Option<T> {
27    fn expect_log(self, msg: &str) -> T {
28        match self {
29            Some(v) => v,
30            None => {
31                Log::e(msg);
32                panic!("{}", msg);
33            }
34        }
35    }
36}