use crate::ion_data::{IonDataHash, IonDataOrd, IonEq};
use crate::text::text_formatter::FmtValueFormatter;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
#[derive(Eq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct Str {
text: String,
}
impl Str {
pub fn len(&self) -> usize {
self.text.len()
}
pub fn is_empty(&self) -> bool {
self.text.is_empty()
}
pub fn text(&self) -> &str {
self.text.as_str()
}
}
impl Display for Str {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut formatter = FmtValueFormatter { output: f };
formatter
.format_string(self.as_ref())
.map_err(|_| std::fmt::Error)
}
}
impl From<&str> for Str {
fn from(value: &str) -> Self {
Str {
text: value.to_string(),
}
}
}
impl From<String> for Str {
fn from(value: String) -> Self {
Str { text: value }
}
}
impl From<Str> for String {
fn from(value: Str) -> Self {
value.text
}
}
impl AsRef<str> for Str {
fn as_ref(&self) -> &str {
self.text()
}
}
impl<S> PartialEq<S> for Str
where
S: AsRef<str>,
{
fn eq(&self, other: &S) -> bool {
let other_text: &str = other.as_ref();
self.text() == other_text
}
}
impl PartialEq<Str> for &str {
fn eq(&self, other: &Str) -> bool {
self.eq(&other.text())
}
}
impl PartialEq<Str> for String {
fn eq(&self, other: &Str) -> bool {
let self_text: &str = self.as_str();
self_text.eq(other.text())
}
}
impl IonEq for Str {
fn ion_eq(&self, other: &Self) -> bool {
self == other
}
}
impl IonDataOrd for Str {
fn ion_cmp(&self, other: &Self) -> Ordering {
self.cmp(other)
}
}
impl IonDataHash for Str {
fn ion_data_hash<H: Hasher>(&self, state: &mut H) {
self.hash(state)
}
}