embedded_test/
lib.rs

1// Copied from https://github.com/knurling-rs/defmt/blob/main/firmware/defmt-test/src/lib.rs
2
3#![no_std]
4#![allow(clippy::needless_doctest_main)]
5#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
6
7mod fmt;
8
9pub use embedded_test_macros::tests;
10
11#[cfg(feature = "panic-handler")]
12#[panic_handler]
13fn panic(info: &core::panic::PanicInfo) -> ! {
14    error!("====================== PANIC ======================");
15
16    #[cfg(not(feature = "defmt"))]
17    error!("{}", info);
18
19    #[cfg(feature = "defmt")]
20    error!("{}", defmt::Display2Format(info));
21
22    semihosting::process::abort()
23}
24
25/// Private implementation details used by the proc macro.
26/// WARNING: This API is not stable and may change at any time.
27#[doc(hidden)]
28pub mod export;
29
30mod sealed {
31    pub trait Sealed {}
32    impl Sealed for () {}
33    impl<T, E> Sealed for Result<T, E> {}
34}
35
36/// Indicates whether a test succeeded or failed.
37///
38/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
39/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
40/// where `Ok` indicates success.
41#[cfg(feature = "defmt")]
42pub trait TestOutcome: defmt::Format + sealed::Sealed {
43    fn is_success(&self) -> bool;
44}
45
46/// Indicates whether a test succeeded or failed.
47///
48/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
49/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
50/// where `Ok` indicates success.
51#[cfg(feature = "log")]
52pub trait TestOutcome: core::fmt::Debug + sealed::Sealed {
53    fn is_success(&self) -> bool;
54}
55
56/// Indicates whether a test succeeded or failed.
57///
58/// This is comparable to the `Termination` trait in libstd, except stable and tailored towards the
59/// needs of embedded-test. It is implemented for `()`, which always indicates success, and `Result`,
60/// where `Ok` indicates success.
61#[cfg(all(not(feature = "log"), not(feature = "defmt")))]
62pub trait TestOutcome: sealed::Sealed {
63    fn is_success(&self) -> bool;
64}
65
66impl TestOutcome for () {
67    fn is_success(&self) -> bool {
68        true
69    }
70}
71
72#[cfg(feature = "log")]
73impl<T: core::fmt::Debug, E: core::fmt::Debug> TestOutcome for Result<T, E> {
74    fn is_success(&self) -> bool {
75        self.is_ok()
76    }
77}
78
79#[cfg(feature = "defmt")]
80impl<T: defmt::Format, E: defmt::Format> TestOutcome for Result<T, E> {
81    fn is_success(&self) -> bool {
82        self.is_ok()
83    }
84}
85
86#[cfg(all(not(feature = "log"), not(feature = "defmt")))]
87impl<T, E> TestOutcome for Result<T, E> {
88    fn is_success(&self) -> bool {
89        self.is_ok()
90    }
91}