Skip to main content

squawk_syntax/ast/
nodes.rs

1pub use crate::ast::generated::nodes::*;
2use crate::{
3    SyntaxNode,
4    ast::{self, AstNode, support},
5};
6
7// TODO: Initial attempt to try and unify the CreateTable and
8// CreateForeignTable. Not sure this is the right approach, we may want to be
9// more general, like TableSource, which can be a View, CTE, Table,
10// ForeignTable, Subquery, etc.
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct CreateTableLike {
13    pub(crate) syntax: SyntaxNode,
14}
15impl CreateTableLike {
16    #[inline]
17    pub fn table_name(&self) -> Option<ast::TableName> {
18        support::child(&self.syntax)
19    }
20    #[inline]
21    pub fn table_arg_list(&self) -> Option<ast::TableArgList> {
22        support::child(&self.syntax)
23    }
24    #[inline]
25    pub fn inherits(&self) -> Option<ast::Inherits> {
26        support::child(&self.syntax)
27    }
28    #[inline]
29    pub fn partition_of(&self) -> Option<ast::PartitionOf> {
30        support::child(&self.syntax)
31    }
32}
33impl AstNode for CreateTableLike {
34    #[inline]
35    fn can_cast(kind: ast::SyntaxKind) -> bool {
36        matches!(
37            kind,
38            ast::SyntaxKind::CREATE_TABLE | ast::SyntaxKind::CREATE_FOREIGN_TABLE
39        )
40    }
41    #[inline]
42    fn cast(syntax: SyntaxNode) -> Option<Self> {
43        if Self::can_cast(syntax.kind()) {
44            Some(Self { syntax })
45        } else {
46            None
47        }
48    }
49    #[inline]
50    fn syntax(&self) -> &SyntaxNode {
51        &self.syntax
52    }
53}
54
55#[derive(Debug, Clone, PartialEq, Eq, Hash)]
56pub struct CreateViewLike {
57    pub(crate) syntax: SyntaxNode,
58}
59impl CreateViewLike {
60    #[inline]
61    pub fn column_list(&self) -> Option<ast::ColumnList> {
62        support::child(&self.syntax)
63    }
64    #[inline]
65    pub fn view(&self) -> Option<ast::View> {
66        support::child(&self.syntax)
67    }
68    #[inline]
69    pub fn query(&self) -> Option<ast::SelectVariant> {
70        support::child(&self.syntax)
71    }
72}
73impl AstNode for CreateViewLike {
74    #[inline]
75    fn can_cast(kind: ast::SyntaxKind) -> bool {
76        matches!(
77            kind,
78            ast::SyntaxKind::CREATE_MATERIALIZED_VIEW | ast::SyntaxKind::CREATE_VIEW
79        )
80    }
81    #[inline]
82    fn cast(syntax: SyntaxNode) -> Option<Self> {
83        if Self::can_cast(syntax.kind()) {
84            Some(Self { syntax })
85        } else {
86            None
87        }
88    }
89    #[inline]
90    fn syntax(&self) -> &SyntaxNode {
91        &self.syntax
92    }
93}