use super::Bookmark;
#[derive(Debug, Clone)]
pub struct Bookmarks {
pub bookmarks: Vec<Bookmark>,
}
impl Bookmarks {
#[must_use]
pub fn new() -> Self {
Self {
bookmarks: Vec::new(),
}
}
pub fn push(&mut self, bookmark: Bookmark) {
self.bookmarks.push(bookmark);
}
#[must_use]
pub fn to_xml_string(&self) -> String {
let inner: String = self.bookmarks.iter().map(Bookmark::to_xml_string).collect();
format!("<Bookmarks>{inner}</Bookmarks>")
}
}
impl Default for Bookmarks {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bookmarks_new() {
let b = Bookmarks::new();
assert!(b.bookmarks.is_empty());
let b2 = Bookmarks::default();
assert!(b2.bookmarks.is_empty());
}
#[test]
fn test_bookmarks_push_and_xml() {
let mut b = Bookmarks::new();
b.push(Bookmark::new("Chapter 1", 1));
b.push(Bookmark::new("Chapter 2", 5));
assert_eq!(b.bookmarks.len(), 2);
let xml = b.to_xml_string();
assert!(xml.contains("<Bookmarks>"));
assert!(xml.contains("</Bookmarks>"));
assert!(xml.contains("Chapter 1"));
assert!(xml.contains("Chapter 2"));
}
}