1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! Console types.
//!
//! Re-exports `crate::host_fn::install_console` for full implementation,
//! plus JSC-compatible ConsoleObject/ConsoleFormatter types.
/// Console object (JS-side provided by crate::host_fn::install_console).
/// This struct represents the Rust-side console state tracker.
#[derive(Debug)]
pub struct ConsoleObject {
/// Whether ANSI colors are enabled for output.
pub enable_ansi_colors: bool,
}
impl ConsoleObject {
pub fn new() -> Self {
Self {
enable_ansi_colors: false,
}
}
}
impl Default for ConsoleObject {
fn default() -> Self {
Self::new()
}
}
/// Console output formatter.
#[derive(Debug)]
pub struct ConsoleFormatter {
/// Current indentation level.
pub indent_level: u32,
}
impl ConsoleFormatter {
pub fn new() -> Self {
Self { indent_level: 0 }
}
/// Increase indentation level.
pub fn indent(&mut self) {
self.indent_level += 1;
}
/// Decrease indentation level.
pub fn dedent(&mut self) {
if self.indent_level > 0 {
self.indent_level -= 1;
}
}
}
impl Default for ConsoleFormatter {
fn default() -> Self {
Self::new()
}
}
/// RAII guard for indentation scope.
pub struct IndentScope<'a> {
formatter: &'a mut ConsoleFormatter,
}
impl<'a> IndentScope<'a> {
pub fn new(formatter: &'a mut ConsoleFormatter) -> Self {
formatter.indent();
Self { formatter }
}
}
impl<'a> Drop for IndentScope<'a> {
fn drop(&mut self) {
self.formatter.dedent();
}
}