Skip to main content

libgraphql_parser/ast/
enum_value_definition.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ast::DirectiveAnnotation;
4use crate::ast::Name;
5use crate::ast::StringValue;
6use crate::ByteSpan;
7use crate::SourceMap;
8use crate::SourceSpan;
9use inherent::inherent;
10
11/// An enum value definition within an enum type.
12///
13/// See
14/// [Enum Value Definitions](https://spec.graphql.org/September2025/#EnumValuesDefinition)
15/// in the spec.
16///
17/// Unlike most other AST node types, this struct has no
18/// `syntax` field. The grammar
19/// (`Description? EnumValue Directives[Const]?`) contains no
20/// tokens beyond what the child nodes already capture:
21/// the name token is in [`Name`]'s syntax, directives in
22/// [`DirectiveAnnotation`]'s syntax, and description in
23/// [`StringValue`]'s syntax.
24#[derive(Clone, Debug, PartialEq)]
25pub struct EnumValueDefinition<'src> {
26    pub description: Option<StringValue<'src>>,
27    pub directives: Vec<DirectiveAnnotation<'src>>,
28    pub name: Name<'src>,
29    pub span: ByteSpan,
30}
31
32impl<'src> EnumValueDefinition<'src> {
33    /// Returns the name of this enum value definition as a string
34    /// slice.
35    ///
36    /// Convenience accessor for `self.name.value`.
37    #[inline]
38    pub fn name_value(&self) -> &str {
39        self.name.value.as_ref()
40    }
41}
42
43#[inherent]
44impl AstNode for EnumValueDefinition<'_> {
45    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
46    pub fn append_source(
47        &self,
48        sink: &mut String,
49        source: Option<&str>,
50    ) {
51        if let Some(src) = source {
52            append_span_source_slice(
53                self.span, sink, src,
54            );
55        }
56    }
57
58    /// Returns this enum value definition's byte-offset span within the
59    /// source text.
60    ///
61    /// The returned [`ByteSpan`] can be resolved to line/column
62    /// positions via [`source_span()`](Self::source_span) or
63    /// [`ByteSpan::resolve()`].
64    #[inline]
65    pub fn byte_span(&self) -> ByteSpan {
66        self.span
67    }
68
69    /// Resolves this enum value definition's position to line/column
70    /// coordinates using the given [`SourceMap`].
71    ///
72    /// Returns [`None`] if the byte offsets cannot be resolved
73    /// (e.g. the span was synthetically constructed without
74    /// valid position data).
75    #[inline]
76    pub fn source_span(
77        &self,
78        source_map: &SourceMap,
79    ) -> Option<SourceSpan> {
80        self.byte_span().resolve(source_map)
81    }
82}