use crate::format::Format;
use mbook::Book;
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Converted {
pub book: Book,
pub report: Report,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Report {
pub format: Format,
pub warnings: Vec<Warning>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Warning {
pub kind: WarningKind,
pub location: Option<String>,
}
impl Warning {
pub(crate) fn new(kind: WarningKind, location: &str) -> Self {
Self {
kind,
location: (!location.is_empty()).then(|| location.to_owned()),
}
}
pub(crate) fn whole_book(kind: WarningKind) -> Self {
Self {
kind,
location: None,
}
}
}
impl fmt::Display for Warning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.location {
Some(location) => write!(f, "{location}: {}", self.kind),
None => self.kind.fmt(f),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WarningKind {
MarkupDropped(String),
PictureDropped(String),
LinkRetargeted(String),
LinkDropped(String),
MetadataDropped(String),
MetadataMissing(String),
}
impl fmt::Display for WarningKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MarkupDropped(what) => write!(f, "{what} markup was dropped"),
Self::PictureDropped(name) => write!(f, "the picture {name:?} was dropped"),
Self::LinkRetargeted(target) => {
write!(
f,
"the contents entry to {target:?} leads to a heading above it"
)
}
Self::LinkDropped(target) => write!(f, "the link to {target:?} was dropped"),
Self::MetadataDropped(key) => write!(f, "a value of {key:?} was dropped"),
Self::MetadataMissing(key) => write!(f, "the source has no {key:?}"),
}
}
}
#[cfg(test)]
mod tests {
use super::{Warning, WarningKind};
#[test]
fn a_warning_with_a_place_names_it_first() {
let warning = Warning::new(WarningKind::MarkupDropped("sub".into()), "ch1.xhtml");
assert_eq!(warning.to_string(), "ch1.xhtml: sub markup was dropped");
}
#[test]
fn a_warning_without_a_place_has_no_prefix() {
let warning = Warning::new(WarningKind::MetadataMissing("title".into()), "");
assert_eq!(warning.location, None);
assert_eq!(warning.to_string(), r#"the source has no "title""#);
}
#[test]
fn a_warning_about_the_whole_book_has_no_place() {
let warning = Warning::whole_book(WarningKind::MetadataDropped("date".into()));
assert_eq!(warning.to_string(), r#"a value of "date" was dropped"#);
}
#[test]
fn every_kind_of_warning_has_a_message() {
let messages = [
WarningKind::PictureDropped("a.svg".into()),
WarningKind::LinkRetargeted("ch1#p".into()),
WarningKind::LinkDropped("ch9".into()),
]
.map(|kind| kind.to_string());
assert_eq!(
messages,
[
r#"the picture "a.svg" was dropped"#,
r#"the contents entry to "ch1#p" leads to a heading above it"#,
r#"the link to "ch9" was dropped"#
]
);
}
}