kutil_cli/debug/
debuggable.rs1use super::{context::*, format::*, theme::*};
2
3use std::io::*;
4
5const TO_STRING_BUFFER_CAPACITY: usize = 1024;
6
7pub trait Debuggable {
15 fn write_debug_for<WriteT>(&self, writer: &mut WriteT, context: &DebugContext) -> Result<()>
23 where
24 WriteT: Write;
25
26 fn write_debug_with_format<WriteT>(&self, writer: &mut WriteT, format: DebugFormat) -> Result<()>
30 where
31 WriteT: Write,
32 {
33 self.write_debug_for(writer, &DebugContext::new(&Theme::default()).with_format(format))?;
34 writeln!(writer)
35 }
36
37 fn write_debug<WriteT>(&self, writer: &mut WriteT) -> Result<()>
39 where
40 WriteT: Write,
41 {
42 self.write_debug_with_format(writer, DebugFormat::default())
43 }
44
45 fn write_debug_plain_with_format<WriteT>(&self, writer: &mut WriteT, format: DebugFormat) -> Result<()>
47 where
48 WriteT: Write,
49 {
50 self.write_debug_for(writer, &DebugContext::new(&Theme::plain()).with_format(format))?;
51 writeln!(writer)
52 }
53
54 fn write_debug_plain<WriteT>(&self, writer: &mut WriteT) -> Result<()>
56 where
57 WriteT: Write,
58 {
59 self.write_debug_with_format(writer, DebugFormat::default())
60 }
61
62 fn print_debug_with_format(&self, format: DebugFormat) {
68 self.write_debug_with_format(&mut anstream::stdout(), format).expect("write_debug_with_format");
69 }
70
71 fn print_debug(&self) {
75 self.print_debug_with_format(DebugFormat::default());
76 }
77
78 fn print_debug_plain_with_format(&self, format: DebugFormat) {
82 self.write_debug_plain_with_format(&mut stdout(), format).expect("write_debug_plain_with_format");
83 }
84
85 fn print_debug_plain(&self) {
89 self.print_debug_plain_with_format(DebugFormat::default());
90 }
91
92 fn eprint_debug_with_format(&self, format: DebugFormat) {
98 self.write_debug_with_format(&mut anstream::stdout(), format).expect("write_debug_with_format");
99 }
100
101 fn eprint_debug(&self) {
105 self.eprint_debug_with_format(DebugFormat::default());
106 }
107
108 fn eprint_debug_plain_with_format(&self, format: DebugFormat) {
112 self.write_debug_plain_with_format(&mut stdout(), format).expect("write_debug_plain_with_format");
113 }
114
115 fn eprint_debug_plain(&self) {
119 self.eprint_debug_plain_with_format(DebugFormat::default());
120 }
121
122 fn to_debug_string_with_format(&self, theme: &Theme, format: DebugFormat) -> Result<String> {
124 let mut writer = Vec::with_capacity(TO_STRING_BUFFER_CAPACITY);
125 self.write_debug_for(&mut writer, &DebugContext::new(theme).with_format(format))?;
126 String::from_utf8(writer.into()).map_err(Error::other)
127 }
128
129 fn to_debug_string(&self, theme: &Theme) -> Result<String> {
131 self.to_debug_string_with_format(theme, DebugFormat::default())
132 }
133}