macros.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #[cfg(not(test))]
  2. #[cfg(feature = "log")]
  3. macro_rules! net_log {
  4. (trace, $($arg:expr),*) => { log::trace!($($arg),*) };
  5. (debug, $($arg:expr),*) => { log::debug!($($arg),*) };
  6. }
  7. #[cfg(test)]
  8. #[cfg(feature = "log")]
  9. macro_rules! net_log {
  10. (trace, $($arg:expr),*) => { println!($($arg),*) };
  11. (debug, $($arg:expr),*) => { println!($($arg),*) };
  12. }
  13. #[cfg(feature = "defmt")]
  14. macro_rules! net_log {
  15. (trace, $($arg:expr),*) => { defmt::trace!($($arg),*) };
  16. (debug, $($arg:expr),*) => { defmt::debug!($($arg),*) };
  17. }
  18. #[cfg(not(any(feature = "log", feature = "defmt")))]
  19. macro_rules! net_log {
  20. ($level:ident, $($arg:expr),*) => {{ $( let _ = $arg; )* }}
  21. }
  22. macro_rules! net_trace {
  23. ($($arg:expr),*) => (net_log!(trace, $($arg),*));
  24. }
  25. macro_rules! net_debug {
  26. ($($arg:expr),*) => (net_log!(debug, $($arg),*));
  27. }
  28. macro_rules! enum_with_unknown {
  29. (
  30. $( #[$enum_attr:meta] )*
  31. pub enum $name:ident($ty:ty) {
  32. $(
  33. $( #[$variant_attr:meta] )*
  34. $variant:ident = $value:expr
  35. ),+ $(,)?
  36. }
  37. ) => {
  38. #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
  39. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  40. $( #[$enum_attr] )*
  41. pub enum $name {
  42. $(
  43. $( #[$variant_attr] )*
  44. $variant
  45. ),*,
  46. Unknown($ty)
  47. }
  48. impl ::core::convert::From<$ty> for $name {
  49. fn from(value: $ty) -> Self {
  50. match value {
  51. $( $value => $name::$variant ),*,
  52. other => $name::Unknown(other)
  53. }
  54. }
  55. }
  56. impl ::core::convert::From<$name> for $ty {
  57. fn from(value: $name) -> Self {
  58. match value {
  59. $( $name::$variant => $value ),*,
  60. $name::Unknown(other) => other
  61. }
  62. }
  63. }
  64. }
  65. }