use core::fmt;
use std::ops::Deref;
use crate::haystack::val::Value;
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Clone, Debug, Default)]
pub struct Str {
pub value: String,
}
impl Str {
pub fn make(val: &str) -> Self {
Str { value: val.into() }
}
pub fn as_str(&self) -> &str {
self.value.as_str()
}
}
impl From<String> for Str {
fn from(value: String) -> Self {
Str { value }
}
}
impl From<&str> for Str {
fn from(value: &str) -> Self {
Str {
value: value.to_owned(),
}
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Value::Str(value.into())
}
}
impl From<Str> for Value {
fn from(value: Str) -> Self {
Value::Str(value)
}
}
impl TryFrom<&Value> for String {
type Error = &'static str;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Str(v) => Ok(v.value.clone()),
_ => Err("Value is not an `Str`"),
}
}
}
impl TryFrom<&Value> for Str {
type Error = &'static str;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Str(v) => Ok(v.clone()),
_ => Err("Value is not an `Str`"),
}
}
}
impl fmt::Display for Str {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.value)
}
}
impl Deref for Str {
type Target = str;
fn deref(&self) -> &str {
&self.value
}
}
impl AsRef<str> for Str {
fn as_ref(&self) -> &str {
&self.value
}
}
impl From<Str> for String {
fn from(s: Str) -> String {
s.value
}
}
impl From<String> for Value {
fn from(value: String) -> Self {
Value::Str(value.into())
}
}
impl PartialEq<str> for Str {
fn eq(&self, other: &str) -> bool {
self.value == other
}
}
impl PartialEq<String> for Str {
fn eq(&self, other: &String) -> bool {
self.value == *other
}
}