use std::collections::BTreeMap;
use crate::{error::Result, package::Package, reader::StorageFiles};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Titled {
pub path: String,
pub title: Option<String>,
}
pub fn titles(package: &Package) -> Result<Vec<Titled>> {
let given = &package.manifest().titles;
let storage = package.storage();
let files = StorageFiles(storage.clone());
let mut found: BTreeMap<String, Option<String>> = BTreeMap::new();
for file in logoxide_text::titles::book_file_titles(package.source(), &files)? {
let title = match file.books > 1 {
true => Some(package.info().name.clone()),
false => file.title,
};
found.insert(file.path, title);
}
for path in crate::references::book_files(package)? {
let title = match logoxide_text::citations::is_thml(&path) {
true => logoxide_text::titles::thml_title(&storage.read_string(&path)?),
false => logoxide_text::titles::epub_title(&storage.read(&path)?),
};
found.insert(path, title);
}
Ok(found
.into_iter()
.map(|(path, own)| Titled {
title: given.get(&path).cloned().or(own),
path,
})
.collect())
}
pub fn untitled(package: &Package) -> Result<Vec<String>> {
Ok(titles(package)?
.into_iter()
.filter(|t| t.title.is_none())
.map(|t| t.path)
.collect())
}