libgraphql_parser/ast/name.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 [name](https://spec.graphql.org/September2025/#sec-Names)
11/// (identifier).
12///
13/// Names are used for type names, field names, argument names,
14/// directive names, enum values, and more. The `value` field
15/// borrows from the source text when possible
16/// (`Cow::Borrowed`) or owns the string when the source is not
17/// available (`Cow::Owned`).
18///
19/// # Syntax Layer
20///
21/// When the parser retains syntax detail, `syntax` contains the
22/// underlying [`GraphQLToken`] with any leading trivia
23/// (whitespace, comments, commas).
24#[derive(Clone, Debug, PartialEq)]
25pub struct Name<'src> {
26 pub span: ByteSpan,
27 pub syntax: Option<Box<NameSyntax<'src>>>,
28 pub value: Cow<'src, str>,
29}
30
31/// Syntax detail for a [`Name`].
32#[derive(Clone, Debug, PartialEq)]
33pub struct NameSyntax<'src> {
34 pub token: GraphQLToken<'src>,
35}
36
37#[inherent]
38impl AstNode for Name<'_> {
39 /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
40 pub fn append_source(
41 &self,
42 sink: &mut String,
43 source: Option<&str>,
44 ) {
45 if let Some(src) = source {
46 append_span_source_slice(
47 self.span, sink, src,
48 );
49 }
50 }
51
52 /// Returns this name's byte-offset span within the
53 /// source text.
54 ///
55 /// The returned [`ByteSpan`] can be resolved to line/column
56 /// positions via [`source_span()`](Self::source_span) or
57 /// [`ByteSpan::resolve()`].
58 #[inline]
59 pub fn byte_span(&self) -> ByteSpan {
60 self.span
61 }
62
63 /// Resolves this name's position to line/column
64 /// coordinates using the given [`SourceMap`].
65 ///
66 /// Returns [`None`] if the byte offsets cannot be resolved
67 /// (e.g. the span was synthetically constructed without
68 /// valid position data).
69 #[inline]
70 pub fn source_span(
71 &self,
72 source_map: &SourceMap,
73 ) -> Option<SourceSpan> {
74 self.byte_span().resolve(source_map)
75 }
76}