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