use std::path::{Path, PathBuf};
use crate::error::{Error, Result};
use crate::source::{parse_dvd_uri, DvdUri};
use crate::DvdDisc;
pub fn convert_dvd_to_mkv(
source: &str,
title_idx: usize,
out_path: impl AsRef<Path>,
) -> Result<()> {
let image_path = resolve_source(source)?;
let disc = DvdDisc::open(&image_path)?;
crate::mkv_writer::write_title_to_mkv(&disc, title_idx, &image_path, out_path)
}
pub fn list_titles(source: &str) -> Result<Vec<crate::ifo::DvdTitleEntry>> {
let image_path = resolve_source(source)?;
let disc = DvdDisc::open(&image_path)?;
let mut reader = std::fs::File::open(&image_path)?;
disc.enumerate_titles(&mut reader)
}
fn resolve_source(source: &str) -> Result<PathBuf> {
if source.starts_with("dvd:") {
match parse_dvd_uri(source)? {
DvdUri::AutoDetect => Err(Error::NotDvdVideo(
"convert_dvd_to_mkv: dvd:// auto-detect is Phase 2 — \
pass an explicit dvd:///path/to/disc.iso URI",
)),
DvdUri::Path(p) => Ok(p),
}
} else {
Ok(PathBuf::from(source))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_dvd_uri_with_path() {
let p = resolve_source("dvd:///tmp/disc.iso").unwrap();
assert_eq!(p, PathBuf::from("/tmp/disc.iso"));
}
#[test]
fn resolve_bare_path() {
let p = resolve_source("/var/lib/disc.iso").unwrap();
assert_eq!(p, PathBuf::from("/var/lib/disc.iso"));
}
#[test]
fn resolve_dvd_auto_rejected() {
let err = resolve_source("dvd://").unwrap_err();
match err {
Error::NotDvdVideo(_) => {}
other => panic!("expected NotDvdVideo for auto-detect, got {other:?}"),
}
}
}