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
use codespan_reporting::diagnostic::{Diagnostic, Severity};

type FileId = ();

/// A collection of [`Diagnostic`]s.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Diagnostics(Vec<Diagnostic<FileId>>);

impl Diagnostics {
    pub fn new() -> Self { Diagnostics(Vec::new()) }

    pub fn iter(&self) -> impl Iterator<Item = &'_ Diagnostic<FileId>> + '_ {
        self.0.iter()
    }

    /// Get an iterator over all the [`Diagnostic`]s that are at least as severe
    /// as a certain [`Severity`].
    pub fn iter_severity(
        &self,
        severity: Severity,
    ) -> impl Iterator<Item = &'_ Diagnostic<FileId>> + '_ {
        self.iter().filter(move |diag| diag.severity >= severity)
    }

    /// Are there any diagnostics which are at least as severe as a certain
    /// [`Severity`] level?
    pub fn has_severity(&self, severity: Severity) -> bool {
        self.iter_severity(severity).next().is_some()
    }

    /// Does this set of [`Diagnostics`] contain any [`Diagnostic`]s which are
    /// at least as bad as an error?
    pub fn has_errors(&self) -> bool { self.has_severity(Severity::Error) }

    /// Does this set of [`Diagnostics`] contain any [`Diagnostic`]s which are
    /// at least as bad as a warning?
    pub fn has_warnings(&self) -> bool { self.has_severity(Severity::Warning) }

    /// Add a new [`Diagnostic`] to the collection.
    pub fn push(&mut self, diag: Diagnostic<FileId>) { self.0.push(diag); }

    /// Is this collection of [`Diagnostic`]s empty?
    pub fn is_empty(&self) -> bool { self.0.is_empty() }

    pub fn len(&self) -> usize { self.0.len() }

    /// Remove all [`Diagnostic`]s from this set of [`Diagnostics`].
    pub fn drain(&mut self) -> impl Iterator<Item = Diagnostic<()>> + '_ {
        self.0.drain(..)
    }
}

impl<'a> IntoIterator for &'a Diagnostics {
    type IntoIter = <&'a Vec<Diagnostic<FileId>> as IntoIterator>::IntoIter;
    type Item = &'a Diagnostic<FileId>;

    fn into_iter(self) -> Self::IntoIter { self.0.iter() }
}

impl IntoIterator for Diagnostics {
    type IntoIter = <Vec<Diagnostic<FileId>> as IntoIterator>::IntoIter;
    type Item = Diagnostic<FileId>;

    fn into_iter(self) -> Self::IntoIter { self.0.into_iter() }
}

impl Extend<Diagnostic<FileId>> for Diagnostics {
    fn extend<T: IntoIterator<Item = Diagnostic<FileId>>>(&mut self, iter: T) {
        self.0.extend(iter);
    }
}