Skip to main content

libgraphql_parser/token/
str_graphql_token_source_config.rs

1/// Configuration for [`StrGraphQLTokenSource`](crate::token::StrGraphQLTokenSource)
2/// controlling which trivia types are captured during lexing.
3///
4/// All flags default to `true`, meaning the lexer operates in full-fidelity
5/// mode by default — every comment, comma, and whitespace run is preserved
6/// as trivia on the emitted tokens. Set individual flags to `false` to
7/// discard specific trivia types for leaner output.
8///
9/// # Example
10///
11/// ```rust
12/// use libgraphql_parser::token::StrGraphQLTokenSourceConfig;
13///
14/// // Full-fidelity (default): all trivia preserved
15/// let full = StrGraphQLTokenSourceConfig::default();
16/// assert!(full.retain_comments);
17/// assert!(full.retain_commas);
18/// assert!(full.retain_whitespace);
19///
20/// // Lean mode: discard all trivia
21/// let lean = StrGraphQLTokenSourceConfig::no_trivia();
22/// assert!(!lean.retain_comments);
23/// assert!(!lean.retain_commas);
24/// assert!(!lean.retain_whitespace);
25/// ```
26#[derive(Clone, Debug, PartialEq)]
27pub struct StrGraphQLTokenSourceConfig {
28    /// Whether to preserve `#` comments as
29    /// [`GraphQLTriviaToken::Comment`](crate::token::GraphQLTriviaToken::Comment)
30    /// on emitted tokens.
31    ///
32    /// When `false`, comments are still consumed (skipped) by the lexer but
33    /// not recorded as trivia.
34    pub retain_comments: bool,
35
36    /// Whether to preserve commas as
37    /// [`GraphQLTriviaToken::Comma`](crate::token::GraphQLTriviaToken::Comma)
38    /// on emitted tokens.
39    ///
40    /// When `false`, commas are still consumed (skipped) by the lexer but
41    /// not recorded as trivia.
42    pub retain_commas: bool,
43
44    /// Whether to preserve whitespace runs as
45    /// [`GraphQLTriviaToken::Whitespace`](crate::token::GraphQLTriviaToken::Whitespace)
46    /// on emitted tokens.
47    ///
48    /// When `false`, whitespace is still consumed (skipped) by the lexer but
49    /// not recorded as trivia. This is the most impactful flag for
50    /// performance since whitespace runs are frequent.
51    pub retain_whitespace: bool,
52}
53
54impl StrGraphQLTokenSourceConfig {
55    /// Returns a config that discards all trivia (comments, commas,
56    /// whitespace). Useful when only semantic tokens are needed and
57    /// source reconstruction is not required.
58    pub fn no_trivia() -> Self {
59        Self {
60            retain_comments: false,
61            retain_commas: false,
62            retain_whitespace: false,
63        }
64    }
65}
66
67impl Default for StrGraphQLTokenSourceConfig {
68    fn default() -> Self {
69        Self {
70            retain_comments: true,
71            retain_commas: true,
72            retain_whitespace: true,
73        }
74    }
75}