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
use atelier_lib::core::action::{ActionIssue, IssueLevel};
use std::error::Error;

#[cfg(feature = "color")]
use colored::Colorize;

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

pub fn report_action_issues(issues: Vec<ActionIssue>, color: bool) -> Result<(), Box<dyn Error>> {
    println!();
    if !issues.is_empty() {
        for issue in issues {
            if color {
                report_issue(issue)
            } else {
                report_issue_no_color(issue)
            }
        }
    } else {
        println!("No issues reported.");
    }
    Ok(())
}

fn report_issue_no_color(issue: ActionIssue) {
    println!("[{}] {}", issue.level(), issue.message(),);
    println!(
        "\tReported by {} for element {}.",
        issue.reporter(),
        match issue.locus() {
            Some(id) => id.to_string(),
            None => String::new(),
        }
    );
    println!()
}

#[cfg(not(feature = "color"))]
fn report_issue(issue: ActionIssue) {
    report_issue_no_color(issue)
}

#[cfg(feature = "color")]
fn report_issue(issue: ActionIssue) {
    println!(
        "{} {}",
        match issue.level() {
            IssueLevel::Info => "[info]".normal(),
            IssueLevel::Warning => "[warning]".yellow(),
            IssueLevel::Error => "[error]".bright_red(),
        },
        issue.message().bold()
    );

    println!(
        "{}",
        format!(
            "\tReported by {}{}.",
            issue.reporter(),
            match issue.locus() {
                Some(id) => format!(" on/for element `{}`", id.to_string().underline()),
                None => String::new(),
            }
        )
        .dimmed()
    );
    println!()
}