graphql_composition/
diagnostics.rs

1/// Warnings and errors produced by composition.
2#[derive(Default, Debug)]
3pub struct Diagnostics(Vec<Diagnostic>);
4
5impl Diagnostics {
6    /// Is any of the diagnostics fatal, i.e. a hard error?
7    pub fn any_fatal(&self) -> bool {
8        self.0.iter().any(|diagnostic| diagnostic.is_fatal)
9    }
10
11    /// Iterate non-fatal diagnostics.
12    pub fn iter_warnings(&self) -> impl Iterator<Item = &str> {
13        self.0
14            .iter()
15            .filter(|diagnostic| !diagnostic.is_fatal)
16            .map(|diagnostic| diagnostic.message.as_str())
17    }
18
19    /// Iterate fatal diagnostics.
20    pub fn iter_errors(&self) -> impl Iterator<Item = &str> {
21        self.0
22            .iter()
23            .filter(|diagnostic| diagnostic.is_fatal)
24            .map(|diagnostic| diagnostic.message.as_str())
25    }
26
27    pub(crate) fn clone_all_from(&mut self, other: &Diagnostics) {
28        self.0.extend(other.0.iter().cloned())
29    }
30
31    /// Iterate over all diagnostic messages.
32    pub fn iter_messages(&self) -> impl Iterator<Item = &str> {
33        self.0.iter().map(|diagnostic| diagnostic.message.as_str())
34    }
35
36    pub(crate) fn push_fatal(&mut self, message: String) {
37        self.0.push(Diagnostic {
38            message,
39            is_fatal: true,
40        });
41    }
42
43    pub(crate) fn push_warning(&mut self, message: String) {
44        self.0.push(Diagnostic {
45            message,
46            is_fatal: false,
47        });
48    }
49}
50
51/// A composition diagnostic.
52#[derive(Debug, Clone)]
53pub(crate) struct Diagnostic {
54    message: String,
55    /// Should this diagnostic be interpreted as a composition failure?
56    is_fatal: bool,
57}