diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-11-01 17:17:24 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-11-01 17:17:24 +0100 |
| commit | 5e8608c7a5b1e4e92f1fb650ea0ef47e5a6df563 (patch) | |
| tree | f731a0329d92d8fae4168957f8e90716b58ad00b /anyfmt | |
| parent | 2c13e251849fd3e3cf06e2d5363c311ac880c77d (diff) | |
Make defmt optional with new `anyfmt` crate
Diffstat (limited to 'anyfmt')
| -rw-r--r-- | anyfmt/Cargo.toml | 10 | ||||
| -rw-r--r-- | anyfmt/src/lib.rs | 149 |
2 files changed, 159 insertions, 0 deletions
diff --git a/anyfmt/Cargo.toml b/anyfmt/Cargo.toml new file mode 100644 index 000000000..3ee7c167b --- /dev/null +++ b/anyfmt/Cargo.toml | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [package] | ||
| 2 | name = "anyfmt" | ||
| 3 | version = "0.1.0" | ||
| 4 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 5 | edition = "2018" | ||
| 6 | |||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | defmt = { version = "0.1.0", optional = true } | ||
| 10 | log = { version = "0.4.11", optional = true } | ||
diff --git a/anyfmt/src/lib.rs b/anyfmt/src/lib.rs new file mode 100644 index 000000000..3286cc27b --- /dev/null +++ b/anyfmt/src/lib.rs | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | #![no_std] | ||
| 2 | |||
| 3 | pub mod export { | ||
| 4 | #[cfg(feature = "defmt")] | ||
| 5 | pub use defmt; | ||
| 6 | #[cfg(feature = "log")] | ||
| 7 | pub use log; | ||
| 8 | } | ||
| 9 | |||
| 10 | #[cfg(feature = "log")] | ||
| 11 | #[macro_export] | ||
| 12 | macro_rules! log { | ||
| 13 | (trace, $($arg:expr),*) => { $crate::export::log::trace!($($arg),*); }; | ||
| 14 | (debug, $($arg:expr),*) => { $crate::export::log::debug!($($arg),*); }; | ||
| 15 | (info, $($arg:expr),*) => { $crate::export::log::info!($($arg),*); }; | ||
| 16 | (warn, $($arg:expr),*) => { $crate::export::log::warn!($($arg),*); }; | ||
| 17 | (error, $($arg:expr),*) => { $crate::export::log::error!($($arg),*); }; | ||
| 18 | } | ||
| 19 | |||
| 20 | #[cfg(feature = "defmt")] | ||
| 21 | #[macro_export] | ||
| 22 | macro_rules! log { | ||
| 23 | (trace, $($arg:expr),*) => { $crate::export::defmt::trace!($($arg),*); }; | ||
| 24 | (debug, $($arg:expr),*) => { $crate::export::defmt::debug!($($arg),*); }; | ||
| 25 | (info, $($arg:expr),*) => { $crate::export::defmt::info!($($arg),*); }; | ||
| 26 | (warn, $($arg:expr),*) => { $crate::export::defmt::warn!($($arg),*); }; | ||
| 27 | (error, $($arg:expr),*) => { $crate::export::defmt::error!($($arg),*); }; | ||
| 28 | } | ||
| 29 | |||
| 30 | #[cfg(not(any(feature = "log", feature = "defmt")))] | ||
| 31 | #[macro_export] | ||
| 32 | macro_rules! log { | ||
| 33 | ($level:ident, $($arg:expr),*) => {{}}; | ||
| 34 | } | ||
| 35 | |||
| 36 | #[macro_export] | ||
| 37 | macro_rules! trace { | ||
| 38 | ($($arg:expr),*) => (log!(trace, $($arg),*)); | ||
| 39 | } | ||
| 40 | |||
| 41 | #[macro_export] | ||
| 42 | macro_rules! debug { | ||
| 43 | ($($arg:expr),*) => ($crate::log!(debug, $($arg),*)); | ||
| 44 | } | ||
| 45 | |||
| 46 | #[macro_export] | ||
| 47 | macro_rules! info { | ||
| 48 | ($($arg:expr),*) => ($crate::log!(info, $($arg),*)); | ||
| 49 | } | ||
| 50 | |||
| 51 | #[macro_export] | ||
| 52 | macro_rules! warn { | ||
| 53 | ($($arg:expr),*) => ($crate::log!(warn, $($arg),*)); | ||
| 54 | } | ||
| 55 | |||
| 56 | #[macro_export] | ||
| 57 | macro_rules! error { | ||
| 58 | ($($arg:expr),*) => ($crate::log!(error, $($arg),*)); | ||
| 59 | } | ||
| 60 | |||
| 61 | #[macro_export] | ||
| 62 | macro_rules! expect { | ||
| 63 | ($arg:expr, $msg:expr) => { | ||
| 64 | match $crate::Try::into_result($arg) { | ||
| 65 | ::core::result::Result::Ok(t) => t, | ||
| 66 | ::core::result::Result::Err(e) => { | ||
| 67 | $crate::panic!("{:?}: {:?}", $crate::intern!($msg), e); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | |||
| 73 | #[cfg(feature = "defmt")] | ||
| 74 | #[macro_export] | ||
| 75 | macro_rules! intern { | ||
| 76 | ($arg:expr) => { | ||
| 77 | $crate::export::defmt::intern!($arg) | ||
| 78 | }; | ||
| 79 | } | ||
| 80 | |||
| 81 | #[cfg(not(feature = "defmt"))] | ||
| 82 | #[macro_export] | ||
| 83 | macro_rules! intern { | ||
| 84 | ($arg:expr) => { | ||
| 85 | $arg | ||
| 86 | }; | ||
| 87 | } | ||
| 88 | |||
| 89 | #[macro_export] | ||
| 90 | macro_rules! unwrap { | ||
| 91 | ($arg:expr) => { | ||
| 92 | expect!($arg, "Unwrap failed") | ||
| 93 | }; | ||
| 94 | } | ||
| 95 | |||
| 96 | #[macro_export] | ||
| 97 | macro_rules! panic { | ||
| 98 | () => { | ||
| 99 | $crate::panic!("panic") | ||
| 100 | }; | ||
| 101 | ($($arg:expr),*) => {{ | ||
| 102 | $crate::log!(error, $($arg),*); | ||
| 103 | ::core::panic!() | ||
| 104 | }}; | ||
| 105 | } | ||
| 106 | |||
| 107 | #[macro_export] | ||
| 108 | macro_rules! assert { | ||
| 109 | ($cond:expr) => { | ||
| 110 | $crate::assert!($cond, "assertion failed"); | ||
| 111 | }; | ||
| 112 | ($cond:expr, $($arg:expr),*) => { | ||
| 113 | { | ||
| 114 | if !$cond { | ||
| 115 | $crate::panic!($($arg),*); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
| 122 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 123 | pub struct NoneError; | ||
| 124 | |||
| 125 | pub trait Try { | ||
| 126 | type Ok; | ||
| 127 | type Error; | ||
| 128 | fn into_result(self) -> Result<Self::Ok, Self::Error>; | ||
| 129 | } | ||
| 130 | |||
| 131 | impl<T> Try for Option<T> { | ||
| 132 | type Ok = T; | ||
| 133 | type Error = NoneError; | ||
| 134 | |||
| 135 | #[inline] | ||
| 136 | fn into_result(self) -> Result<T, NoneError> { | ||
| 137 | self.ok_or(NoneError) | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | impl<T, E> Try for Result<T, E> { | ||
| 142 | type Ok = T; | ||
| 143 | type Error = E; | ||
| 144 | |||
| 145 | #[inline] | ||
| 146 | fn into_result(self) -> Self { | ||
| 147 | self | ||
| 148 | } | ||
| 149 | } | ||
