Skip to main content

easyofd_core/annotation/
ann_page.rs

1//! 注释所在页信息。
2
3use std::fmt::Write;
4
5/// 对应 Java: org.ofdrw.core.annotation.AnnPage
6///
7/// 描述注释所在的页面,包含页码和该页的注释列表。
8#[derive(Debug, Clone)]
9pub struct AnnPage {
10    /// 页面索引(0-based)。
11    pub page_index: u32,
12    /// 该页的注释文件路径。
13    pub annot_file: Option<String>,
14}
15
16impl AnnPage {
17    /// 创建一个新的注释页。
18    #[must_use]
19    pub fn new(page_index: u32) -> Self {
20        Self {
21            page_index,
22            annot_file: None,
23        }
24    }
25
26    /// 设置注释文件路径。
27    #[must_use]
28    pub fn annot_file(mut self, file: impl Into<String>) -> Self {
29        self.annot_file = Some(file.into());
30        self
31    }
32
33    /// 序列化为 XML 字符串。
34    #[must_use]
35    pub fn to_xml_string(&self) -> String {
36        let mut xml = format!(r#"<ofd:AnnPage PageIndex="{}""#, self.page_index);
37        if let Some(ref file) = self.annot_file {
38            let _ = write!(xml, r#" AnnotFile="{file}""#);
39        }
40        xml.push_str(" />");
41        xml
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_ann_page_new() {
51        let p = AnnPage::new(0);
52        assert_eq!(p.page_index, 0);
53        assert!(p.annot_file.is_none());
54    }
55
56    #[test]
57    fn test_ann_page_builder() {
58        let p = AnnPage::new(3).annot_file("Page_0/Annot.xml");
59        assert_eq!(p.page_index, 3);
60        assert_eq!(p.annot_file.as_deref(), Some("Page_0/Annot.xml"));
61    }
62
63    #[test]
64    fn test_ann_page_to_xml_string_basic() {
65        let p = AnnPage::new(2);
66        let xml = p.to_xml_string();
67        assert!(xml.contains(r#"PageIndex="2""#));
68        assert!(!xml.contains("AnnotFile"));
69    }
70
71    #[test]
72    fn test_ann_page_to_xml_string_with_file() {
73        let p = AnnPage::new(1).annot_file("Page_1/ann.xml");
74        let xml = p.to_xml_string();
75        assert!(xml.contains(r#"AnnotFile="Page_1/ann.xml""#));
76    }
77}