~skye/sbse/pre.cal

view raw


/** `use sse::macros::*;` */
#[allow(unused_imports)]
pub mod macros {
    use crate::{fatal, err_fmt, ok_fmt}; // , log, elog};
}
<?
mkfatal:{ "eprintln!(\"err: {}\", format!($($x)*)); std::process::exit(",($x),");" };
?>
/**
 * eprintln a prefix `err: fatal:` and a pattern, then exit the program.
 *
 * an optional argument $c:expr can be added to specify the exit code used
 * by std::process::exit(). ex:
 *
 * ```
 * match x {
 *     Err(e) => fatal!([-255], "this is a fatal error: {e}"),
 *     _ => ...
 * }
 * ```
 */
#[macro_export]
macro_rules! fatal {
    ($($x:tt)*) => {{
        <? <<mkfatal. , -1; ?>
    }};
    ([$c:expr], $($x:tt)*) => {{
        <? <<mkfatal. , "$c"; ?>
    }};
}

/** return `Err(format!($($x)*))` */
#[macro_export]
macro_rules! err_fmt {
    ($($x:tt)*) => {{
        Err(format!($($x)*))
    }};
}

/** return `Ok(format!($($x)*))` */
#[macro_export]
macro_rules! ok_fmt {
    ($($x:tt)*) => {{
        Ok(format!($($x)*))
    }};
}