Skip to main content

libgraphql_parser/ast/
argument.rs

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