subparse/
lib.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5#![deny(
6    missing_docs,
7    missing_debug_implementations,
8    missing_copy_implementations,
9    trivial_casts,
10    trivial_numeric_casts,
11    unsafe_code,
12    unstable_features,
13    unused_import_braces,
14    unused_qualifications
15)]
16
17//! This crate provides a common interface for popular subtitle formats (`.srt`, `.ssa`, `.ass`, `.idx`, `.sub`).
18//!
19//! Files can be parsed, modified and saved again - some formats can be created from scratch.
20//! The focus is on non-destructive parsing, meaning that formatting and other information are preserved
21//! if not explicitely changed.
22
23extern crate combine;
24extern crate encoding_rs;
25extern crate failure;
26extern crate itertools;
27extern crate vobsub;
28
29/// Error-chain generated error types.
30#[macro_use]
31pub mod errors;
32
33mod formats;
34
35/// Types that represent a time point, duration and time span.
36pub mod timetypes;
37
38use errors::*;
39pub use formats::idx::IdxFile;
40pub use formats::microdvd::MdvdFile;
41pub use formats::srt::SrtFile;
42pub use formats::ssa::SsaFile;
43pub use formats::vobsub::VobFile;
44pub use formats::{
45    get_subtitle_format, get_subtitle_format_by_extension, get_subtitle_format_by_extension_err, get_subtitle_format_err,
46    is_valid_extension_for_subtitle_format, parse_bytes, parse_str,
47};
48pub use formats::{SubtitleFile, SubtitleFormat};
49use timetypes::TimeSpan;
50
51/// This trait represents the generic interface for reading and writing subtitle information across all subtitle formats.
52///
53/// This trait allows you to read, change and rewrite the subtitle file.
54pub trait SubtitleFileInterface {
55    /// The subtitle entries can be changed by calling `update_subtitle_entries()`.
56    fn get_subtitle_entries(&self) -> Result<Vec<SubtitleEntry>>;
57
58    /// Set the entries from the subtitle entries from the `get_subtitle_entries()`.
59    ///
60    /// The length of the given input slice should always match the length of the vector length from
61    /// `get_subtitle_entries()`. This function can not delete/create new entries, but preserves
62    /// everything else in the file (formatting, authors, ...).
63    ///
64    /// If the input entry has `entry.line == None`, the line will not be overwritten.
65    ///
66    /// Be aware that .idx files cannot save time_spans_ (a subtitle will be shown between two
67    /// consecutive timepoints/there are no separate starts and ends) - so the timepoint will be set
68    /// to the start of the corresponding input-timespan.
69    fn update_subtitle_entries(&mut self, i: &[SubtitleEntry]) -> Result<()>;
70
71    /// Returns a byte-stream in the respective format (.ssa, .srt, etc.) with the
72    /// (probably) altered information.
73    fn to_data(&self) -> Result<Vec<u8>>;
74}
75
76/// The data which can be read from/written to a subtitle file.
77#[derive(Debug)]
78pub struct SubtitleEntry {
79    /// The duration for which the current subtitle will be shown.
80    pub timespan: TimeSpan,
81
82    // TODO: to Vec<String>
83    /// The text which will be shown in this subtitle. Be aware that
84    /// for example VobSub files (and any other image based format)
85    /// will have `None` as value.
86    pub line: Option<String>,
87}
88
89impl SubtitleEntry {
90    /// Create subtitle entry with text.
91    fn new(timespan: TimeSpan, line: String) -> SubtitleEntry {
92        SubtitleEntry {
93            timespan: timespan,
94            line: Some(line),
95        }
96    }
97}
98
99impl From<TimeSpan> for SubtitleEntry {
100    fn from(f: TimeSpan) -> SubtitleEntry {
101        SubtitleEntry { timespan: f, line: None }
102    }
103}