use kuchiki::NodeRef;
use serde::Serialize;
use crate::Bookmark;
use crate::Folder;
#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub enum Item {
Subfolder(Folder),
Shortcut(Bookmark),
}
impl Item {
pub fn from_node(node: &NodeRef) -> Option<Self> {
if let Some(bookmark) = Bookmark::from_node(node) {
Some(Item::Shortcut(bookmark))
} else if let Some(folder) = Folder::from_node(node) {
Some(Item::Subfolder(folder))
} else {
None
}
}
pub fn is_shortcut(&self) -> bool {
match self {
Item::Subfolder(_) => false,
Item::Shortcut(_) => true,
}
}
pub fn take_shortcut(&self) -> Option<&Bookmark> {
match self {
Item::Subfolder(_) => None,
Item::Shortcut(bookmark) => Some(bookmark),
}
}
pub fn is_subfolder(&self) -> bool {
match self {
Item::Subfolder(_) => true,
Item::Shortcut(_) => false,
}
}
pub fn take_subfolder(&self) -> Option<&Folder> {
match self {
Item::Subfolder(folder) => Some(folder),
Item::Shortcut(_) => None,
}
}
}
impl PartialEq for Item {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Item::Subfolder(f1), Item::Subfolder(f2)) => f1 == f2,
(Item::Shortcut(b1), Item::Shortcut(b2)) => b1 == b2,
_ => false,
}
}
}