stylish_core/
lib.rs

1#![no_std]
2
3//! The semver-stable subset of [`stylish`].
4//!
5//! See the main documentation in [`stylish`], there is no reason to depend on
6//! this crate directly.
7
8#![allow(uncommon_codepoints)]
9#![doc(test(attr(deny(warnings))))]
10#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
11
12#[cfg(doc)]
13extern crate self as stylish;
14
15#[cfg(feature = "alloc")]
16extern crate alloc;
17
18#[cfg(feature = "std")]
19extern crate std;
20
21#[cfg(feature = "std")]
22pub mod io;
23
24#[macro_use]
25mod std_compat;
26
27mod arguments;
28mod display;
29#[cfg(feature = "alloc")]
30mod format;
31mod formatter;
32#[cfg(feature = "alloc")]
33mod string;
34#[cfg(all(feature = "alloc", feature = "macros"))]
35mod to_string;
36mod write;
37
38pub use core::fmt::{Error, Result};
39
40pub use stylish_style::{Background, Color, Foreground, Intensity, Restyle, Style, StyleDiff};
41
42#[cfg(all(feature = "alloc", feature = "macros"))]
43pub use self::to_string::ToStylishString;
44pub use self::{arguments::Arguments, display::Display, formatter::Formatter, write::Write};
45#[cfg(feature = "alloc")]
46pub use self::{format::format, string::String};
47
48#[cfg(feature = "macros")]
49#[doc(hidden)]
50pub mod 𓀄 {
51    pub use core::{fmt, option::Option};
52
53    pub use stylish_macros::{format_args, format_args_nl};
54    pub use with_builtin_macros::with_builtin;
55
56    pub use crate::{
57        arguments::{Arguments, StdFmt, StdFmtDebug, StdFmtOther},
58        formatter::{Align, DebugHex, Formatter, FormatterArgs, Sign},
59        Background, Color, Display, Foreground, Intensity, StyleDiff,
60    };
61}
62
63#[cfg(feature = "macros")]
64/// Constructs parameters for the other string-formatting macros.
65///
66/// This macro functions by taking a formatting string literal containing `{}`
67/// for each additional argument passed. `format_args!` prepares the additional
68/// parameters to ensure the output can be interpreted as a string and
69/// canonicalizes the arguments into a single type. Any value that implements
70/// the [`stylish::Display`] trait or any of the [`std::fmt`] formatting traits
71/// can be passed to `format_args!` with the appropriate trait selector.
72///
73/// This macro produces a value of type [`stylish::Arguments`]. This value can
74/// be passed to the macros within [`stylish`] for performing useful
75/// redirection. All other formatting macros ([`stylish::format!`],
76/// [`stylish::write!`], etc) are proxied through this one. `format_args!`,
77/// unlike some of its derived macros, avoids heap allocations.
78///
79/// For more information, see the documentation in [`stylish`].
80///
81/// # Examples
82///
83/// ```rust
84/// let s = stylish::html::format(stylish::format_args!(
85///     "hello {:(fg=green)}",
86///     "world"
87/// ));
88/// assert_eq!(s, stylish::html::format!("hello {:(fg=green)}", "world"));
89/// ```
90#[macro_export]
91macro_rules! format_args {
92    ($fmt:literal $(, $($arg:tt)*)?) => {
93        $crate::_format_args!($fmt $(, $($arg)*)?)
94    };
95    ($fmt:expr $(, $($arg:tt)*)?) => {
96        $crate::_format_args!($fmt $(, $($arg)*)?)
97    };
98}
99
100#[cfg(feature = "macros")]
101#[cfg(not(stylish_proc_macro_expand))]
102#[doc(hidden)]
103#[macro_export]
104macro_rules! _format_args {
105    ($fmt:literal $(, $($arg:tt)*)?) => {
106        $crate::𓀄::format_args!(crate=$crate, $fmt $(, $($arg)*)?)
107    };
108    ($fmt:expr $(, $($arg:tt)*)?) => {
109        $crate::𓀄::with_builtin!(let $fmt_lit = $fmt in {
110            $crate::𓀄::format_args!(crate=$crate, $fmt_lit $(, $($arg)*)?)
111        })
112    };
113}
114
115#[cfg(feature = "macros")]
116#[cfg(stylish_proc_macro_expand)]
117#[doc(hidden)]
118#[macro_export]
119macro_rules! _format_args {
120    ($fmt:expr $(, $($arg:tt)*)?) => {
121        $crate::𓀄::format_args!(crate=$crate, $fmt $(, $($arg)*)?)
122    };
123}