1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pub mod srt;
pub mod ssa;
pub mod idx;
pub mod common;
use SubtitleFile;
use ParseSubtitleString;
use errors::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SubtitleFormat {
SubRip,
SubStationAlpha,
VobSubIdx,
}
pub fn get_subtitle_format_by_ending(path: &str) -> Option<SubtitleFormat> {
if path.ends_with(".srt") {
Some(SubtitleFormat::SubRip)
} else if path.ends_with(".ssa") || path.ends_with(".ass") {
Some(SubtitleFormat::SubStationAlpha)
} else if path.ends_with(".idx") {
Some(SubtitleFormat::VobSubIdx)
} else {
None
}
}
pub fn get_subtitle_format_by_ending_err(path: &str) -> Result<SubtitleFormat> {
match get_subtitle_format_by_ending(path) {
Some(format) => Ok(format),
None => Err(Error::from(ErrorKind::UnknownFileFormat)),
}
}
pub trait ClonableSubtitleFile: SubtitleFile {
fn clone(&self) -> Box<ClonableSubtitleFile>;
}
impl<T> ClonableSubtitleFile for T
where T: SubtitleFile + Clone + 'static
{
fn clone(&self) -> Box<ClonableSubtitleFile> {
Box::new(Clone::clone(self))
}
}
pub fn parse_file_from_string(format: SubtitleFormat, content: String) -> Result<Box<ClonableSubtitleFile>> {
match format {
SubtitleFormat::SubRip => Ok(Box::new(srt::SrtFile::parse_from_string(content)?)),
SubtitleFormat::SubStationAlpha => Ok(Box::new(ssa::SsaFile::parse_from_string(content)?)),
SubtitleFormat::VobSubIdx => Ok(Box::new(idx::IdxFile::parse_from_string(content)?)),
}
}