Skip to main content

libgraphql_parser/ast/
object_value.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ast::DelimiterPair;
4use crate::ast::ObjectField;
5use crate::ByteSpan;
6use crate::SourceMap;
7use crate::SourceSpan;
8use inherent::inherent;
9
10/// A GraphQL input object value (e.g., `{x: 1, y: 2}`).
11///
12/// See the
13/// [Input Object Values](https://spec.graphql.org/September2025/#sec-Input-Object-Values)
14/// section of the spec.
15#[derive(Clone, Debug, PartialEq)]
16pub struct ObjectValue<'src> {
17    pub fields: Vec<ObjectField<'src>>,
18    pub span: ByteSpan,
19    pub syntax: Option<Box<ObjectValueSyntax<'src>>>,
20}
21
22/// Syntax detail for an [`ObjectValue`].
23#[derive(Clone, Debug, PartialEq)]
24pub struct ObjectValueSyntax<'src> {
25    pub braces: DelimiterPair<'src>,
26}
27
28#[inherent]
29impl AstNode for ObjectValue<'_> {
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 object 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 object 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}