1use std::borrow::Cow;
2
3use crate::{
4 green::{GreenNode, GreenToken, SyntaxKind},
5 GreenNodeData, NodeOrToken, TextSize,
6};
7
8use super::GreenTokenData;
9
10pub(super) type GreenElement = NodeOrToken<GreenNode, GreenToken>;
11pub(crate) type GreenElementRef<'a> = NodeOrToken<&'a GreenNodeData, &'a GreenTokenData>;
12
13impl From<GreenNode> for GreenElement {
14 #[inline]
15 fn from(node: GreenNode) -> GreenElement {
16 NodeOrToken::Node(node)
17 }
18}
19
20impl<'a> From<&'a GreenNode> for GreenElementRef<'a> {
21 #[inline]
22 fn from(node: &'a GreenNode) -> GreenElementRef<'a> {
23 NodeOrToken::Node(node)
24 }
25}
26
27impl From<GreenToken> for GreenElement {
28 #[inline]
29 fn from(token: GreenToken) -> GreenElement {
30 NodeOrToken::Token(token)
31 }
32}
33
34impl From<Cow<'_, GreenNodeData>> for GreenElement {
35 #[inline]
36 fn from(cow: Cow<'_, GreenNodeData>) -> Self {
37 NodeOrToken::Node(cow.into_owned())
38 }
39}
40
41impl<'a> From<&'a GreenToken> for GreenElementRef<'a> {
42 #[inline]
43 fn from(token: &'a GreenToken) -> GreenElementRef<'a> {
44 NodeOrToken::Token(token)
45 }
46}
47
48impl GreenElementRef<'_> {
49 pub fn to_owned(self) -> GreenElement {
50 match self {
51 NodeOrToken::Node(it: &GreenNodeData) => NodeOrToken::Node(it.to_owned()),
52 NodeOrToken::Token(it: &GreenTokenData) => NodeOrToken::Token(it.to_owned()),
53 }
54 }
55}
56
57impl GreenElement {
58 /// Returns kind of this element.
59 #[inline]
60 pub fn kind(&self) -> SyntaxKind {
61 self.as_deref().kind()
62 }
63
64 /// Returns the length of the text covered by this element.
65 #[inline]
66 pub fn text_len(&self) -> TextSize {
67 self.as_deref().text_len()
68 }
69}
70
71impl GreenElementRef<'_> {
72 /// Returns kind of this element.
73 #[inline]
74 pub fn kind(&self) -> SyntaxKind {
75 match self {
76 NodeOrToken::Node(it: &&GreenNodeData) => it.kind(),
77 NodeOrToken::Token(it: &&GreenTokenData) => it.kind(),
78 }
79 }
80
81 /// Returns the length of the text covered by this element.
82 #[inline]
83 pub fn text_len(self) -> TextSize {
84 match self {
85 NodeOrToken::Node(it: &GreenNodeData) => it.text_len(),
86 NodeOrToken::Token(it: &GreenTokenData) => it.text_len(),
87 }
88 }
89}
90