Skip to content

Commit 0a156c8

Browse files
committed
Hide implementation details of TokenText
1 parent d9b4ac8 commit 0a156c8

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

crates/syntax/src/ast/node_ext.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
4040
}
4141

4242
match node.green() {
43-
Cow::Borrowed(green_ref) => TokenText::Borrowed(first_token(green_ref).text()),
44-
Cow::Owned(green) => TokenText::Owned(first_token(&green).to_owned()),
43+
Cow::Borrowed(green_ref) => TokenText::borrowed(first_token(green_ref).text()),
44+
Cow::Owned(green) => TokenText::owned(first_token(&green).to_owned()),
4545
}
4646
}
4747

crates/syntax/src/token_text.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22
33
use std::{cmp::Ordering, fmt, ops};
44

5-
pub enum TokenText<'a> {
5+
use rowan::GreenToken;
6+
7+
pub struct TokenText<'a>(pub(crate) Repr<'a>);
8+
9+
pub(crate) enum Repr<'a> {
610
Borrowed(&'a str),
7-
Owned(rowan::GreenToken),
11+
Owned(GreenToken),
812
}
913

10-
impl TokenText<'_> {
14+
impl<'a> TokenText<'a> {
15+
pub(crate) fn borrowed(text: &'a str) -> Self {
16+
TokenText(Repr::Borrowed(text))
17+
}
18+
19+
pub(crate) fn owned(green: GreenToken) -> Self {
20+
TokenText(Repr::Owned(green))
21+
}
22+
1123
pub fn as_str(&self) -> &str {
12-
match self {
13-
TokenText::Borrowed(it) => *it,
14-
TokenText::Owned(green) => green.text(),
24+
match self.0 {
25+
Repr::Borrowed(it) => it,
26+
Repr::Owned(ref green) => green.text(),
1527
}
1628
}
1729
}

0 commit comments

Comments
 (0)