#[macro_export]
macro_rules! appends {
($s:expr $(,)?) => {};
($s:expr, # $ch:literal $(, $($rest:tt)*)?) => {
$s.push($ch);
$crate::appends!($s $(, $($rest)*)?);
};
($s:expr, @ $num:expr $(, $($rest:tt)*)?) => {
{
use ::std::fmt::Write;
let _ = write!($s, "{}", $num);
}
$crate::appends!($s $(, $($rest)*)?);
};
($s:expr, $item:expr $(, $($rest:tt)*)?) => {
$s.push_str($item);
$crate::appends!($s $(, $($rest)*)?);
};
}
#[macro_export]
macro_rules! appendln {
($s:expr $(, $($items:tt)*)?) => {
$crate::appends!($s $(, $($items)*)?);
$s.push('\n');
};
}
#[macro_export]
macro_rules! cstr {
($($arg:tt)*) => {{
$crate::ToCompactString::to_compact_string(&::core::format_args!($($arg)*))
}};
}
#[macro_export]
macro_rules! append {
($dst:expr, $($arg:tt)*) => {{
use ::std::fmt::Write as _;
::std::fmt::Write::write_fmt(&mut $dst, format_args!($($arg)*)).unwrap()
}};
}
#[cfg(test)]
mod tests {
#[test]
fn test_appends_strings() {
let mut s = std::string::String::new();
appends!(s, "Hello", ", ", "world", "!");
assert_eq!(s, "Hello, world!");
}
#[test]
fn test_appends_with_char() {
let mut s = std::string::String::new();
appends!(s, "a", #':', "b");
assert_eq!(s, "a:b");
}
#[test]
fn test_appends_with_number() {
let mut s = std::string::String::new();
let n = 42u32;
appends!(s, "count: ", @n);
assert_eq!(s, "count: 42");
}
#[test]
fn test_appends_mixed() {
let mut s = std::string::String::new();
let start = 10usize;
let end = 20usize;
appends!(s, "range: ", @start, #':', @end);
assert_eq!(s, "range: 10:20");
}
#[test]
fn test_appendln() {
let mut s = std::string::String::new();
appendln!(s, "Hello");
appendln!(s, "World");
assert_eq!(s, "Hello\nWorld\n");
}
#[test]
fn test_appends_empty() {
let s = std::string::String::from("start");
appends!(s);
assert_eq!(s, "start");
}
}