stylish_core/to_string.rs
1use crate::{format, Display, String};
2
3/// A trait for converting a value to a [`stylish::String`].
4///
5/// This trait is automatically implemented for any type which implements the
6/// [`stylish::Display`] trait. As such, `ToString` shouldn’t be implemented
7/// directly: [`stylish::Display`] should be implemented instead, and you get
8/// the `ToString` implementation for free.
9pub trait ToStylishString {
10 /// Converts the given value to a [`stylish::String`].
11 ///
12 /// ```rust
13 /// struct Warning(&'static str);
14 ///
15 /// impl stylish::Display for Warning {
16 /// fn fmt(&self, f: &mut stylish::Formatter<'_>) -> stylish::Result {
17 /// f.with(stylish::Foreground(stylish::Color::Red))
18 /// .write_str(self.0)
19 /// }
20 /// }
21 ///
22 /// use stylish::ToStylishString;
23 ///
24 /// let s: stylish::String = Warning("FIRE").to_stylish_string();
25 /// assert_eq!(
26 /// stylish::html::format!("{:s}", Warning("FIRE")),
27 /// stylish::html::format!("{:s}", s),
28 /// );
29 /// ```
30 fn to_stylish_string(&self) -> String;
31}
32
33impl<T> ToStylishString for T
34where
35 T: Display + ?Sized,
36{
37 fn to_stylish_string(&self) -> String {
38 format!("{:s}", self)
39 }
40}