use crate::fonts::{self, Fonts};
use crate::images;
use crate::inline::{self, Inline};
use crate::refs::{self, Renames};
use anyhow::Result;
use std::collections::HashSet;
#[derive(Debug, Default)]
pub(crate) struct Book {
pub(crate) fonts: Fonts,
pub(crate) renames: Renames,
pub(crate) rotated: HashSet<String>,
pub(crate) inline: Inline,
}
impl Book {
fn changes_chapters(&self) -> bool {
!(self.renames.is_empty() && self.rotated.is_empty() && self.inline.is_empty())
}
}
pub(crate) enum Action {
Skip,
RemoveFont,
CopyRaw,
Rewrite(Rewrite, Fallback),
Image(images::Kind),
}
pub(crate) type Rewrite = fn(&str, &str, &Book) -> Result<Outcome>;
#[derive(Debug, PartialEq)]
pub(crate) enum Outcome {
Keep,
Replace(String),
Drop,
}
impl Outcome {
fn then(self, original: &str, next: impl FnOnce(&str) -> Result<Outcome>) -> Result<Outcome> {
Ok(match self {
Outcome::Drop => Outcome::Drop,
Outcome::Keep => next(original)?,
Outcome::Replace(text) => match next(&text)? {
Outcome::Keep => Outcome::Replace(text),
other => other,
},
})
}
}
pub(crate) enum Fallback {
Copy,
CopyWithWarning,
Reject,
}
pub(crate) fn is_document(name: &str) -> bool {
let lower = name.to_ascii_lowercase();
[".xhtml", ".html", ".htm", ".svg"]
.iter()
.any(|ext| lower.ends_with(ext))
}
pub(crate) fn decide(name: &str, book: &Book) -> Action {
let lower = name.to_ascii_lowercase();
if name == "mimetype" {
Action::Skip
} else if book.fonts.contains(name) {
Action::RemoveFont
} else if lower.ends_with(".css") {
Action::Rewrite(rewrite_css, Fallback::Copy)
} else if lower.ends_with(".opf") {
Action::Rewrite(rewrite_opf, Fallback::Reject)
} else if lower == "meta-inf/encryption.xml" {
Action::Rewrite(
|xml, _, book| fonts::clean_encryption(xml, &book.fonts),
Fallback::Reject,
)
} else if let Some(kind) = images::kind_of(name) {
Action::Image(kind)
} else if is_document(name) && book.changes_chapters() {
Action::Rewrite(refs::rewrite_document, Fallback::CopyWithWarning)
} else {
Action::CopyRaw
}
}
fn rewrite_css(css: &str, entry: &str, book: &Book) -> Result<Outcome> {
fonts::strip_font_faces(css, &book.fonts)?
.then(css, |css| refs::rewrite_css(css, entry, &book.renames))
}
fn rewrite_opf(opf: &str, entry: &str, book: &Book) -> Result<Outcome> {
fonts::remove_manifest_items(opf, &book.fonts)?
.then(opf, |opf| refs::rewrite_opf(opf, entry, &book.renames))?
.then(opf, |opf| {
Ok(match inline::manifest_items(opf, entry, &book.inline) {
Some(opf) => Outcome::Replace(opf),
None => Outcome::Keep,
})
})
}
#[cfg(test)]
mod tests {
use super::*;
fn replace(text: &str) -> Result<Outcome> {
Ok(Outcome::Replace(format!("{text}+")))
}
fn keep(_: &str) -> Result<Outcome> {
Ok(Outcome::Keep)
}
#[test]
fn then_combines_both_steps() {
assert_eq!(Outcome::Keep.then("a", keep).unwrap(), Outcome::Keep);
assert_eq!(
Outcome::Keep.then("a", replace).unwrap(),
Outcome::Replace("a+".into())
);
assert_eq!(
Outcome::Replace("b".into()).then("a", keep).unwrap(),
Outcome::Replace("b".into())
);
assert_eq!(
Outcome::Replace("b".into()).then("a", replace).unwrap(),
Outcome::Replace("b+".into())
);
assert_eq!(Outcome::Drop.then("a", replace).unwrap(), Outcome::Drop);
}
}