libgraphql_parser/ast/string_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;
8use std::borrow::Cow;
9
10/// A GraphQL string value.
11///
12/// Per the
13/// [String Value](https://spec.graphql.org/September2025/#sec-String-Value)
14/// section of the spec, string values may be quoted strings
15/// or block strings. This struct contains the processed
16/// string after escape-sequence resolution and block-string
17/// indentation stripping. Borrows from source when no
18/// transformation was needed; owned when escapes were resolved
19/// or block-string stripping produced a non-contiguous result.
20#[derive(Clone, Debug, PartialEq)]
21pub struct StringValue<'src> {
22 /// Whether this string was written as a block string
23 /// (`"""..."""`) rather than a quoted string (`"..."`).
24 /// Both forms produce the same semantic value after
25 /// processing, but tools (formatters, schema differs)
26 /// may need to preserve or inspect the original form.
27 pub is_block: bool,
28 pub span: ByteSpan,
29 pub syntax: Option<Box<StringValueSyntax<'src>>>,
30 /// The processed string value after escape-sequence
31 /// resolution and block-string indentation stripping.
32 pub value: Cow<'src, str>,
33}
34
35/// Syntax detail for a [`StringValue`].
36#[derive(Clone, Debug, PartialEq)]
37pub struct StringValueSyntax<'src> {
38 pub token: GraphQLToken<'src>,
39}
40
41#[inherent]
42impl AstNode for StringValue<'_> {
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 string 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 string 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}