1use std::rc::Rc;
2
3use wasm_bindgen::JsValue;
4
5#[derive(Clone, Debug, Default)]
6pub struct Color {
7 open: String,
8 close: String,
9}
10
11impl Color {
12 pub fn open(&self) -> String {
13 self.open.clone()
14 }
15
16 pub fn close(&self) -> String {
17 self.close.clone()
18 }
19
20 pub fn paint(&self, s: &str) -> String {
21 format!("{}{}{}", self.open(), s, self.close())
22 }
23}
24
25impl From<ansi_style::Color> for Color {
26 fn from(value: ansi_style::Color) -> Self {
27 Color {
28 open: value.open(),
29 close: value.close().into(),
30 }
31 }
32}
33
34#[derive(Clone, Debug, Default)]
35pub struct Colors {
36 pub comment: Color,
37 pub content: Color,
38 pub prop: Color,
39 pub tag: Color,
40 pub value: Color,
41}
42
43#[derive(Clone, Debug)]
44pub struct Theme {
45 pub comment: Color,
46 pub content: Color,
47 pub prop: Color,
48 pub tag: Color,
49 pub value: Color,
50}
51
52impl Default for Theme {
53 fn default() -> Self {
54 Self {
55 comment: ansi_style::Color::BlackBright.into(),
56 content: Color {
57 open: "\x1B[0m".into(),
59 close: "\x1B[0m".into(),
60 },
61 prop: ansi_style::Color::Yellow.into(),
62 tag: ansi_style::Color::Cyan.into(),
63 value: ansi_style::Color::Green.into(),
64 }
65 }
66}
67
68pub type Refs = Vec<JsValue>;
69
70pub type CompareKeys = Rc<dyn Fn(String, String) -> usize>;
71
72#[derive(Default)]
73pub struct PrettyFormatOptions {
74 pub call_to_json: Option<bool>,
75 pub escape_regex: Option<bool>,
76 pub escape_string: Option<bool>,
77 pub highlight: Option<bool>,
78 pub indent: Option<usize>,
79 pub max_depth: Option<usize>,
80 pub max_width: Option<usize>,
81 pub min: Option<bool>,
82 pub print_basic_prototype: Option<bool>,
83 pub print_function_name: Option<bool>,
84 pub theme: Option<Theme>,
85 pub compare_keys: Option<CompareKeys>,
86 pub plugins: Option<Plugins>,
87}
88
89impl PrettyFormatOptions {
90 pub fn call_to_json(mut self, value: bool) -> Self {
91 self.call_to_json = Some(value);
92 self
93 }
94
95 pub fn escape_regex(mut self, value: bool) -> Self {
96 self.escape_regex = Some(value);
97 self
98 }
99
100 pub fn escape_string(mut self, value: bool) -> Self {
101 self.escape_string = Some(value);
102 self
103 }
104
105 pub fn highlight(mut self, value: bool) -> Self {
106 self.highlight = Some(value);
107 self
108 }
109
110 pub fn indent(mut self, value: usize) -> Self {
111 self.indent = Some(value);
112 self
113 }
114
115 pub fn max_depth(mut self, value: usize) -> Self {
116 self.max_depth = Some(value);
117 self
118 }
119
120 pub fn max_width(mut self, value: usize) -> Self {
121 self.max_width = Some(value);
122 self
123 }
124
125 pub fn min(mut self, value: bool) -> Self {
126 self.min = Some(value);
127 self
128 }
129
130 pub fn print_basic_prototype(mut self, value: bool) -> Self {
131 self.print_basic_prototype = Some(value);
132 self
133 }
134
135 pub fn print_function_name(mut self, value: bool) -> Self {
136 self.print_function_name = Some(value);
137 self
138 }
139
140 pub fn theme(mut self, value: Theme) -> Self {
141 self.theme = Some(value);
142 self
143 }
144
145 pub fn compare_keys(mut self, value: CompareKeys) -> Self {
146 self.compare_keys = Some(value);
147 self
148 }
149
150 pub fn plugins(mut self, value: Plugins) -> Self {
151 self.plugins = Some(value);
152 self
153 }
154}
155
156pub struct Config {
157 pub call_to_json: bool,
158 pub compare_keys: Option<CompareKeys>,
159 pub colors: Colors,
160 pub escape_regex: bool,
161 pub escape_string: bool,
162 pub indent: String,
163 pub max_depth: usize,
164 pub max_width: usize,
165 pub min: bool,
166 pub plugins: Plugins,
167 pub print_function_name: bool,
169 pub spacing_inner: String,
170 pub spacing_outer: String,
171}
172
173pub type Printer = dyn Fn(&JsValue, &Config, String, usize, Refs, Option<bool>) -> String;
174
175pub trait Plugin {
176 fn test(&self, val: &JsValue) -> bool;
177
178 fn serialize(
179 &self,
180 val: &JsValue,
181 config: &Config,
182 indentation: String,
183 depth: usize,
184 refs: Refs,
185 printer: &Printer,
186 ) -> String;
187}
188
189pub type Plugins = Vec<Rc<dyn Plugin>>;