compose_syntax/ast/
bindings.rs1use crate::ast::func::Pattern;
2use crate::ast::{node, AstNode, Expr, Ident};
3use crate::kind::SyntaxKind;
4use crate::{Span, SyntaxNode};
5
6node! {
7 struct LetBinding
8}
9
10impl<'a> LetBinding<'a> {
11 pub fn has_initial_value(self) -> bool {
12 self.0.children().any(|n| n.kind() == SyntaxKind::Eq)
13 }
14
15 pub fn initial_value(self) -> Option<Expr<'a>> {
16 self.0.children().filter_map(SyntaxNode::cast).nth(1)
17 }
18
19 fn bindings(self) -> Vec<Ident<'a>> {
20 let pattern: Pattern = self.0.try_cast_first().expect("expected pattern");
21 pattern.bindings()
22 }
23
24 pub fn pattern(self) -> Pattern<'a> {
25 self.0.cast_first()
26 }
27
28 pub fn is_mut(self) -> bool {
29 self.0.children().any(|n| n.kind() == SyntaxKind::Mut)
30 }
31
32 pub fn eq_span(self) -> Span {
33 self.0.children().find(|&n| n.kind() == SyntaxKind::Eq).map(|n| n.span()).unwrap_or_else(|| self.0.span())
34 }
35
36 pub fn mut_span(self) -> Option<Span> {
37 self.0.children().find(|&n| n.kind() == SyntaxKind::Mut).map(|n| n.span())
38 }
39
40 pub fn initial_value_span(self) -> Option<Span> {
41 self.initial_value().map(|e| e.span())
42 }
43
44 pub fn is_public(self) -> bool {
45 self.0.children().any(|n| n.kind() == SyntaxKind::Pub)
46 }
47
48 pub fn pub_span(self) -> Option<Span> {
49 self.0.children().find(|&n| n.kind() == SyntaxKind::Pub).map(|n| n.span())
50 }
51}
52
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57 use crate::assert_ast;
58 use crate::ast::Int;
59
60 #[test]
61 fn test_let_binding() {
62 assert_ast!(
63 "let x = 1",
64 binding as LetBinding {
65 binding.bindings().into_iter() => [
66 ident as Ident {
67 assert_eq!(ident.get(), "x");
68 }
69 ]
70 with init: Int = binding.initial_value().unwrap() => {
71 assert_eq!(init.get(), 1);
72 }
73 }
74 )
75 }
76}