Skip to main content

libgraphql_parser/ast/
list_value.rs

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