Skip to main content

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