Skip to main content

libgraphql_parser/ast/
int_value.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ByteSpan;
4use crate::SourceMap;
5use crate::SourceSpan;
6use crate::token::GraphQLToken;
7use inherent::inherent;
8
9/// A GraphQL integer value.
10///
11/// Per the [Int](https://spec.graphql.org/September2025/#sec-Int)
12/// section of the spec, implementations should support
13/// "at minimum, the range of a signed 32-bit integer."
14/// This parser represents Int values as `i32`. On
15/// overflow/underflow the parser emits a diagnostic and
16/// clamps to `i32::MAX` / `i32::MIN`.
17#[derive(Clone, Debug, PartialEq)]
18pub struct IntValue<'src> {
19    pub span: ByteSpan,
20    pub syntax: Option<Box<IntValueSyntax<'src>>>,
21    /// The parsed 32-bit integer value. On overflow/underflow
22    /// the parser emits a diagnostic and clamps to
23    /// `i32::MAX` / `i32::MIN`.
24    pub value: i32,
25}
26
27impl IntValue<'_> {
28    /// Widen to `i64` (infallible).
29    pub fn as_i64(&self) -> i64 {
30        self.value as i64
31    }
32}
33
34/// Syntax detail for an [`IntValue`].
35#[derive(Clone, Debug, PartialEq)]
36pub struct IntValueSyntax<'src> {
37    pub token: GraphQLToken<'src>,
38}
39
40#[inherent]
41impl AstNode for IntValue<'_> {
42    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
43    pub fn append_source(
44        &self,
45        sink: &mut String,
46        source: Option<&str>,
47    ) {
48        if let Some(src) = source {
49            append_span_source_slice(
50                self.span, sink, src,
51            );
52        }
53    }
54
55    /// Returns this int value's byte-offset span within the
56    /// source text.
57    ///
58    /// The returned [`ByteSpan`] can be resolved to line/column
59    /// positions via [`source_span()`](Self::source_span) or
60    /// [`ByteSpan::resolve()`].
61    #[inline]
62    pub fn byte_span(&self) -> ByteSpan {
63        self.span
64    }
65
66    /// Resolves this int value's position to line/column
67    /// coordinates using the given [`SourceMap`].
68    ///
69    /// Returns [`None`] if the byte offsets cannot be resolved
70    /// (e.g. the span was synthetically constructed without
71    /// valid position data).
72    #[inline]
73    pub fn source_span(
74        &self,
75        source_map: &SourceMap,
76    ) -> Option<SourceSpan> {
77        self.byte_span().resolve(source_map)
78    }
79}