use std::borrow::Cow;
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub enum DecodedEntity<'a> {
Standard(char),
Custom(Cow<'a, str>),
}
impl Default for DecodedEntity<'_> {
fn default() -> Self {
Self::Custom(Cow::Borrowed(""))
}
}
impl<'a> DecodedEntity<'a> {
#[inline]
pub fn push_to(self, buf: &mut String) {
match self {
Self::Standard(c) => buf.push(c),
Self::Custom(s) => buf.push_str(&s),
}
}
#[inline]
pub fn encode(&'a self, buf: &'a mut [u8]) -> &'a str {
match self {
Self::Standard(c) => c.encode_utf8(buf),
Self::Custom(s) => s,
}
}
}
impl From<char> for DecodedEntity<'_> {
#[inline]
fn from(value: char) -> Self {
Self::Standard(value)
}
}
impl<'a> From<&'a str> for DecodedEntity<'a> {
#[inline]
fn from(value: &'a str) -> Self {
Self::Custom(value.into())
}
}
impl<'a> From<Cow<'a, str>> for DecodedEntity<'a> {
#[inline]
fn from(value: Cow<'a, str>) -> Self {
Self::Custom(value)
}
}
impl From<String> for DecodedEntity<'_> {
#[inline]
fn from(value: String) -> Self {
Self::Custom(value.into())
}
}
impl fmt::Debug for DecodedEntity<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Standard(c) => c.fmt(f),
Self::Custom(s) => s.fmt(f),
}
}
}
impl fmt::Display for DecodedEntity<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Standard(c) => c.fmt(f),
Self::Custom(s) => s.fmt(f),
}
}
}
impl PartialEq<char> for DecodedEntity<'_> {
fn eq(&self, other: &char) -> bool {
match self {
Self::Standard(ch) => ch == other,
Self::Custom(_) => false,
}
}
}
impl<'a> PartialEq<DecodedEntity<'a>> for char {
fn eq(&self, other: &DecodedEntity<'a>) -> bool {
*other == *self
}
}
impl PartialEq<str> for DecodedEntity<'_> {
fn eq(&self, other: &str) -> bool {
match self {
Self::Standard(_) => false,
Self::Custom(s) => **s == *other,
}
}
}
impl<'a> PartialEq<DecodedEntity<'a>> for str {
fn eq(&self, other: &DecodedEntity<'a>) -> bool {
*other == *self
}
}