use {
alloc::{
borrow::Cow,
string::{String, ToString},
},
crate::{Error, Json, Result},
};
impl Json {
pub fn as_str(&self) -> Result<&str> {
match self {
Json::String(s) => Ok(s),
_ => Err(e!("Json is not a String")),
}
}
pub fn as_mut_str(&mut self) -> Result<&mut String> {
match self {
Json::String(s) => Ok(s),
_ => Err(e!("Json is not a String")),
}
}
}
impl From<String> for Json {
fn from(s: String) -> Self {
Json::String(s)
}
}
impl From<&str> for Json {
fn from(s: &str) -> Self {
Json::String(s.to_string())
}
}
impl From<Cow<'_, str>> for Json {
fn from(s: Cow<str>) -> Self {
Self::from(s.into_owned())
}
}
impl TryFrom<Json> for String {
type Error = Error;
fn try_from(value: Json) -> core::result::Result<Self, Self::Error> {
match value {
Json::String(s) => Ok(s),
_ => Err(e!("Json is not a String")),
}
}
}
impl PartialEq<String> for Json {
fn eq(&self, other: &String) -> bool {
match self {
Self::String(s) => s == other,
_ => false,
}
}
}
impl PartialEq<Json> for String {
fn eq(&self, other: &Json) -> bool {
match other {
Json::String(s) => self == s,
_ => false,
}
}
}
impl PartialEq<str> for Json {
fn eq(&self, other: &str) -> bool {
match self {
Self::String(s) => s == other,
_ => false,
}
}
}
impl PartialEq<Json> for str {
fn eq(&self, other: &Json) -> bool {
match other {
Json::String(s) => self == s,
_ => false,
}
}
}