Skip to content

Commit

Permalink
feat(vobsub)!: make Index optional.
Browse files Browse the repository at this point in the history
`sub` file parsing don't need `idx`, so separate `sub` data from `idx`.
Create a new Sub struct who can initialized from file, and contain the content of the `sub`.
Remove `sub_data` from `Index` struct.
  • Loading branch information
gwen-lg committed Mar 8, 2025
1 parent 4ad3e8f commit fc3d674
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
8 changes: 1 addition & 7 deletions src/vobsub/idx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::vobsub::IResultExt;

use super::{
palette::{palette, DEFAULT_PALETTE},
sub, Palette, VobSubError,
Palette, VobSubError,
};

/// A `*.idx` file describing the subtitles in a `*.sub` file.
Expand Down Expand Up @@ -81,12 +81,6 @@ impl Index {
pub const fn palette(&self) -> &Palette {
&self.palette
}

/// Iterate over the subtitles associated with this `*.idx` file.
#[must_use]
pub fn subtitles<D>(&self) -> sub::VobsubParser<D> {
sub::VobsubParser::new(&self.sub_data)
}
}

/// Read the palette in `*.idx` file content
Expand Down
2 changes: 1 addition & 1 deletion src/vobsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub use self::{
img::{conv_to_rgba, VobSubIndexedImage, VobSubOcrImage, VobSubToImage},
palette::{palette, palette_rgb_to_luminance, Palette},
probe::{is_idx_file, is_sub_file},
sub::ErrorMissing,
sub::{ErrorMissing, Sub},
};

use crate::content::ContentError;
Expand Down
49 changes: 42 additions & 7 deletions src/vobsub/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use nom::{
sequence::{preceded, Tuple},
IResult,
};
use std::{cmp::Ordering, fmt::Debug, iter::FusedIterator, marker::PhantomData};
use std::{
cmp::Ordering, fmt::Debug, fs, io::Read, iter::FusedIterator, marker::PhantomData, path::Path,
};
use thiserror::Error;

/// Parse four 4-bit palette entries.
Expand Down Expand Up @@ -319,6 +321,41 @@ macro_rules! try_iter {
};
}

/// Store the content of a `*.sub` file.
pub struct Sub {
/// Our compressed subtitle data.
data: Vec<u8>,
}
impl Sub {
/// Init a `Sub` from a file path.
///
/// # Errors
///
/// Will return `VobSubError::Io` if not able to read a file from `path`.
pub fn open<P>(path: P) -> Result<Self, VobSubError>
where
P: AsRef<Path> + Clone,
{
let mut sub = fs::File::open(path.as_ref()).map_err(|source| VobSubError::Io {
source,
path: path.as_ref().to_path_buf(),
})?;
let mut data = vec![];
sub.read_to_end(&mut data)
.map_err(|source| VobSubError::Io {
source,
path: path.as_ref().to_path_buf(),
})?;
Ok(Self { data })
}

/// Iterate over the subtitles associated with this `*.idx` file.
#[must_use]
pub fn subtitles<D>(&self) -> VobsubParser<D> {
VobsubParser::new(&self.data)
}
}

/// An internal iterator over subtitles. These subtitles may not have a
/// valid `end_time`, so we'll try to fix them up before letting the user
/// see them.
Expand Down Expand Up @@ -414,7 +451,6 @@ impl<D> FusedIterator for VobsubParser<'_, D> {}
#[cfg(test)]
mod tests {
use super::*;
use crate::vobsub::idx;

#[test]
fn parse_palette_entries() {
Expand Down Expand Up @@ -549,9 +585,9 @@ mod tests {
#[test]
fn parse_subtitles_from_subtitle_edit() {
//use env_logger;
use idx::Index;

//let _ = env_logger::init();
let idx = Index::open("./fixtures/tiny.idx").unwrap();
let idx = Sub::open("./fixtures/tiny.sub").unwrap();
let mut subs = idx.subtitles::<TimeSpan>();
subs.next().expect("missing sub").unwrap();
assert!(subs.next().is_none());
Expand All @@ -560,18 +596,17 @@ mod tests {
#[test]
fn parse_fuzz_corpus_seeds() {
//use env_logger;
use idx::Index;
//let _ = env_logger::init();

// Make sure these two fuzz corpus inputs still work, and that they
// return the same subtitle data.
let tiny = Index::open("./fixtures/tiny.idx")
let tiny = Sub::open("./fixtures/tiny.sub")
.unwrap()
.subtitles::<TimeSpan>()
.next()
.unwrap()
.unwrap();
let split = Index::open("./fixtures/tiny-split.idx")
let split = Sub::open("./fixtures/tiny-split.sub")
.unwrap()
.subtitles::<TimeSpan>()
.next()
Expand Down

0 comments on commit fc3d674

Please sign in to comment.