libgraphql_parser/ast/enum_value.rs
1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ByteSpan;
4use crate::SourceMap;
5use crate::SourceSpan;
6use crate::token::GraphQLToken;
7use inherent::inherent;
8use std::borrow::Cow;
9
10/// A GraphQL enum value (an unquoted name that is not
11/// `true`, `false`, or `null`).
12///
13/// See the
14/// [Enum Value](https://spec.graphql.org/September2025/#sec-Enum-Value)
15/// section of the spec.
16#[derive(Clone, Debug, PartialEq)]
17pub struct EnumValue<'src> {
18 pub span: ByteSpan,
19 pub syntax: Option<Box<EnumValueSyntax<'src>>>,
20 pub value: Cow<'src, str>,
21}
22
23/// Syntax detail for an [`EnumValue`] (the enum value
24/// literal, not the enum value definition).
25#[derive(Clone, Debug, PartialEq)]
26pub struct EnumValueSyntax<'src> {
27 pub token: GraphQLToken<'src>,
28}
29
30#[inherent]
31impl AstNode for EnumValue<'_> {
32 /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
33 pub fn append_source(
34 &self,
35 sink: &mut String,
36 source: Option<&str>,
37 ) {
38 if let Some(src) = source {
39 append_span_source_slice(
40 self.span, sink, src,
41 );
42 }
43 }
44
45 /// Returns this enum value's byte-offset span within the
46 /// source text.
47 ///
48 /// The returned [`ByteSpan`] can be resolved to line/column
49 /// positions via [`source_span()`](Self::source_span) or
50 /// [`ByteSpan::resolve()`].
51 #[inline]
52 pub fn byte_span(&self) -> ByteSpan {
53 self.span
54 }
55
56 /// Resolves this enum value's position to line/column
57 /// coordinates using the given [`SourceMap`].
58 ///
59 /// Returns [`None`] if the byte offsets cannot be resolved
60 /// (e.g. the span was synthetically constructed without
61 /// valid position data).
62 #[inline]
63 pub fn source_span(
64 &self,
65 source_map: &SourceMap,
66 ) -> Option<SourceSpan> {
67 self.byte_span().resolve(source_map)
68 }
69}