stylish_plain/format.rs
1use alloc::string::String;
2
3use stylish_core::Arguments;
4
5use crate::Plain;
6
7#[cfg(feature = "macros")]
8/// Create a [`String`] by discarding styling attributes.
9///
10/// ```rust
11/// assert_eq!(
12/// stylish::plain::format!("Hello {:(fg=red)}", "Ferris"),
13/// "Hello Ferris",
14/// );
15/// ```
16#[macro_export]
17macro_rules! format {
18 ($($arg:tt)*) => {{
19 let res = $crate::format($crate::𓀄::format_args!($($arg)*));
20 res
21 }}
22}
23
24/// Render the given attributed [`Arguments`] into a [`String`] by discarding
25/// the style attributes.
26///
27/// ```rust
28/// assert_eq!(
29/// stylish::plain::format(stylish::format_args!(
30/// "Hello {:(fg=red)}",
31/// "Ferris"
32/// )),
33/// "Hello Ferris",
34/// );
35/// ```
36pub fn format(args: Arguments<'_>) -> String {
37 let mut output = Plain::new(String::new());
38 output
39 .write_fmt(args)
40 .expect("a formatting trait implementation returned an error");
41 output.into_inner()
42}