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};
|
|
|
|
|
2021-05-06 05:07:06 +00:00
|
|
|
use rowan::GreenToken;
|
|
|
|
|
|
|
|
pub struct TokenText<'a>(pub(crate) Repr<'a>);
|
|
|
|
|
|
|
|
pub(crate) enum Repr<'a> {
|
2021-05-06 05:07:06 +00:00
|
|
|
Borrowed(&'a str),
|
2021-05-06 05:07:06 +00:00
|
|
|
Owned(GreenToken),
|
2021-05-06 05:07:06 +00:00
|
|
|
}
|
2021-03-26 18:33:45 +00:00
|
|
|
|
2021-05-06 05:07:06 +00:00
|
|
|
impl<'a> TokenText<'a> {
|
|
|
|
pub(crate) fn borrowed(text: &'a str) -> Self {
|
|
|
|
TokenText(Repr::Borrowed(text))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn owned(green: GreenToken) -> Self {
|
|
|
|
TokenText(Repr::Owned(green))
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:33:45 +00:00
|
|
|
pub fn as_str(&self) -> &str {
|
2021-09-16 16:03:37 +00:00
|
|
|
match &self.0 {
|
|
|
|
&Repr::Borrowed(it) => it,
|
|
|
|
Repr::Owned(green) => green.text(),
|
2021-05-06 05:07:06 +00:00
|
|
|
}
|
2021-03-26 18:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:07:06 +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()
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
impl AsRef<str> for TokenText<'_> {
|
2021-03-26 18:33:45 +00:00
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.as_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:07:06 +00:00
|
|
|
impl From<TokenText<'_>> for String {
|
2022-07-20 13:02:08 +00:00
|
|
|
fn from(token_text: TokenText<'_>) -> Self {
|
2021-03-26 18:33:45 +00:00
|
|
|
token_text.as_str().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:07:06 +00:00
|
|
|
impl PartialEq<&'_ str> for TokenText<'_> {
|
2021-03-26 18:33:45 +00:00
|
|
|
fn eq(&self, other: &&str) -> bool {
|
|
|
|
self.as_str() == *other
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
impl PartialEq<TokenText<'_>> for &'_ str {
|
2022-07-20 13:02:08 +00:00
|
|
|
fn eq(&self, other: &TokenText<'_>) -> bool {
|
2021-03-26 18:33:45 +00:00
|
|
|
other == self
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
impl PartialEq<TokenText<'_>> for String {
|
2022-07-20 13:02:08 +00:00
|
|
|
fn eq(&self, other: &TokenText<'_>) -> bool {
|
2021-03-26 18:33:45 +00:00
|
|
|
other == self
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
impl PartialEq for TokenText<'_> {
|
2022-07-20 13:02:08 +00:00
|
|
|
fn eq(&self, other: &TokenText<'_>) -> bool {
|
2021-03-26 18:33:45 +00:00
|
|
|
self.as_str() == other.as_str()
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
impl PartialOrd for TokenText<'_> {
|
2021-03-26 18:33:45 +00:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 05:07:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|