rust-analyzer/crates/syntax/src/token_text.rs

84 lines
1.9 KiB
Rust
Raw Normal View History

2021-03-26 18:33:45 +00:00
//! Yet another version of owned string, backed by a syntax tree token.
use std::{cmp::Ordering, fmt, ops};
pub enum TokenText<'a> {
Borrowed(&'a str),
Owned(rowan::GreenToken),
}
2021-03-26 18:33:45 +00:00
impl TokenText<'_> {
2021-03-26 18:33:45 +00:00
pub fn as_str(&self) -> &str {
match self {
TokenText::Borrowed(it) => *it,
TokenText::Owned(green) => green.text(),
}
2021-03-26 18:33:45 +00:00
}
}
impl ops::Deref for TokenText<'_> {
2021-03-26 18:33:45 +00:00
type Target = str;
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<TokenText<'_>> for String {
2021-03-26 18:33:45 +00:00
fn from(token_text: TokenText) -> Self {
token_text.as_str().into()
}
}
impl PartialEq<&'_ str> for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<TokenText<'_>> for &'_ str {
2021-03-26 18:33:45 +00:00
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq<String> for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<TokenText<'_>> for String {
2021-03-26 18:33:45 +00:00
fn eq(&self, other: &TokenText) -> bool {
other == self
}
}
impl PartialEq for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn eq(&self, other: &TokenText) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for TokenText<'_> {}
impl Ord for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl PartialOrd for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Display for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl fmt::Debug for TokenText<'_> {
2021-03-26 18:33:45 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}