use alloc::boxed::Box;
use std::path::Path;
use crate::bytes::Bytes;
use super::{Bundle, BundleError, EmbeddedBundle, SqliteBundle, flat_bundle_version};
pub fn open<P: AsRef<Path>>(path: P) -> Result<Box<dyn Bundle>, BundleError> {
let path = path.as_ref();
if !is_flat(path)? {
return Ok(Box::new(SqliteBundle::open(path)?));
}
let bytes = Bytes::from_bytes_vec(std::fs::read(path)?);
Ok(Box::new(EmbeddedBundle::open(bytes)?))
}
fn is_flat(path: &Path) -> Result<bool, BundleError> {
use std::io::Read;
let mut header = [0u8; 12];
match std::fs::File::open(path)?.read_exact(&mut header) {
Ok(()) => Ok(flat_bundle_version(&header).is_some()),
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => Ok(false),
Err(err) => Err(err.into()),
}
}