ion/format/
config.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use colored::Color;
8
9use crate::flags::IteratorFlags;
10
11/// Configuration for the colours used when formatting values as specific types.
12#[derive(Clone, Copy, Debug)]
13pub struct ColourConfig {
14	pub boolean: Color,
15	pub number: Color,
16	pub string: Color,
17	pub bigint: Color,
18	pub symbol: Color,
19	pub null: Color,
20	pub undefined: Color,
21	pub array: Color,
22	pub object: Color,
23	pub function: Color,
24	pub date: Color,
25	pub promise: Color,
26	pub regexp: Color,
27}
28
29impl Default for ColourConfig {
30	fn default() -> Self {
31		ColourConfig {
32			boolean: Color::Cyan,
33			number: Color::Blue,
34			string: Color::Green,
35			bigint: Color::Blue,
36			symbol: Color::Magenta,
37			null: Color::TrueColor { r: 118, g: 118, b: 118 },
38			undefined: Color::TrueColor { r: 118, g: 118, b: 118 },
39			array: Color::White,
40			object: Color::White,
41			function: Color::White,
42			date: Color::White,
43			promise: Color::Yellow,
44			regexp: Color::Green,
45		}
46	}
47}
48
49impl ColourConfig {
50	/// Returns [ColourConfig] where all formatted strings are white.
51	pub fn white() -> ColourConfig {
52		ColourConfig {
53			boolean: Color::White,
54			number: Color::White,
55			string: Color::White,
56			bigint: Color::White,
57			symbol: Color::White,
58			null: Color::White,
59			undefined: Color::White,
60			array: Color::White,
61			object: Color::White,
62			function: Color::White,
63			date: Color::White,
64			promise: Color::White,
65			regexp: Color::White,
66		}
67	}
68}
69
70/// Represents configuration for formatting
71#[derive(Clone, Copy, Debug)]
72#[must_use]
73pub struct Config {
74	pub colours: ColourConfig,
75	pub iteration: IteratorFlags,
76	pub depth: u16,
77	pub indentation: u16,
78	pub multiline: bool,
79	pub quoted: bool,
80}
81
82impl Config {
83	/// Replaces the colors in the [configuration](Config).
84	pub fn colours(self, colours: ColourConfig) -> Config {
85		Config { colours, ..self }
86	}
87
88	pub fn iteration(self, iteration: IteratorFlags) -> Config {
89		Config { iteration, ..self }
90	}
91
92	pub fn depth(self, depth: u16) -> Config {
93		Config { depth, ..self }
94	}
95
96	pub fn indentation(self, indentation: u16) -> Config {
97		Config { indentation, ..self }
98	}
99
100	pub fn multiline(self, multiline: bool) -> Config {
101		Config { multiline, ..self }
102	}
103
104	pub fn quoted(self, quoted: bool) -> Config {
105		Config { quoted, ..self }
106	}
107}
108
109impl Default for Config {
110	fn default() -> Config {
111		Config {
112			colours: ColourConfig::default(),
113			iteration: IteratorFlags::default(),
114			depth: 0,
115			indentation: 0,
116			multiline: true,
117			quoted: false,
118		}
119	}
120}