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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! Implements helpers for `.srt`.
//!
//! It describes the [SRTFile] and [SRTLine] structs and
//! provides the [parse] function.

use serde::Deserialize;
use serde::Serialize;

use crate::util::time::Time;
use std::fmt::Display;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::str;
use std::str::FromStr;

use super::ssa::{SSAEvent, SSAFile};
use super::vtt::VTTFile;
use super::vtt::VTTLine;

/// Contains a Vec<[SRTLine]>
///
/// The `.srt` format is relatively simple to parse and generally looks like :
///```text
/// 0
/// 00:00:00,000 --> 00:00:02,000
/// This is my text
///
/// 1
/// 00:00:02,000 --> 00:00:04,000
/// This is my second text
/// ```
///
/// The [parse] function takes as input a [String] that represents either the parsed file or the path to it.
///
/// A simple example of working with this would be the following :
///
///
/// ```
/// use rsubs_lib::srt::parse;
///
/// let mut a = parse("./tests/fixtures/test.srt".to_string()).unwrap();
/// for line in a.lines.iter_mut(){
///     line.line_text.push_str(" Ipsum"); // add "Ipsum" to the end of each line.
/// }
/// // print the parsed and modified `.srt` file
/// println!("{}",a.clone());
///
/// // and then write it to a file
/// a.to_file("./tests/fixtures/doctest1.srt");
///
/// ```
#[derive(Debug, Clone, PartialEq, Default, Eq, Serialize, Deserialize)]
pub struct SRTFile {
    pub lines: Vec<SRTLine>,
}

/// Describes each line
///
/// Each line has a start and end [Time], a [String] text and an [i32] line number.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SRTLine {
    pub line_number: i32,
    pub line_text: String,
    pub line_start: Time,
    pub line_end: Time,
}
impl Default for SRTLine {
    fn default() -> Self {
        SRTLine {
            line_number: 0,
            line_text: "".to_string(),
            line_start: Time::default(),
            line_end: Time::default(),
        }
    }
}

impl SRTFile {
    /// Convert from [SRTFile] to [SSAFile] replacing `\r\n` to `\\N` since SSA/ASS is single line
    pub fn to_ass(self) -> SSAFile {
        let mut ssa = SSAFile::default();
        ssa.events.clear();
        for line in self.lines {
            let event = SSAEvent {
                line_start: line.line_start,
                line_end: line.line_end,
                line_text: line.line_text.replace("\r\n", "\\N"),
                ..Default::default()
            };
            ssa.events.push(event);
        }
        ssa
    }
    /// Convert from [SRTFile] to [VTTFile], WebVTT at its core is exactly the same as Subrip
    pub fn to_vtt(self) -> VTTFile {
        let mut vtt = VTTFile::default();
        vtt.lines.clear();
        for line in self.lines {
            let vttline = VTTLine {
                line_number: line.line_number.to_string(),
                line_start: line.line_start,
                line_end: line.line_end,
                line_text: line.line_text,
                ..Default::default()
            };
            vtt.lines.push(vttline);
        }
        vtt
    }
    /// Takes the path of the file in the form of a [String] to be written to as input.
    pub fn to_file(self, path: &str) -> std::io::Result<()> {
        let mut w = File::options()
            .write(true)
            .create(true)
            .open(path)
            .expect("File can't be created");
        write!(w, "{self}")?;
        Ok(())
    }
}
impl From<VTTFile> for SRTFile {
    fn from(a: VTTFile) -> Self {
        a.to_srt()
    }
}
impl From<SSAFile> for SRTFile {
    fn from(a: SSAFile) -> Self {
        a.to_srt()
    }
}
impl Display for SRTFile {
    /// Consumes self and dumps the file to String.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut lines = "".to_string();
        for i in self.lines.clone() {
            let line = i.line_number.to_string()
                + "\r\n"
                + &i.line_start.to_string().replace('.', ",")
                + " --> "
                + &i.line_end.to_string().replace('.', ",")
                + "\r\n"
                + &i.line_text.replace("\\N", "\r\n")
                + "\r\n\r\n";
            lines += &line;
        }
        write!(f, "{lines}")
    }
}

impl FromStr for SRTFile {
    type Err = std::io::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let path_or_content = s.to_string();
        let mut b: String = "".to_string();
        let mut sub: SRTFile = SRTFile::default();
        if !path_or_content.contains('\n') {
            if std::fs::read(&path_or_content).is_ok() {
                let mut f = File::open(path_or_content).expect("Couldn't open file");
                f.read_to_string(&mut b).expect("Couldn't read file");
            }
        } else {
            b = path_or_content;
        }

        let (split, ssplit) = if b.split("\r\n\r\n").count() < 2 {
            ("\n\n", "\n")
        } else {
            ("\r\n\r\n", "\r\n")
        };
        let lines = b.split(split).collect::<Vec<&str>>();
        for i in lines {
            let mut subline = SRTLine::default();
            let subsplit: Vec<&str> = i.split(ssplit).collect();
            if !subsplit
                .first()
                .expect("Failed to parse line number")
                .is_empty()
            {
                subline.line_number = subsplit
                    .first()
                    .expect("Failed to parse line number")
                    .parse::<i32>()
                    .expect("Failed to parse line number");
                let mut timesplit = subsplit
                    .get(1)
                    .expect("Failed to parse times line")
                    .split(" --> ");
                (subline.line_start, subline.line_end) = (
                    Time::from_str(timesplit.next().unwrap()).unwrap(),
                    Time::from_str(timesplit.next().unwrap()).unwrap(),
                );
                subline.line_text = subsplit
                    .get(2..)
                    .expect("Couldn't find text")
                    .join("\r\n")
                    .replace("\r\n", "\\N");
                sub.lines.push(subline)
            }
        }
        Ok(sub)
    }
}

/// Parses the given [String] into a [SRTFile]
///
/// The string may represent either the path to a file or the file content itself.
pub fn parse(path_or_content: String) -> Result<SRTFile, std::io::Error> {
    let mut b: String = "".to_string();
    let mut sub: SRTFile = SRTFile::default();
    if !path_or_content.contains('\n') {
        if std::fs::read(&path_or_content).is_ok() {
            let mut f = File::open(path_or_content).expect("Couldn't open file");
            f.read_to_string(&mut b).expect("Couldn't read file");
        }
    } else {
        b = path_or_content;
    }

    let (split, ssplit) = if b.split("\r\n\r\n").count() < 2 {
        ("\n\n", "\n")
    } else {
        ("\r\n\r\n", "\r\n")
    };
    let lines = b.split(split).collect::<Vec<&str>>();
    for i in lines {
        let mut subline = SRTLine::default();
        let subsplit: Vec<&str> = i.split(ssplit).collect();
        if !subsplit
            .first()
            .expect("Failed to parse line number")
            .is_empty()
        {
            subline.line_number = subsplit
                .first()
                .expect("Failed to parse line number")
                .parse::<i32>()
                .expect("Failed to parse line number");
            let mut timesplit = subsplit
                .get(1)
                .expect("Failed to parse times line")
                .split(" --> ");
            (subline.line_start, subline.line_end) = (
                Time::from_str(timesplit.next().unwrap()).unwrap(),
                Time::from_str(timesplit.next().unwrap()).unwrap(),
            );
            subline.line_text = subsplit
                .get(2..)
                .expect("Couldn't find text")
                .join("\r\n")
                .replace("\r\n", "\\N");
            sub.lines.push(subline)
        }
    }
    Ok(sub)
}