use std::fmt::Write;
#[derive(Debug, Clone)]
pub struct AnnPage {
pub page_index: u32,
pub annot_file: Option<String>,
}
impl AnnPage {
#[must_use]
pub fn new(page_index: u32) -> Self {
Self {
page_index,
annot_file: None,
}
}
#[must_use]
pub fn annot_file(mut self, file: impl Into<String>) -> Self {
self.annot_file = Some(file.into());
self
}
#[must_use]
pub fn to_xml_string(&self) -> String {
let mut xml = format!(r#"<ofd:AnnPage PageIndex="{}""#, self.page_index);
if let Some(ref file) = self.annot_file {
let _ = write!(xml, r#" AnnotFile="{file}""#);
}
xml.push_str(" />");
xml
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ann_page_new() {
let p = AnnPage::new(0);
assert_eq!(p.page_index, 0);
assert!(p.annot_file.is_none());
}
#[test]
fn test_ann_page_builder() {
let p = AnnPage::new(3).annot_file("Page_0/Annot.xml");
assert_eq!(p.page_index, 3);
assert_eq!(p.annot_file.as_deref(), Some("Page_0/Annot.xml"));
}
#[test]
fn test_ann_page_to_xml_string_basic() {
let p = AnnPage::new(2);
let xml = p.to_xml_string();
assert!(xml.contains(r#"PageIndex="2""#));
assert!(!xml.contains("AnnotFile"));
}
#[test]
fn test_ann_page_to_xml_string_with_file() {
let p = AnnPage::new(1).annot_file("Page_1/ann.xml");
let xml = p.to_xml_string();
assert!(xml.contains(r#"AnnotFile="Page_1/ann.xml""#));
}
}