MoosicBox Assert
A conditional assertion library providing enhanced assertion macros with colorized output and stack traces, controlled by environment variables.
Features
- Conditional Assertions: Enable/disable assertions via environment variable
- Colorized Output: Red background with white text for assertion failures
- Stack Traces: Automatic backtrace capture on assertion failures
- Multiple Assert Types: Different assertion behaviors (exit, error, panic, unimplemented)
- Flexible Error Handling: Convert assertions to errors, warnings, or panics
- Environment Control: Runtime control over assertion behavior
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
= "0.4.29" # Required by exported macros from this crate (for example assert!/die!/assert_or_error!/die_or_error!/die_or_warn!)
Usage
Basic Assertions
use ;
Assert with Error Return
use assert_or_err;
Assert with Logging
use assert_or_error;
Assert with Panic
use assert_or_panic;
Assert with Unimplemented
use assert_or_unimplemented;
Environment Control
use assert;
Assertion Types
assert!(condition [, message])
Exits the process with colored output when condition fails and assertions are enabled.
assert_or_err!(condition, error [, message])
Returns the error when condition fails, or exits with assertion if ENABLE_ASSERT=1.
assert_or_error!(condition, message)
Logs an error when condition fails, or exits with assertion if ENABLE_ASSERT=1.
assert_or_panic!(condition [, message])
Panics with colored output when condition fails, or exits with assertion if ENABLE_ASSERT=1.
assert_or_unimplemented!(condition [, message])
Calls unimplemented!() when condition fails, or exits with assertion if ENABLE_ASSERT=1.
die!([message])
Unconditionally exits with colored output when assertions are enabled.
Additional Macros
The library also provides additional utility macros for specialized use cases:
die_or_warn!(message): Exits with colored output ifENABLE_ASSERT=1, otherwise logs a warningdie_or_err!(error, message): Exits ifENABLE_ASSERT=1, otherwise returns the errordie_or_error!(message): Exits ifENABLE_ASSERT=1, otherwise logs an errordie_or_propagate!(result [, message]): Exits ifENABLE_ASSERT=1and result is error, otherwise propagates error with?die_or_panic!(message): Exits ifENABLE_ASSERT=1, otherwise panicsdie_or_unimplemented!(message): Exits ifENABLE_ASSERT=1, otherwise callsunimplemented!()
Environment Variables
ENABLE_ASSERT: Set to "1" to enable assertions, any other value disables them
Output Format
When assertions fail, the output includes:
- Red background with white text for visibility
- Bold and underlined formatting
- Full stack trace showing the failure location
- Custom messages with formatting support
Dependencies
colored: For colorized terminal outputmoosicbox_env_utils: For environment variable handling- Standard library backtrace support
Use Cases
- Development: Enable detailed assertion checking
- Testing: Verify preconditions and postconditions
- Production: Disable assertions for performance
- Debugging: Get detailed failure information with stack traces