Skip to main content

libgraphql_parser/ast/
float_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 float value.
10///
11/// Per the
12/// [Float Value](https://spec.graphql.org/September2025/#sec-Float-Value)
13/// section of the spec, Float is a double-precision
14/// floating-point value (IEEE 754). On overflow the parser
15/// emits a diagnostic and stores
16/// `f64::INFINITY` / `f64::NEG_INFINITY`.
17#[derive(Clone, Debug)]
18pub struct FloatValue<'src> {
19    pub span: ByteSpan,
20    pub syntax: Option<Box<FloatValueSyntax<'src>>>,
21    /// The parsed `f64` value. On overflow the parser emits a
22    /// diagnostic and stores
23    /// `f64::INFINITY` / `f64::NEG_INFINITY`.
24    pub value: f64,
25}
26
27impl PartialEq for FloatValue<'_> {
28    fn eq(&self, other: &Self) -> bool {
29        self.value.to_bits() == other.value.to_bits()
30            && self.span == other.span
31            && self.syntax == other.syntax
32    }
33}
34
35/// Syntax detail for a [`FloatValue`].
36#[derive(Clone, Debug, PartialEq)]
37pub struct FloatValueSyntax<'src> {
38    pub token: GraphQLToken<'src>,
39}
40
41#[inherent]
42impl AstNode for FloatValue<'_> {
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 float value'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 float value'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}