1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
use crate::{Arguments, String, Write};
#[cfg(feature = "macros")]
/// Create a [`stylish::String`] using interpolation of runtime elements.
///
/// The first argument `format!` receives is a format string literal, the rest
/// are parameters interpolated based on the format string. See the main
/// [`stylish`] docs for more info on how the interpolation is controlled.
///
/// ```rust
/// let s: stylish::String = stylish::format!("Hello {:(fg=green)}!", "World");
///
/// assert_eq!(
/// stylish::html::format!("{:s}", s),
/// "Hello <span style=color:green>World</span>!",
/// );
/// ```
#[macro_export]
macro_rules! format {
($($arg:tt)*) => {{
let res = $crate::format($crate::format_args!($($arg)*));
res
}};
}
/// The `format` function takes a [`stylish::Arguments`] struct and returns the
/// resulting attributed and formatted [`stylish::String`].
///
/// The [`stylish::Arguments`] instance can be created with the
/// [`stylish::format_args!`] macro.
///
/// # Examples
///
/// Basic usage:
///
/// ```rust
/// let s = stylish::format(stylish::format_args!(
/// "Hello, {:(fg=green)}!",
/// "world"
/// ));
/// assert_eq!(
/// stylish::html::format!("{:s}", s),
/// "Hello, <span style=color:green>world</span>!"
/// );
/// ```
///
///
/// Please note that using [`stylish::format!`] might be preferable. Example:
///
/// ```rust
/// let s = stylish::format!("Hello, {:(fg=green)}!", "world");
/// assert_eq!(
/// stylish::html::format!("{:s}", s),
/// "Hello, <span style=color:green>world</span>!"
/// );
/// ```
pub fn format(args: Arguments<'_>) -> String {
let mut output = String::new();
output
.write_fmt(args)
.expect("a formatting trait implementation returned an error");
output
}