Skip to main content

ocelot_base/
source_diagnostics.rs

1use crate::source_diagnostic::SourceDiagnostic;
2
3/// Collection of structured source diagnostics.
4#[derive(Debug, Clone, PartialEq, Eq, Default)]
5pub struct SourceDiagnostics {
6    /// Diagnostics included in this collection.
7    pub diagnostics: Vec<SourceDiagnostic>,
8}
9
10impl SourceDiagnostics {
11    /// Creates an empty diagnostic collection.
12    pub fn new() -> Self {
13        Self::default()
14    }
15
16    /// Returns a copy of this collection with one appended diagnostic.
17    pub fn with_diagnostic(mut self, diagnostic: SourceDiagnostic) -> Self {
18        self.diagnostics.push(diagnostic);
19        self
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::SourceDiagnostics;
26    use crate::diagnostic_level::DiagnosticLevel;
27    use crate::source_diagnostic::SourceDiagnostic;
28
29    #[test]
30    fn source_diagnostics_tracks_multiple_entries() {
31        let diagnostics = SourceDiagnostics::new()
32            .with_diagnostic(SourceDiagnostic::new(
33                DiagnosticLevel::Error,
34                "examples/test.ocelot",
35                "first error",
36            ))
37            .with_diagnostic(SourceDiagnostic::new(
38                DiagnosticLevel::Warning,
39                "examples/test.ocelot",
40                "second warning",
41            ));
42
43        assert_eq!(diagnostics.diagnostics.len(), 2);
44        assert_eq!(diagnostics.diagnostics[0].message, "first error");
45        assert_eq!(diagnostics.diagnostics[1].message, "second warning");
46    }
47}