use std::collections::BTreeMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use crate::io::{Input, InputOrigin, InputProvider, OpenResult};
#[derive(Debug)]
pub struct Bundle {
path: PathBuf,
files: BTreeMap<String, String>,
}
impl Bundle {
pub fn open(path: impl AsRef<Path>) -> Result<Self, String> {
let path = path.as_ref().to_path_buf();
let file = std::fs::File::open(&path)
.map_err(|e| format!("cannot read {}: {e}", path.display()))?;
let mut archive = zip::ZipArchive::new(file)
.map_err(|e| format!("{} is not a bundle: {e}", path.display()))?;
let mut files = BTreeMap::new();
for i in 0..archive.len() {
let mut entry = archive
.by_index(i)
.map_err(|e| format!("{}: {e}", path.display()))?;
if entry.is_dir() {
continue;
}
let name = entry.name().to_string();
let mut content = String::new();
if entry.read_to_string(&mut content).is_err() {
continue;
}
if let Some(base) = name.rsplit('/').next() {
files.entry(base.to_string()).or_insert(content.clone());
}
files.insert(name, content);
}
Ok(Bundle { path, files })
}
pub fn all_files(&self) -> Vec<&str> {
self.files.keys().map(String::as_str).collect()
}
pub fn digest(&self) -> String {
let mut joined = String::new();
for (name, content) in &self.files {
joined.push_str(name);
joined.push('\0');
joined.push_str(&crate::io::digest_of(content));
joined.push('\n');
}
crate::io::digest_of(&joined)
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
}
impl InputProvider for Bundle {
fn input_open(&mut self, name: &str) -> OpenResult<Input> {
let candidates: Vec<String> = match Path::new(name).extension() {
Some(_) => vec![name.to_string()],
None => vec![name.to_string(), format!("{name}.tex")],
};
for candidate in candidates {
if let Some(content) = self.files.get(&candidate) {
return OpenResult::Ok(Input {
name: name.to_string(),
path: None,
origin: InputOrigin::Memory,
content: content.clone(),
});
}
}
OpenResult::NotAvailable
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
fn bundle_of(name: &str, files: &[(&str, &str)]) -> PathBuf {
let dir = std::env::temp_dir().join(format!("texrs_bundle_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(format!("{name}.zip"));
let file = std::fs::File::create(&path).unwrap();
let mut zip = zip::ZipWriter::new(file);
for (entry, content) in files {
zip.start_file(*entry, zip::write::SimpleFileOptions::default())
.unwrap();
zip.write_all(content.as_bytes()).unwrap();
}
zip.finish().unwrap();
path
}
#[test]
fn a_bundle_answers_by_name_and_by_the_name_with_tex_on_it() {
let path = bundle_of("simple", &[("macros.tex", "\\def\\a{A}")]);
let mut bundle = Bundle::open(&path).expect("opens");
assert_eq!(
bundle
.input_open("macros.tex")
.must_exist()
.unwrap()
.content,
"\\def\\a{A}"
);
assert_eq!(
bundle.input_open("macros").must_exist().unwrap().content,
"\\def\\a{A}"
);
assert!(bundle.input_open("absent").is_not_available());
let _ = std::fs::remove_file(&path);
}
#[test]
fn a_file_deep_in_a_bundle_is_reachable_by_its_bare_name() {
let path = bundle_of(
"deep",
&[("tex/latex/mine/macros.tex", "DEEP"), ("top.tex", "TOP")],
);
let mut bundle = Bundle::open(&path).expect("opens");
assert_eq!(
bundle.input_open("macros").must_exist().unwrap().content,
"DEEP"
);
assert_eq!(
bundle
.input_open("tex/latex/mine/macros.tex")
.must_exist()
.unwrap()
.content,
"DEEP"
);
assert_eq!(
bundle.input_open("top").must_exist().unwrap().content,
"TOP"
);
let _ = std::fs::remove_file(&path);
}
#[test]
fn an_entry_is_not_a_path_on_this_machine() {
let path = bundle_of("origin", &[("macros.tex", "X")]);
let mut bundle = Bundle::open(&path).expect("opens");
let opened = bundle.input_open("macros.tex").must_exist().unwrap();
assert_eq!(opened.origin, InputOrigin::Memory);
assert_eq!(
opened.path, None,
"a bundle entry has no path an editor could open"
);
let _ = std::fs::remove_file(&path);
}
#[test]
fn a_digest_is_of_what_the_bundle_holds_not_of_the_file_it_came_in() {
let one = bundle_of("digest_a", &[("a.tex", "A"), ("b.tex", "B")]);
let two = bundle_of("digest_b", &[("b.tex", "B"), ("a.tex", "A")]);
let three = bundle_of("digest_c", &[("a.tex", "A"), ("b.tex", "CHANGED")]);
let a = Bundle::open(&one).unwrap();
let b = Bundle::open(&two).unwrap();
let c = Bundle::open(&three).unwrap();
assert_eq!(a.digest(), b.digest(), "the same files are the same bundle");
assert_ne!(a.digest(), c.digest(), "a changed file is a changed bundle");
assert_eq!(a.all_files(), vec!["a.tex", "b.tex"]);
for path in [&one, &two, &three] {
let _ = std::fs::remove_file(path);
}
}
#[test]
fn what_is_not_a_bundle_is_refused_by_name() {
let dir = std::env::temp_dir().join(format!("texrs_bundle_bad_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("not.zip");
std::fs::write(&path, b"not a zip at all").unwrap();
let err = Bundle::open(&path).unwrap_err();
assert!(err.contains("is not a bundle"), "{err}");
assert!(Bundle::open(dir.join("absent.zip"))
.unwrap_err()
.contains("cannot read"));
let _ = std::fs::remove_dir_all(&dir);
}
}