Skip to main content

libgraphql_parser/ast/
fragment_spread.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ast::DirectiveAnnotation;
4use crate::ast::Name;
5use crate::ByteSpan;
6use crate::SourceMap;
7use crate::SourceSpan;
8use crate::token::GraphQLToken;
9use inherent::inherent;
10
11/// A named fragment spread (`...FragmentName`).
12///
13/// See
14/// [Fragment Spreads](https://spec.graphql.org/September2025/#FragmentSpread)
15/// in the spec.
16#[derive(Clone, Debug, PartialEq)]
17pub struct FragmentSpread<'src> {
18    pub directives: Vec<DirectiveAnnotation<'src>>,
19    pub name: Name<'src>,
20    pub span: ByteSpan,
21    pub syntax: Option<Box<FragmentSpreadSyntax<'src>>>,
22}
23
24/// Syntax detail for a [`FragmentSpread`].
25#[derive(Clone, Debug, PartialEq)]
26pub struct FragmentSpreadSyntax<'src> {
27    pub ellipsis: GraphQLToken<'src>,
28}
29
30impl<'src> FragmentSpread<'src> {
31    /// Returns the name of this fragment spread as a string
32    /// slice.
33    ///
34    /// Convenience accessor for `self.name.value`.
35    #[inline]
36    pub fn name_value(&self) -> &str {
37        self.name.value.as_ref()
38    }
39}
40
41#[inherent]
42impl AstNode for FragmentSpread<'_> {
43    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
44    pub fn append_source(
45        &self,
46        sink: &mut String,
47        source: Option<&str>,
48    ) {
49        if let Some(src) = source {
50            append_span_source_slice(
51                self.span, sink, src,
52            );
53        }
54    }
55
56    /// Returns this fragment spread's byte-offset span within the
57    /// source text.
58    ///
59    /// The returned [`ByteSpan`] can be resolved to line/column
60    /// positions via [`source_span()`](Self::source_span) or
61    /// [`ByteSpan::resolve()`].
62    #[inline]
63    pub fn byte_span(&self) -> ByteSpan {
64        self.span
65    }
66
67    /// Resolves this fragment spread's position to line/column
68    /// coordinates using the given [`SourceMap`].
69    ///
70    /// Returns [`None`] if the byte offsets cannot be resolved
71    /// (e.g. the span was synthetically constructed without
72    /// valid position data).
73    #[inline]
74    pub fn source_span(
75        &self,
76        source_map: &SourceMap,
77    ) -> Option<SourceSpan> {
78        self.byte_span().resolve(source_map)
79    }
80}