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
// Copyright 2017 Aldrin J D'Souza.
// Licensed under the MIT License <https://opensource.org/licenses/MIT>

// Output concerns

use regex::Regex;
use report::Report;
use config::Configuration;
use handlebars::Handlebars;

/// Render the given report with the given configuration
pub fn render(config: &Configuration, report: &Report) {

    // Render the report using Handlebars
    let mut out = Handlebars::new()
        .template_render(&config.template, report)
        .unwrap()
        .trim()
        .to_string();

    // If we have line post processors,
    if !config.post_processors.is_empty() {
        // Post-process the output lines
        out = postprocess(config, &out);
    }

    // Print it out
    println!("{}", out);
}

/// Postprocess the output before printing it out
pub fn postprocess(config: &Configuration, output: &str) -> String {

    // Compile the post processor regular expressions
    let mut processors = Vec::new();
    for processor in &config.post_processors {

        // Processor the lookup regular expression
        if let Ok(lookup) = Regex::new(&processor.lookup) {

            // Inform
            info!("Using post-processor {:#?}", lookup);

            // Remember the regex and the replacement string
            processors.push((lookup, processor.replace.as_str()));
        } else {

            // Invalid regex, warn and ignore
            warn!("Post-processor {:#?} is invalid", processor);
        }
    }

    // Track the processed output
    let mut processed = Vec::new();

    // Take each line in the output
    for line in output.lines() {

        // Make a mutable copy
        let mut next: String = line.to_string();

        // Run all available processors through it
        for processor in &processors {

            // Replace the pattern as appropriate
            next = processor.0.replace_all(&next, processor.1).to_string();
        }

        // Replace line with the processed
        processed.push(next);
    }

    // Return what we ended with
    processed.join("\n")
}