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 path(&self) -> Option<ast::Path> {
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}
29impl AstNode for CreateTableLike {
30    #[inline]
31    fn can_cast(kind: ast::SyntaxKind) -> bool {
32        matches!(
33            kind,
34            ast::SyntaxKind::CREATE_TABLE | ast::SyntaxKind::CREATE_FOREIGN_TABLE
35        )
36    }
37    #[inline]
38    fn cast(syntax: SyntaxNode) -> Option<Self> {
39        if Self::can_cast(syntax.kind()) {
40            Some(Self { syntax })
41        } else {
42            None
43        }
44    }
45    #[inline]
46    fn syntax(&self) -> &SyntaxNode {
47        &self.syntax
48    }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Hash)]
52pub struct CreateViewLike {
53    pub(crate) syntax: SyntaxNode,
54}
55impl CreateViewLike {
56    #[inline]
57    pub fn column_list(&self) -> Option<ast::ColumnList> {
58        support::child(&self.syntax)
59    }
60    #[inline]
61    pub fn path(&self) -> Option<ast::Path> {
62        support::child(&self.syntax)
63    }
64    #[inline]
65    pub fn query(&self) -> Option<ast::SelectVariant> {
66        support::child(&self.syntax)
67    }
68}
69impl AstNode for CreateViewLike {
70    #[inline]
71    fn can_cast(kind: ast::SyntaxKind) -> bool {
72        matches!(
73            kind,
74            ast::SyntaxKind::CREATE_MATERIALIZED_VIEW | ast::SyntaxKind::CREATE_VIEW
75        )
76    }
77    #[inline]
78    fn cast(syntax: SyntaxNode) -> Option<Self> {
79        if Self::can_cast(syntax.kind()) {
80            Some(Self { syntax })
81        } else {
82            None
83        }
84    }
85    #[inline]
86    fn syntax(&self) -> &SyntaxNode {
87        &self.syntax
88    }
89}