android_logcat/
expect_log.rs

1use std::fmt::Debug;
2
3use crate::android_log::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<S>(self, msg: S) -> T
10    where
11        S: AsRef<str>;
12}
13
14/// Implement ExpectLogcat for Result<T, E> where E: Debug
15impl<T, E: Debug> ExpectLogcat<T> for Result<T, E> {
16    fn expect_log<S>(self, msg: S) -> T
17    where
18        S: AsRef<str>,
19    {
20        match self {
21            Ok(v) => v,
22            Err(e) => {
23                Log::e(&format!("{}: {:?}", msg.as_ref(), e));
24                panic!("{}: {:?}", msg.as_ref(), e);
25            }
26        }
27    }
28}
29
30/// Implement ExpectLogcat for Option<T> to handle None case
31impl<T> ExpectLogcat<T> for Option<T> {
32    fn expect_log<S>(self, msg: S) -> T
33    where
34        S: AsRef<str>,
35    {
36        match self {
37            Some(v) => v,
38            None => {
39                Log::e(msg.as_ref());
40                panic!("{}", msg.as_ref());
41            }
42        }
43    }
44}