Skip to main content

libgraphql_parser/
graphql_error_note.rs

1use crate::GraphQLErrorNoteKind;
2use crate::GraphQLSourceSpan;
3use crate::SmallVec;
4
5/// An error note providing additional context about an error.
6///
7/// Notes augment the primary error message with:
8/// - Explanatory context (why the error occurred)
9/// - Actionable suggestions (how to fix it)
10/// - Specification references (where to learn more)
11/// - Related source locations (e.g., where a delimiter was opened)
12#[derive(Debug, Clone, PartialEq)]
13pub struct GraphQLErrorNote {
14    /// The kind of note (determines rendering prefix).
15    pub kind: GraphQLErrorNoteKind,
16
17    /// The note message.
18    pub message: String,
19
20    /// Optional span pointing to a related location.
21    ///
22    /// When present, the note is rendered with a source snippet
23    /// pointing to this location.
24    pub span: Option<GraphQLSourceSpan>,
25}
26
27impl GraphQLErrorNote {
28    /// Creates a general note without a span.
29    pub fn general(message: impl Into<String>) -> Self {
30        Self {
31            kind: GraphQLErrorNoteKind::General,
32            message: message.into(),
33            span: None,
34        }
35    }
36
37    /// Creates a general note with a span.
38    pub fn general_with_span(message: impl Into<String>, span: GraphQLSourceSpan) -> Self {
39        Self {
40            kind: GraphQLErrorNoteKind::General,
41            message: message.into(),
42            span: Some(span),
43        }
44    }
45
46    /// Creates a help note without a span.
47    pub fn help(message: impl Into<String>) -> Self {
48        Self {
49            kind: GraphQLErrorNoteKind::Help,
50            message: message.into(),
51            span: None,
52        }
53    }
54
55    /// Creates a help note with a span.
56    pub fn help_with_span(message: impl Into<String>, span: GraphQLSourceSpan) -> Self {
57        Self {
58            kind: GraphQLErrorNoteKind::Help,
59            message: message.into(),
60            span: Some(span),
61        }
62    }
63
64    /// Creates a spec reference note.
65    pub fn spec(url: impl Into<String>) -> Self {
66        Self {
67            kind: GraphQLErrorNoteKind::Spec,
68            message: url.into(),
69            span: None,
70        }
71    }
72}
73
74/// Type alias for error notes.
75///
76/// Uses SmallVec since most errors have 0-2 notes, avoiding heap
77/// allocation in the common case.
78pub type GraphQLErrorNotes = SmallVec<[GraphQLErrorNote; 2]>;