Skip to content

Instantly share code, notes, and snippets.

@dmgolembiowski
Created June 9, 2024 20:07
Show Gist options
  • Save dmgolembiowski/ee526c3ee1dbc7169c1d538a3adcb685 to your computer and use it in GitHub Desktop.
Save dmgolembiowski/ee526c3ee1dbc7169c1d538a3adcb685 to your computer and use it in GitHub Desktop.
Beginning to consolidate/revise some of my favorite macros, crafted either by me or someone respectable
//! This crate contains various macros and functions for the Rust programming language.
#[macro_export]
macro_rules! runtime_globals {
( $($constant:meta => $value:meta $(; $fallback_value:expr )? $(,)? )* ) => {
$(
::paste::paste! {
#[::lazy_fn::lazy_fn]
fn [< lazy_fn_ $constant:lower >]() -> &'static str {
match ::std::env::var(concat!('"', $value ,'"')) {
Err(e) => {
$(
::std::boxed::Box::leak($fallback_value.into_boxed_str())
)?
},
Ok(fat_str) => {
::std::boxed::Box::leak(fat_str.into_boxed_str())
}
}
}
pub static ref [< GLOBAL_ $constant:upper >]: &'static str = [< lazy_fn_ $constant:lower >]();
}
)*
}
}
#[macro_export]
macro_rules! globals {
($( $constant:ident = $value:expr, )*) => {
$(
paste! {
pub static ref [< GLOBAL_ $constant >]: &'static str = $value ;
}
)*
};
}
#[macro_export]
macro_rules! input {
( $( $optional_msg:expr )? ) => {
let mut __input = String::new();
$(
::std::io::print!( $optional_msg );
)?
'out: loop {
match ::std::io::stdin().read_line(&mut __input) {
Err(_) => panic!("Using `input!` requires a TTY"),
Ok(it) => {
let __removed_newline = &mut password.pop();
break 'out;
}
}
}
__input
}
}
#[cfg(test)]
mod tests {
use super::*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment