1 //! Yet another version of owned string, backed by a syntax tree token.
3 use std::{cmp::Ordering, fmt, ops};
7 pub struct TokenText<'a>(pub(crate) Repr<'a>);
9 pub(crate) enum Repr<'a> {
14 impl<'a> TokenText<'a> {
15 pub(crate) fn borrowed(text: &'a str) -> Self {
16 TokenText(Repr::Borrowed(text))
19 pub(crate) fn owned(green: GreenToken) -> Self {
20 TokenText(Repr::Owned(green))
23 pub fn as_str(&self) -> &str {
25 Repr::Borrowed(it) => it,
26 Repr::Owned(ref green) => green.text(),
31 impl ops::Deref for TokenText<'_> {
34 fn deref(&self) -> &str {
38 impl AsRef<str> for TokenText<'_> {
39 fn as_ref(&self) -> &str {
44 impl From<TokenText<'_>> for String {
45 fn from(token_text: TokenText) -> Self {
46 token_text.as_str().into()
50 impl PartialEq<&'_ str> for TokenText<'_> {
51 fn eq(&self, other: &&str) -> bool {
52 self.as_str() == *other
55 impl PartialEq<TokenText<'_>> for &'_ str {
56 fn eq(&self, other: &TokenText) -> bool {
60 impl PartialEq<String> for TokenText<'_> {
61 fn eq(&self, other: &String) -> bool {
62 self.as_str() == other.as_str()
65 impl PartialEq<TokenText<'_>> for String {
66 fn eq(&self, other: &TokenText) -> bool {
70 impl PartialEq for TokenText<'_> {
71 fn eq(&self, other: &TokenText) -> bool {
72 self.as_str() == other.as_str()
75 impl Eq for TokenText<'_> {}
76 impl Ord for TokenText<'_> {
77 fn cmp(&self, other: &Self) -> Ordering {
78 self.as_str().cmp(other.as_str())
81 impl PartialOrd for TokenText<'_> {
82 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
86 impl fmt::Display for TokenText<'_> {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 fmt::Display::fmt(self.as_str(), f)
91 impl fmt::Debug for TokenText<'_> {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 fmt::Debug::fmt(self.as_str(), f)