tiger_lib/report/
builder.rs

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! By splitting the builder up into stages, we achieve two goals.
//! - The order of calls is enforced, leading to more consistent code. E.g. calls to `weak()` or
//!     `strong()` should always directly follow the opening call.
//! - The user is forced to add at least one pointer, making it impossible to create a report
//!     without pointers, which would lead to panics.

use crate::report::{log, Confidence, ErrorKey, ErrorLoc, LogReport, PointedMessage, Severity};

// =================================================================================================
// =============== Starting points:
// =================================================================================================

pub fn tips(key: ErrorKey) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, Severity::Tips)
}

pub fn untidy(key: ErrorKey) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, Severity::Untidy)
}

pub fn warn(key: ErrorKey) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, Severity::Warning)
}

pub fn err(key: ErrorKey) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, Severity::Error)
}

pub fn fatal(key: ErrorKey) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, Severity::Fatal)
}

pub fn report(key: ErrorKey, severity: Severity) -> ReportBuilderStage1 {
    ReportBuilderStage1::new(key, severity)
}

// =================================================================================================
// =============== Builder internals:
// =================================================================================================

#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct ReportBuilderStage1(ErrorKey, Severity, Confidence);

impl ReportBuilderStage1 {
    /// For internal use only.
    fn new(key: ErrorKey, severity: Severity) -> Self {
        Self(key, severity, Confidence::Reasonable)
    }

    /// Optional step. Confidence defaults to Reasonable but this overrides it to Weak.
    pub fn weak(mut self) -> Self {
        self.2 = Confidence::Weak;
        self
    }

    /// Optional step. Confidence defaults to Reasonable but this overrides it to Strong.
    pub fn strong(mut self) -> Self {
        self.2 = Confidence::Strong;
        self
    }

    /// Optional step for when confidence is not known at compile time.
    pub fn conf(mut self, conf: Confidence) -> Self {
        self.2 = conf;
        self
    }

    /// Sets the main report message.
    pub fn msg<S: Into<String>>(self, msg: S) -> ReportBuilderStage2 {
        ReportBuilderStage2 { stage1: self, msg: msg.into(), info: None }
    }
}

#[derive(Debug)]
#[must_use]
pub struct ReportBuilderStage2 {
    stage1: ReportBuilderStage1,
    msg: String,
    info: Option<String>,
}

impl ReportBuilderStage2 {
    /// Optional step. Adds an info section to the report.
    pub fn info<S: Into<String>>(mut self, info: S) -> Self {
        let info = info.into();
        self.info = if info.is_empty() { None } else { Some(info) };
        self
    }

    /// Optional step. Adds an info section to the report if the `info` parameter is `Some`.
    pub fn opt_info<S: Into<String>>(mut self, info: Option<S>) -> Self {
        self.info = info.map(Into::into);
        self
    }

    pub fn loc<E: ErrorLoc>(self, eloc: E) -> ReportBuilderStage3 {
        let length = eloc.loc_length();
        ReportBuilderStage3 {
            stage1: self.stage1,
            msg: self.msg,
            info: self.info,
            pointers: vec![PointedMessage { loc: eloc.into_loc(), length, msg: None }],
        }
    }

    #[cfg(feature = "ck3")]
    pub fn loc_msg<E: ErrorLoc, S: Into<String>>(self, eloc: E, msg: S) -> ReportBuilderStage3 {
        let length = eloc.loc_length();
        ReportBuilderStage3 {
            stage1: self.stage1,
            msg: self.msg,
            info: self.info,
            pointers: vec![PointedMessage { loc: eloc.into_loc(), length, msg: Some(msg.into()) }],
        }
    }

    pub fn pointers(self, pointers: Vec<PointedMessage>) -> ReportBuilderStage3 {
        ReportBuilderStage3 { stage1: self.stage1, msg: self.msg, info: self.info, pointers }
    }
}

#[derive(Debug)]
#[must_use]
pub struct ReportBuilderStage3 {
    stage1: ReportBuilderStage1,
    msg: String,
    info: Option<String>,
    pointers: Vec<PointedMessage>,
}

impl ReportBuilderStage3 {
    pub fn loc_msg<E: ErrorLoc, S: Into<String>>(mut self, eloc: E, msg: S) -> Self {
        let length = eloc.loc_length();
        self.pointers.push(PointedMessage { loc: eloc.into_loc(), length, msg: Some(msg.into()) });
        self
    }
    pub fn opt_loc_msg<E: ErrorLoc, S: Into<String>>(mut self, eloc: Option<E>, msg: S) -> Self {
        if let Some(eloc) = eloc {
            let length = eloc.loc_length();
            self.pointers.push(PointedMessage {
                loc: eloc.into_loc(),
                length,
                msg: Some(msg.into()),
            });
        }
        self
    }
    /// Build the report and return it.
    /// Build the report and return it.
    pub fn build(self) -> LogReport {
        LogReport {
            key: self.stage1.0,
            severity: self.stage1.1,
            confidence: self.stage1.2,
            msg: self.msg,
            info: self.info,
            pointers: self.pointers,
        }
    }
    /// Build the report and push it to be printed.
    pub fn push(self) {
        log(self.build());
    }
}