Skip to main content

easyofd_reader/model/
annotation_entity.rs

1//! 注释实体。
2//!
3//! 对应 Java: org.ofdrw.reader.model.AnnotionEntity
4//!
5//! 注意:Java 原始类名为 `AnnotionEntity`(拼写错误),此处保留原名以保持兼容。
6
7/// 注释实体,描述一个页面上的注释集合。
8///
9/// 对应 Java: `org.ofdrw.reader.model.AnnotionEntity`
10///
11/// 注意:Java 原始类名为 `AnnotionEntity`(拼写错误,应为 `AnnotationEntity`),
12/// 此处保留原名以保持 API 兼容。
13#[derive(Debug, Clone)]
14pub struct AnnotionEntity {
15    /// 注释所在页面 ID。
16    pub page_id: String,
17    /// 注释列表(原始 XML 字符串表示)。
18    ///
19    /// Java 版使用 `List<Annot>` 对象,Rust 版存储为原始 XML 字符串,
20    /// 因为 easyofd-core 的 `Annot` 类型已提供完整解析。
21    pub annot_xmls: Vec<String>,
22}
23
24impl AnnotionEntity {
25    /// 创建新的注释实体。
26    #[must_use]
27    pub fn new(page_id: impl Into<String>, annot_xmls: Vec<String>) -> Self {
28        Self {
29            page_id: page_id.into(),
30            annot_xmls,
31        }
32    }
33
34    /// 注释数量。
35    #[must_use]
36    pub fn len(&self) -> usize {
37        self.annot_xmls.len()
38    }
39
40    /// 是否没有注释。
41    #[must_use]
42    pub fn is_empty(&self) -> bool {
43        self.annot_xmls.is_empty()
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_annotation_entity_new() {
53        let entity = AnnotionEntity::new("page_0", vec!["<Annot/>".into()]);
54        assert_eq!(entity.page_id, "page_0");
55        assert_eq!(entity.len(), 1);
56        assert!(!entity.is_empty());
57    }
58
59    #[test]
60    fn test_annotation_entity_empty() {
61        let entity = AnnotionEntity::new("page_1", vec![]);
62        assert!(entity.is_empty());
63        assert_eq!(entity.len(), 0);
64    }
65
66    #[test]
67    fn test_annotation_entity_clone() {
68        let entity = AnnotionEntity::new("p0", vec!["a".into(), "b".into()]);
69        let cloned = entity.clone();
70        assert_eq!(cloned.page_id, entity.page_id);
71        assert_eq!(cloned.annot_xmls, entity.annot_xmls);
72    }
73}