use std::fmt;
use std::str::FromStr;
macro_rules! element_kinds {
($($variant:ident => $name:literal),+ $(,)?) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ElementKind {
$($variant),+
}
impl ElementKind {
pub fn as_str(self) -> &'static str {
match self {
$(ElementKind::$variant => $name),+
}
}
}
impl FromStr for ElementKind {
type Err = ParseElementKindError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
$($name => ElementKind::$variant,)+
_ => return Err(ParseElementKindError),
})
}
}
};
}
element_kinds! {
AudioTerm => "audio_term",
Device => "device",
Episode => "episode",
EpisodeTitle => "episode_title",
FileChecksum => "file_checksum",
FileExtension => "file_extension",
Language => "language",
Other => "other",
Part => "part",
ReleaseGroup => "release_group",
ReleaseInformation => "release_information",
ReleaseVersion => "release_version",
Season => "season",
Source => "source",
Subtitles => "subtitles",
Title => "title",
Type => "type",
VideoResolution => "video_resolution",
VideoTerm => "video_term",
Volume => "volume",
Year => "year",
}
impl fmt::Display for ElementKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseElementKindError;
impl fmt::Display for ParseElementKindError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("not a valid ElementKind")
}
}
impl std::error::Error for ParseElementKindError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Element {
pub kind: ElementKind,
pub value: String,
pub position: usize,
}