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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use super::print_items::*;

pub fn surround_with_new_lines(mut elements: Vec<PrintItem>) -> Vec<PrintItem> {
    elements.insert(0, PrintItem::NewLine);
    elements.push(PrintItem::NewLine);
    elements
}

pub fn with_indent(mut elements: Vec<PrintItem>) -> Vec<PrintItem> {
    elements.insert(0, PrintItem::StartIndent);
    elements.push(PrintItem::FinishIndent);
    elements
}

pub fn new_line_group(mut elements: Vec<PrintItem>) -> Vec<PrintItem> {
    elements.insert(0, PrintItem::StartNewLineGroup);
    elements.push(PrintItem::FinishNewLineGroup);
    elements
}

pub fn if_true(
    name: &'static str,
    resolver: impl Fn(&mut ConditionResolverContext) -> Option<bool> + Clone + 'static,
    true_item: PrintItem
) -> PrintItem {
    Condition::new(name, ConditionProperties {
        true_path: Some(vec![true_item]),
        false_path: Option::None,
        condition: Box::new(resolver.clone()),
    }).into()
}

pub fn if_true_or(
    name: &'static str,
    resolver: impl Fn(&mut ConditionResolverContext) -> Option<bool> + Clone + 'static,
    true_item: PrintItem,
    false_item: PrintItem
) -> PrintItem {
    Condition::new(name, ConditionProperties {
        true_path: Some(vec![true_item]),
        false_path: Some(vec![false_item]),
        condition: Box::new(resolver.clone())
    }).into()
}

pub fn parse_raw_string(text: &str) -> Vec<PrintItem> {
    let mut items: Vec<PrintItem> = Vec::new();
    let mut has_ignored_indent = false;
    let lines = text.lines().collect::<Vec<&str>>();

    for i in 0..lines.len() {
        if i > 0 {
            if !has_ignored_indent {
                items.push(PrintItem::StartIgnoringIndent);
                has_ignored_indent = true;
            }

            items.push(PrintItem::NewLine);
        }

        items.extend(parse_line(&lines[i]));
    }

    if has_ignored_indent {
        items.push(PrintItem::FinishIgnoringIndent);
    }

    return items;

    fn parse_line(line: &str) -> Vec<PrintItem> {
        let mut items: Vec<PrintItem> = Vec::new();
        let parts = line.split("\t").collect::<Vec<&str>>();
        for i in 0..parts.len() {
            if i > 0 {
                items.push(PrintItem::Tab);
            }
            items.push(parts[i].into());
        }
        items
    }
}

pub fn prepend_if_has_items(items: Vec<PrintItem>, item: PrintItem) -> Vec<PrintItem> {
    let mut items = items;
    if !items.is_empty() {
        items.insert(0, item);
    }
    items
}