use std::path::Path;
use crate::{
detection::detect_format_with_encoding, encoding::detect_file_encoding, AssSubtitle, Error,
SsaSubtitle, SubRipSubtitle, Subtitle, TimedMicroDvdSubtitle, WebVttSubtitle,
};
#[derive(Debug)]
pub enum TimedSubtitleFile {
Ass(AssSubtitle),
MicroDvd(TimedMicroDvdSubtitle),
Ssa(SsaSubtitle),
SubRip(SubRipSubtitle),
WebVtt(WebVttSubtitle),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Format {
Ass,
MicroDvd,
Ssa,
SubRip,
WebVtt,
}
impl TimedSubtitleFile {
pub fn new(path: impl AsRef<Path>) -> Result<Self, Error> {
let encoding = detect_file_encoding(path.as_ref(), None).ok();
let format = detect_format_with_encoding(path.as_ref(), encoding)?;
match format {
Format::Ass => {
AssSubtitle::from_path_with_encoding(path.as_ref(), encoding).map(Self::Ass)
}
Format::MicroDvd => {
TimedMicroDvdSubtitle::from_path_with_encoding(path.as_ref(), encoding)
.map(Self::MicroDvd)
}
Format::Ssa => {
SsaSubtitle::from_path_with_encoding(path.as_ref(), encoding).map(Self::Ssa)
}
Format::SubRip => {
SubRipSubtitle::from_path_with_encoding(path.as_ref(), encoding).map(Self::SubRip)
}
Format::WebVtt => {
WebVttSubtitle::from_path_with_encoding(path.as_ref(), encoding).map(Self::WebVtt)
}
}
}
pub fn with_format(path: impl AsRef<Path>, format: Format) -> Result<Self, Error> {
match format {
Format::Ass => AssSubtitle::from_path(path.as_ref()).map(Self::Ass),
Format::MicroDvd => TimedMicroDvdSubtitle::from_path(path.as_ref()).map(Self::MicroDvd),
Format::Ssa => SsaSubtitle::from_path(path.as_ref()).map(Self::Ssa),
Format::SubRip => SubRipSubtitle::from_path(path.as_ref()).map(Self::SubRip),
Format::WebVtt => WebVttSubtitle::from_path(path.as_ref()).map(Self::WebVtt),
}
}
pub fn export(&self, path: impl AsRef<Path>) -> Result<(), Error> {
match self {
Self::Ass(data) => data.export(path.as_ref()),
Self::MicroDvd(data) => data.export(path.as_ref()),
Self::Ssa(data) => data.export(path.as_ref()),
Self::SubRip(data) => data.export(path.as_ref()),
Self::WebVtt(data) => data.export(path.as_ref()),
}
}
}