use super::Tag;
pub trait TagCodec: Sized {
type Error: core::error::Error + Send + Sync;
fn parse<I, S>(tag: I) -> Result<Self, Self::Error>
where
I: IntoIterator<Item = S>,
S: AsRef<str>;
fn to_tag(&self) -> Tag;
}
#[macro_export]
macro_rules! impl_tag_codec_conversions {
($ty:ty) => {
impl From<&$ty> for Tag {
#[inline]
fn from(value: &$ty) -> Self {
value.to_tag()
}
}
impl From<$ty> for Tag {
#[inline]
fn from(value: $ty) -> Self {
value.to_tag()
}
}
impl TryFrom<&Tag> for $ty {
type Error = <$ty as TagCodec>::Error;
#[inline]
fn try_from(tag: &Tag) -> Result<Self, Self::Error> {
<$ty as TagCodec>::parse(tag.as_slice())
}
}
impl TryFrom<Tag> for $ty {
type Error = <$ty as TagCodec>::Error;
#[inline]
fn try_from(tag: Tag) -> Result<Self, Self::Error> {
<$ty as TagCodec>::parse(tag.as_slice())
}
}
};
}
pub use impl_tag_codec_conversions;