use std::borrow::Cow;
use crate::Value;
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextString<'a>(Cow<'a, str>);
impl<'a> TextString<'a> {
pub const fn new() -> Self {
Self(Cow::Borrowed(""))
}
pub fn as_str(&self) -> &str {
self.0.as_ref()
}
pub fn as_string_mut(&mut self) -> &mut String {
self.0.to_mut()
}
pub fn into_owned<'b>(self) -> TextString<'b> {
match self.0 {
Cow::Borrowed(text) => TextString(text.to_string().into()),
Cow::Owned(text) => TextString(text.into()),
}
}
}
impl<'a> From<char> for TextString<'a> {
fn from(value: char) -> Self {
Self(value.to_string().into())
}
}
impl<'a, T> From<&'a T> for TextString<'a>
where
T: AsRef<str> + ?Sized,
{
fn from(value: &'a T) -> Self {
Self(value.as_ref().into())
}
}
impl<'a> From<String> for TextString<'a> {
fn from(value: String) -> Self {
Self(value.into())
}
}
impl<'a> From<Cow<'a, str>> for TextString<'a> {
fn from(value: Cow<'a, str>) -> Self {
Self(value)
}
}
impl<'a> From<TextString<'a>> for Value<'a> {
fn from(value: TextString<'a>) -> Self {
Self::TextString(value.0)
}
}