1use std::{cmp::Ordering, fmt, ops};
4
5use rowan::GreenToken;
6use smol_str::SmolStr;
7
8pub struct TokenText<'a>(pub(crate) Repr<'a>);
9
10pub(crate) enum Repr<'a> {
11 Borrowed(&'a str),
12 Owned(GreenToken),
13}
14
15impl<'a> TokenText<'a> {
16 pub fn borrowed(text: &'a str) -> Self {
17 TokenText(Repr::Borrowed(text))
18 }
19
20 pub(crate) fn owned(green: GreenToken) -> Self {
21 TokenText(Repr::Owned(green))
22 }
23
24 pub fn as_str(&self) -> &str {
25 match &self.0 {
26 &Repr::Borrowed(it) => it,
27 Repr::Owned(green) => green.text(),
28 }
29 }
30}
31
32impl ops::Deref for TokenText<'_> {
33 type Target = str;
34
35 fn deref(&self) -> &str {
36 self.as_str()
37 }
38}
39impl AsRef<str> for TokenText<'_> {
40 fn as_ref(&self) -> &str {
41 self.as_str()
42 }
43}
44
45impl From<TokenText<'_>> for String {
46 fn from(token_text: TokenText<'_>) -> Self {
47 token_text.as_str().into()
48 }
49}
50
51impl From<TokenText<'_>> for SmolStr {
52 fn from(token_text: TokenText<'_>) -> Self {
53 SmolStr::new(token_text.as_str())
54 }
55}
56
57impl PartialEq<&'_ str> for TokenText<'_> {
58 fn eq(&self, other: &&str) -> bool {
59 self.as_str() == *other
60 }
61}
62impl PartialEq<TokenText<'_>> for &'_ str {
63 fn eq(&self, other: &TokenText<'_>) -> bool {
64 other == self
65 }
66}
67impl PartialEq<String> for TokenText<'_> {
68 fn eq(&self, other: &String) -> bool {
69 self.as_str() == other.as_str()
70 }
71}
72impl PartialEq<TokenText<'_>> for String {
73 fn eq(&self, other: &TokenText<'_>) -> bool {
74 other == self
75 }
76}
77impl PartialEq for TokenText<'_> {
78 fn eq(&self, other: &TokenText<'_>) -> bool {
79 self.as_str() == other.as_str()
80 }
81}
82impl Eq for TokenText<'_> {}
83impl Ord for TokenText<'_> {
84 fn cmp(&self, other: &Self) -> Ordering {
85 self.as_str().cmp(other.as_str())
86 }
87}
88impl PartialOrd for TokenText<'_> {
89 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
90 Some(self.cmp(other))
91 }
92}
93impl fmt::Display for TokenText<'_> {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 fmt::Display::fmt(self.as_str(), f)
96 }
97}
98impl fmt::Debug for TokenText<'_> {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 fmt::Debug::fmt(self.as_str(), f)
101 }
102}