sqruff_lib_core/parser/grammar/
conditional.rs1use crate::errors::SQLParseError;
2use crate::parser::IndentationConfig;
3use crate::parser::context::ParseContext;
4use crate::parser::match_result::{MatchResult, Span};
5use crate::parser::matchable::{Matchable, MatchableTrait};
6use crate::parser::segments::ErasedSegment;
7use crate::parser::segments::meta::Indent;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct Conditional {
11 meta: Indent,
12 requirements: IndentationConfig,
13}
14
15impl Conditional {
16 pub fn new(meta: Indent) -> Self {
17 Self {
18 meta,
19 requirements: IndentationConfig::default(),
20 }
21 }
22
23 fn require(mut self, flag: IndentationConfig) -> Self {
24 self.requirements.insert(flag);
25 self
26 }
27
28 pub fn indented_ctes(self) -> Self {
29 self.require(IndentationConfig::INDENTED_CTES)
30 }
31
32 pub fn indented_joins(self) -> Self {
33 self.require(IndentationConfig::INDENTED_JOINS)
34 }
35
36 pub fn indented_using_on(self) -> Self {
37 self.require(IndentationConfig::INDENTED_USING_ON)
38 }
39
40 pub fn indented_on_contents(self) -> Self {
41 self.require(IndentationConfig::INDENTED_ON_CONTENTS)
42 }
43
44 pub fn indented_then(self) -> Self {
45 self.require(IndentationConfig::INDENTED_THEN)
46 }
47
48 pub fn indented_then_contents(self) -> Self {
49 self.require(IndentationConfig::INDENTED_THEN_CONTENTS)
50 }
51
52 pub fn indented_joins_on(self) -> Self {
53 self.require(IndentationConfig::INDENTED_JOINS_ON)
54 }
55
56 fn is_enabled(&self, parse_context: &ParseContext) -> bool {
57 parse_context.indentation_config.contains(self.requirements)
58 }
59}
60
61impl MatchableTrait for Conditional {
62 fn elements(&self) -> &[Matchable] {
63 &[]
64 }
65
66 fn match_segments(
67 &self,
68 _segments: &[ErasedSegment],
69 idx: u32,
70 parse_context: &mut ParseContext,
71 ) -> Result<MatchResult, SQLParseError> {
72 if !self.is_enabled(parse_context) {
73 return Ok(MatchResult::empty_at(idx));
74 }
75
76 Ok(MatchResult {
77 span: Span {
78 start: idx,
79 end: idx,
80 },
81 insert_segments: vec![(idx, self.meta.kind)],
82 ..Default::default()
83 })
84 }
85}