kutil_cli/debug/utils/
list.rs1use super::super::{context::*, debuggable::*, format::*};
2
3use std::io::*;
4
5pub const DEBUG_INTO_LIST_ITEM: &str = "•";
7
8pub fn write_debug_as_list<'own, ItemT, IteratorT, WriteT>(
12 iterator: IteratorT,
13 override_format: Option<DebugFormat>,
14 writer: &mut WriteT,
15 context: &DebugContext,
16) -> Result<()>
17where
18 ItemT: Debuggable + 'own,
19 IteratorT: Iterator<Item = &'own ItemT>,
20 WriteT: Write,
21{
22 let mut iterator = iterator.peekable();
23
24 if iterator.peek().is_none() {
25 context.separate(writer)?;
26 return context.theme.write_delimiter(writer, "[]");
27 }
28
29 let format = match override_format {
30 Some(format) => format,
31 None => context.format.clone(),
32 };
33
34 match format {
35 DebugFormat::Compact => {
36 context.separate(writer)?;
37 context.theme.write_delimiter(writer, "[")?;
38
39 let child_context = context.child().with_separator(false);
40
41 while let Some(item) = iterator.next() {
42 item.write_debug_for(writer, &child_context)?;
43 if iterator.peek().is_some() {
44 context.theme.write_delimiter(writer, ",")?;
45 }
46 }
47
48 context.theme.write_delimiter(writer, "]")
49 }
50
51 DebugFormat::Reduced | DebugFormat::Verbose => {
52 let child_context = context.child().with_separator(true).increase_indentation();
53
54 let mut first = true;
55 for item in iterator {
56 context.separate_or_indent_into(writer, DEBUG_INTO_LIST_ITEM, first)?;
57 item.write_debug_for(writer, &child_context)?;
58
59 first = false;
60 }
61
62 Ok(())
63 }
64 }
65}