use super::CTDest;
use super::OfdAction;
#[derive(Debug, Clone)]
pub struct Bookmark {
pub name: String,
pub dest: CTDest,
}
impl Bookmark {
#[must_use]
pub fn new(name: impl Into<String>, dest: CTDest) -> Self {
Self {
name: name.into(),
dest,
}
}
}
impl OfdAction for Bookmark {
fn to_xml_string(&self) -> String {
format!(
"<ofd:Bookmark Name=\"{}\">{}</ofd:Bookmark>",
self.name,
self.dest.to_xml_string()
)
}
fn clone_box(&self) -> Box<dyn OfdAction> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::super::DestType;
use super::*;
#[test]
fn test_bookmark_new() {
let dest = CTDest::new(1);
let bm = Bookmark::new("Chapter 1", dest);
assert_eq!(bm.name, "Chapter 1");
assert_eq!(bm.dest.page, 1);
}
#[test]
fn test_bookmark_to_xml() {
let dest = CTDest::new(5).dest_type(DestType::XYZ).left(10.0).top(20.0);
let bm = Bookmark::new("Section 2", dest);
let xml = bm.to_xml_string();
assert!(xml.contains("Name=\"Section 2\""));
assert!(xml.contains("<ofd:Bookmark"));
assert!(xml.contains("PageID=\"5\""));
assert!(xml.contains("Type=\"XYZ\""));
assert!(xml.contains("</ofd:Bookmark>"));
}
#[test]
fn test_bookmark_clone_debug() {
let dest = CTDest::new(3);
let bm = Bookmark::new("test", dest);
let bm2 = bm.clone();
assert_eq!(bm2.name, "test");
let dbg = format!("{bm:?}");
assert!(dbg.contains("Bookmark"));
}
}