panache_parser/syntax/
citations.rs1use super::{AstNode, PanacheLanguage, SyntaxKind, SyntaxNode, SyntaxToken};
4
5pub struct Citation(SyntaxNode);
6
7impl AstNode for Citation {
8 type Language = PanacheLanguage;
9
10 fn can_cast(kind: SyntaxKind) -> bool {
11 kind == SyntaxKind::CITATION
12 }
13
14 fn cast(syntax: SyntaxNode) -> Option<Self> {
15 if Self::can_cast(syntax.kind()) {
16 Some(Self(syntax))
17 } else {
18 None
19 }
20 }
21
22 fn syntax(&self) -> &SyntaxNode {
23 &self.0
24 }
25}
26
27impl Citation {
28 pub fn keys(&self) -> Vec<CitationKey> {
29 self.0
30 .children_with_tokens()
31 .filter_map(|element| element.into_token())
32 .filter(|token| token.kind() == SyntaxKind::CITATION_KEY)
33 .map(CitationKey)
34 .collect()
35 }
36
37 pub fn key_texts(&self) -> Vec<String> {
38 self.keys().into_iter().map(|key| key.text()).collect()
39 }
40}
41
42pub struct CitationKey(SyntaxToken);
43
44impl CitationKey {
45 pub fn text(&self) -> String {
46 self.0.text().to_string()
47 }
48
49 pub fn text_range(&self) -> rowan::TextRange {
50 self.0.text_range()
51 }
52}