Skip to main content

suicide_rs/
lib.rs

1//! # suicide-rs
2//! 
3//! This crate allows you to crash a program with fancy output. That's it.
4//! 
5//! 
6#![no_std]
7#[doc(hidden)]
8pub mod internal;
9
10/// Abort program with fancy output
11///
12/// Like panic, but automatically sets the errno to the code and exits with extra information for your bittersweet pleasure
13/// You can thank me later.
14///
15/// Internally, this macro is a wrapper for a function that uses
16/// `tinyvec_string::tinystring::TinyString<A>` internally, allowing for a passed
17/// string to be heap-allocated up to 32 characters long, before falling back to the heap.
18/// 
19/// # Usage
20/// ```rust
21/// extern crate suicide_rs; 
22///
23/// // Syntax: die!(i32, &str, format_args!(&str, ...));
24/// 
25/// let Errno: linux_errnos::x86_64::Errno = linux_errnos::x86_64::Errno::EINVAL; // This example uses the linux_errnos crate, but function uses raw i32, so any implementation will work
26/// let Colour: &str = inline_colorization::color_red; // This example uses the inline_colorization crate, but function uses &str, so any implementation will work
27/// let Msg: &str = "It is good day to be not dead!";
28/// suicide_rs::die!(Errno.into_raw(), Colour, Msg);
29/// unreachable!("You are dead!");
30/// ```
31///
32/// # Example
33/// ```rust
34/// extern crate suicide_rs;
35/// 
36/// let val1: u8 = 10;
37/// let val2: u8 = 20;
38/// if (val1 + val2) != 35 {
39///     suicide_rs::die!(EINVAL, color_red, "It is good day to be not dead!");
40/// }
41/// unreachable!("You are dead!");
42/// ```
43#[macro_export]
44macro_rules! die {
45    ($code:expr, $colour:expr, $($arg:tt)*) => {
46        $crate::internal::_die($code, $colour, format_args!($($arg)*))
47    };
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use linux_errnos::x86_64::Errno;
54
55    #[test]
56    #[should_panic]
57    fn it_works() {
58        let val1: u8 = 10;
59        let val2: u8 = 20;
60        if (val1 + val2) != 35 {
61            die!(Errno::EINVAL.into_raw(), inline_colorization::color_red, "It is good day to be not dead!");
62        }
63        unreachable!("You are dead!");
64    }
65}