Skip to main content

easyofd_core/doc/bookmark/
bookmarks.rs

1//! 书签集。
2
3use super::Bookmark;
4
5/// 对应 Java: org.ofdrw.core.pageDescription.Bookmarks
6///
7/// 书签集合,包含文档中所有书签定义。
8#[derive(Debug, Clone)]
9pub struct Bookmarks {
10    /// 书签列表。
11    pub bookmarks: Vec<Bookmark>,
12}
13
14impl Bookmarks {
15    /// 创建空的书签集。
16    #[must_use]
17    pub fn new() -> Self {
18        Self {
19            bookmarks: Vec::new(),
20        }
21    }
22
23    /// 添加一个书签。
24    pub fn push(&mut self, bookmark: Bookmark) {
25        self.bookmarks.push(bookmark);
26    }
27
28    /// 序列化为 XML 字符串。
29    #[must_use]
30    pub fn to_xml_string(&self) -> String {
31        let inner: String = self.bookmarks.iter().map(Bookmark::to_xml_string).collect();
32        format!("<Bookmarks>{inner}</Bookmarks>")
33    }
34}
35
36impl Default for Bookmarks {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_bookmarks_new() {
48        let b = Bookmarks::new();
49        assert!(b.bookmarks.is_empty());
50        let b2 = Bookmarks::default();
51        assert!(b2.bookmarks.is_empty());
52    }
53
54    #[test]
55    fn test_bookmarks_push_and_xml() {
56        let mut b = Bookmarks::new();
57        b.push(Bookmark::new("Chapter 1", 1));
58        b.push(Bookmark::new("Chapter 2", 5));
59        assert_eq!(b.bookmarks.len(), 2);
60        let xml = b.to_xml_string();
61        assert!(xml.contains("<Bookmarks>"));
62        assert!(xml.contains("</Bookmarks>"));
63        assert!(xml.contains("Chapter 1"));
64        assert!(xml.contains("Chapter 2"));
65    }
66}