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.1"
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.
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