libgraphql_parser/graphql_error_note.rs
1use crate::GraphQLErrorNoteKind;
2use crate::SourceSpan;
3
4/// An error note providing additional context about an error.
5///
6/// Notes augment the primary error message with:
7/// - Explanatory context (why the error occurred)
8/// - Actionable suggestions (how to fix it)
9/// - Specification references (where to learn more)
10/// - Related source locations (e.g., where a delimiter was opened)
11#[derive(Debug, Clone, PartialEq)]
12pub struct GraphQLErrorNote {
13 /// The kind of note (determines rendering prefix).
14 pub kind: GraphQLErrorNoteKind,
15
16 /// The note message.
17 pub message: String,
18
19 /// Optional pre-resolved span pointing to a related location.
20 ///
21 /// When present, the note is rendered with a source snippet
22 /// pointing to this location. Like the primary error's
23 /// `source_span`, this is eagerly resolved at error
24 /// construction time so it carries line/column/byte-offset
25 /// information without requiring a `SourceMap` at display time.
26 pub span: Option<SourceSpan>,
27}
28
29impl GraphQLErrorNote {
30 /// Creates a general note without a span.
31 pub fn general(message: impl Into<String>) -> Self {
32 Self {
33 kind: GraphQLErrorNoteKind::General,
34 message: message.into(),
35 span: None,
36 }
37 }
38
39 /// Creates a general note with a pre-resolved span.
40 pub fn general_with_span(
41 message: impl Into<String>,
42 span: SourceSpan,
43 ) -> Self {
44 Self {
45 kind: GraphQLErrorNoteKind::General,
46 message: message.into(),
47 span: Some(span),
48 }
49 }
50
51 /// Creates a help note without a span.
52 pub fn help(message: impl Into<String>) -> Self {
53 Self {
54 kind: GraphQLErrorNoteKind::Help,
55 message: message.into(),
56 span: None,
57 }
58 }
59
60 /// Creates a help note with a pre-resolved span.
61 pub fn help_with_span(
62 message: impl Into<String>,
63 span: SourceSpan,
64 ) -> Self {
65 Self {
66 kind: GraphQLErrorNoteKind::Help,
67 message: message.into(),
68 span: Some(span),
69 }
70 }
71
72 /// Creates a spec reference note.
73 pub fn spec(url: impl Into<String>) -> Self {
74 Self {
75 kind: GraphQLErrorNoteKind::Spec,
76 message: url.into(),
77 span: None,
78 }
79 }
80}