graphql_starter/ansi/
mod.rs1crate::using! {
5 ansi,
6 color,
7}
8
9crate::using! {
10 pub text,
11 minifier,
12}
13
14pub use ansi_to_html::Error;
15
16fn invalid_ansi(s: &'static str) -> impl Fn() -> Error {
17 move || Error::InvalidAnsi { msg: s.to_string() }
18}
19
20pub struct AnsiString(String);
24impl From<String> for AnsiString {
25 fn from(value: String) -> Self {
26 Self(value)
27 }
28}
29impl From<&str> for AnsiString {
30 fn from(value: &str) -> Self {
31 Self(value.to_owned())
32 }
33}
34impl From<AnsiString> for String {
35 fn from(value: AnsiString) -> Self {
36 value.0
37 }
38}
39
40impl AnsiString {
41 pub fn as_ansi(&self) -> &str {
42 &self.0
43 }
44
45 pub fn as_html(&self) -> Result<String, Error> {
46 ansi_to_html::Converter::new().convert(&self.0)
47 }
48
49 pub fn as_plaintext(&self) -> String {
50 strip_ansi_escapes::strip_str(&self.0)
51 }
52
53 pub fn as_styled_text(&self) -> Result<Vec<StyledText>, Error> {
54 ansi_to_text(&self.0)
55 }
56}
57
58#[cfg(feature = "graphql")]
59#[async_graphql::Object]
60impl AnsiString {
62 async fn ansi(&self) -> &str {
64 self.as_ansi()
65 }
66
67 async fn html(&self) -> crate::error::GraphQLResult<String> {
69 use crate::error::MapToErr;
70 Ok(self.as_html().map_to_internal_err("Invalid ansi string")?)
71 }
72
73 async fn plain(&self) -> String {
75 self.as_plaintext()
76 }
77
78 async fn text(&self) -> crate::error::GraphQLResult<Vec<StyledText>> {
80 use crate::error::MapToErr;
81 Ok(self.as_styled_text().map_to_internal_err("Invalid ansi string")?)
82 }
83}