use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct Bookmark {
pub name: String,
pub url: String,
}
impl Bookmark {
pub fn new(name: String, url: String) -> Self {
Self { name, url }
}
}
impl std::fmt::Display for Bookmark {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.name, self.url)
}
}
pub struct BookmarkManager {
bookmarks: Vec<Bookmark>,
}
impl BookmarkManager {
pub fn new() -> Self {
Self {
bookmarks: Vec::new(),
}
}
pub fn append_bookmark(&mut self, name: String, url: String) {
let bookmark = Bookmark::new(name, url);
self.bookmarks.push(bookmark);
}
pub fn remove_bookmark(&mut self, index: usize) -> Result<(), String> {
if index < self.bookmarks.len() {
self.bookmarks.remove(index);
Ok(())
} else {
Err(format!("Index {} out of bounds", index))
}
}
pub fn get_bookmark(&self, index: usize) -> Option<&Bookmark> {
if index < self.bookmarks.len() {
Some(&self.bookmarks[index])
} else {
None
}
}
pub fn bookmarks(&self) -> &Vec<Bookmark> {
&self.bookmarks
}
}