use super::VobSubError;
use std::{fs, io::Read as _, path::Path};
fn has_magic(path: &Path, magic: &[u8]) -> Result<bool, VobSubError> {
let mkerr = |source| VobSubError::Io {
source,
path: path.into(),
};
let mut f = fs::File::open(path).map_err(mkerr)?;
let mut bytes = vec![0; magic.len()];
f.read_exact(&mut bytes).map_err(mkerr)?;
Ok(magic == &bytes[..])
}
pub fn is_idx_file<P: AsRef<Path>>(path: P) -> Result<bool, VobSubError> {
has_magic(path.as_ref(), b"# VobSub index file")
}
pub fn is_sub_file<P: AsRef<Path>>(path: P) -> Result<bool, VobSubError> {
has_magic(path.as_ref(), &[0x00, 0x00, 0x01, 0xba])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn probe_idx_files() {
assert!(is_idx_file("./fixtures/tiny.idx").unwrap());
assert!(!is_idx_file("./fixtures/tiny.sub").unwrap());
}
#[test]
fn probe_sub_files() {
assert!(is_sub_file("./fixtures/tiny.sub").unwrap());
assert!(!is_sub_file("./fixtures/tiny.idx").unwrap());
}
}