use std::{
borrow::Cow,
fmt::{Display, Formatter, Result},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Indexer<'a> {
Number(usize),
String(Cow<'a, str>),
Empty,
}
impl<'a> Indexer<'a> {
pub fn into_owned(self) -> Indexer<'static> {
match self {
Indexer::Number(n) => Indexer::Number(n),
Indexer::String(cow) => Indexer::String(Cow::Owned(cow.into_owned())),
Indexer::Empty => Indexer::Empty,
}
}
}
impl From<usize> for Indexer<'_> {
fn from(f: usize) -> Self {
Self::Number(f)
}
}
impl From<String> for Indexer<'static> {
fn from(f: String) -> Self {
Self::String(crate::decode(f))
}
}
impl<'a> From<&'a String> for Indexer<'a> {
fn from(f: &'a String) -> Self {
Self::String(crate::decode(f))
}
}
impl<'a> From<Cow<'a, str>> for Indexer<'a> {
fn from(value: Cow<'a, str>) -> Self {
Self::String(crate::decode(value))
}
}
impl<'a> From<&'a str> for Indexer<'a> {
fn from(f: &'a str) -> Self {
Self::String(crate::decode(f))
}
}
impl From<()> for Indexer<'_> {
fn from(_: ()) -> Self {
Self::Empty
}
}
impl Display for Indexer<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Indexer::Number(n) => f.write_str(&n.to_string()),
Indexer::String(s) => f.write_str(&crate::encode(s)),
Indexer::Empty => Ok(()),
}
}
}