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