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
90
91
92
93
94
95
96
97
98
99
100
use crate::{ErrorKind, FriendlyError};
use colored::*;

const HEADER_LENGTH: usize = 80;

impl FriendlyError {
    fn gen_error(&mut self) -> (String, usize) {
        let mut output = String::new();
        output.push_str("--- Error");
        if let Some(code) = &self.data.code {
            output.push('(');
            output.push_str(code);
            output.push(')');
        }
        let length = output.len();
        (output.red().bold().to_string(), length)
    }

    fn gen_warning(&mut self) -> (String, usize) {
        let mut output = String::new();
        output.push_str("--- Warning");
        if let Some(code) = &self.data.code {
            output.push('(');
            output.push_str(code);
            output.push(')');
        }
        let length = output.len();
        (output.yellow().bold().to_string(), length)
    }

    pub fn print_header(&mut self) {
        let mut header_length = 0;
        let (type_string, length) = match self.data.kind {
            ErrorKind::Error => self.gen_error(),
            ErrorKind::Warning => self.gen_warning(),
        };
        header_length += length;
        self.output.push_str(&type_string);
        if let Some(title) = &self.data.title {
            self.output.push_str(": ");
            self.output.push_str(title);
            header_length += title.len() + 2;
        }
        self.output.push(' ');
        header_length += 1;
        while header_length < HEADER_LENGTH {
            self.output.push('-');
            header_length += 1;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn header_base_case() {
        colored::control::set_override(false);
        let mut error = FriendlyError::new();
        error.print_header();
        assert_eq!(
            error.output,
            "--- Error ----------------------------------------------------------------------"
        );
    }

    #[test]
    fn header_with_title() {
        colored::control::set_override(false);
        let mut error = FriendlyError::new().title("Error message");
        error.print_header();
        assert_eq!(
            error.output,
            "--- Error: Error message -------------------------------------------------------"
        );
    }

    #[test]
    fn header_with_code() {
        colored::control::set_override(false);
        let mut error = FriendlyError::new().code("E123");
        error.print_header();
        assert_eq!(
            error.output,
            "--- Error(E123) ----------------------------------------------------------------"
        );
    }

    #[test]
    fn header_with_title_and_code() {
        colored::control::set_override(false);
        let mut error = FriendlyError::new().title("Error message").code("E123");
        error.print_header();
        assert_eq!(
            error.output,
            "--- Error(E123): Error message -------------------------------------------------"
        );
    }
}