From 1d64036c27c179e18ce6b4229599a65c0c59639e Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Wed, 7 Feb 2024 15:38:03 +0100 Subject: [PATCH] Move macro to zeitgeist-macros and add examples --- docs/STYLE_GUIDE.md | 2 +- macros/src/lib.rs | 58 +++++++++++++++++++++++++++++++++++ primitives/src/lib.rs | 1 - primitives/src/macros.rs | 66 ---------------------------------------- 4 files changed, 59 insertions(+), 68 deletions(-) delete mode 100644 primitives/src/macros.rs diff --git a/docs/STYLE_GUIDE.md b/docs/STYLE_GUIDE.md index 00225be19..4974fb753 100644 --- a/docs/STYLE_GUIDE.md +++ b/docs/STYLE_GUIDE.md @@ -161,7 +161,7 @@ duplicating documentation. ```rust // In code logic - zeitgeist_primitives::unreachable_non_terminating!(condition, log_target, message) + zeitgeist_macros::unreachable_non_terminating!(condition, log_target, message) ``` ```rust diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 783c292e7..2534ff00f 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -32,3 +32,61 @@ macro_rules! create_b_tree_map { [$(($key, $value),)*].iter().cloned().collect::>() } } + +/// This macro does ensure that a condition `$condition` is met, and if it is not met +/// it will log a message `$message` with optional message arguments `message_args` to +/// an optional log target `$log_target`, cause an assertion in a test environment +/// and execute some optional extra code. +/// +/// ```ignore +/// // Examples: +/// unreachable_non_terminating!(a == b, "a does not equal b"); +/// unreachable_non_terminating!(a == b, log_target, "a does not equal b"); +/// unreachable_non_terminating!(a == b, "{:?} != {:?}", a, b); +/// unreachable_non_terminating!(a == b, log_target, "{:?} != {:?}", a, b); +/// ``` +#[macro_export] +macro_rules! unreachable_non_terminating { + ($condition: expr, $message: literal, $($message_args: tt)*) => { + let message = format!($message, $($message_args)*); + + #[cfg(test)] + assert!($condition, "{}", message); + + if !$condition { + log::warn!("{}", message); + } + }; + ($condition: expr, $log_target: ident, $message: literal, $($message_args: tt)*) => { + let message = format!($message, $($message_args)*); + + #[cfg(test)] + assert!($condition, "{}", message); + + if !$condition { + log::warn!(target: $log_target, "{}", message); + } + }; + ($condition: expr, $extra_code: expr, $message: literal, $($message_args: tt)*) => { + let message = format!($message, $($message_args)*); + + #[cfg(test)] + assert!($condition, "{}", message); + + if !$condition { + log::warn!("{}", message); + $extra_code; + } + }; + ($condition: expr, $log_target: ident, $extra_code: expr, $message: literal, $($message_args: tt)*) => { + let message = format!($message, $($message_args)*); + + #[cfg(test)] + assert!($condition, "{}", message); + + if !$condition { + log::warn!(target: $log_target, "{}", message); + $extra_code; + } + }; +} diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 29570b41f..3c571da82 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -22,7 +22,6 @@ extern crate alloc; mod asset; pub mod constants; -pub mod macros; mod market; pub mod math; mod max_runtime_usize; diff --git a/primitives/src/macros.rs b/primitives/src/macros.rs deleted file mode 100644 index f4b4c04b4..000000000 --- a/primitives/src/macros.rs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2024 Forecasting Technologies LTD. -// -// This file is part of Zeitgeist. -// -// Zeitgeist is free software: you can redistribute it and/or modify it -// under the terms of the GNU General Public License as published by the -// Free Software Foundation, either version 3 of the License, or (at -// your option) any later version. -// -// Zeitgeist is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with Zeitgeist. If not, see . - -/// This macro does ensure that a condition `$condition` is met, and if it is not met -/// it will log a message `$message` with optional message arguments `message_args` to -/// an optional log target `$log_target`, cause an assertion in a test environment -/// and execute some optional extra code. -#[macro_export] -macro_rules! unreachable_non_terminating { - ($condition: expr, $message: literal, $($message_args: tt)*) => { - let message = format!($message, $($message_args)*); - - #[cfg(test)] - assert!($condition, "{}", message); - - if !$condition { - log::warn!("{}", message); - } - }; - ($condition: expr, $log_target: ident, $message: literal, $($message_args: tt)*) => { - let message = format!($message, $($message_args)*); - - #[cfg(test)] - assert!($condition, "{}", message); - - if !$condition { - log::warn!(target: $log_target, "{}", message); - } - }; - ($condition: expr, $extra_code: expr, $message: literal, $($message_args: tt)*) => { - let message = format!($message, $($message_args)*); - - #[cfg(test)] - assert!($condition, "{}", message); - - if !$condition { - log::warn!("{}", message); - $extra_code; - } - }; - ($condition: expr, $log_target: ident, $extra_code: expr, $message: literal, $($message_args: tt)*) => { - let message = format!($message, $($message_args)*); - - #[cfg(test)] - assert!($condition, "{}", message); - - if !$condition { - log::warn!(target: $log_target, "{}", message); - $extra_code; - } - }; -}