Borrow text from nodes of immutable syntax trees

This commit is contained in:
Dawer 2021-05-06 10:07:06 +05:00
parent dc4fa504ea
commit d7e169fe55
2 changed files with 49 additions and 25 deletions

View file

@ -2,75 +2,81 @@
use std::{cmp::Ordering, fmt, ops};
pub struct TokenText(pub(crate) rowan::GreenToken);
pub enum TokenText<'a> {
Borrowed(&'a str),
Owned(rowan::GreenToken),
}
impl TokenText {
impl TokenText<'_> {
pub fn as_str(&self) -> &str {
self.0.text()
match self {
TokenText::Borrowed(it) => *it,
TokenText::Owned(green) => green.text(),
}
}
}
impl ops::Deref for TokenText {
impl ops::Deref for TokenText<'_> {
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for TokenText {
impl AsRef<str> for TokenText<'_> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<TokenText> for String {
impl From<TokenText<'_>> for String {
fn from(token_text: TokenText) -> Self {
token_text.as_str().into()
}
}
impl PartialEq<&'_ str> for TokenText {
impl PartialEq<&'_ str> for TokenText<'_> {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<TokenText> for &'_ str {
impl PartialEq<TokenText<'_>> for &'_ str {
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq<String> for TokenText {
impl PartialEq<String> for TokenText<'_> {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<TokenText> for String {
impl PartialEq<TokenText<'_>> for String {
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq for TokenText {
impl PartialEq for TokenText<'_> {
fn eq(&self, other: &TokenText) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for TokenText {}
impl Ord for TokenText {
impl Eq for TokenText<'_> {}
impl Ord for TokenText<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl PartialOrd for TokenText {
impl PartialOrd for TokenText<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Display for TokenText {
impl fmt::Display for TokenText<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl fmt::Debug for TokenText {
impl fmt::Debug for TokenText<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}