use bucket_group_name::BucketGroupName; use bucket_name_and_balance::BucketNameAndBalance; use iced::Element; use itertools::Either; use schist_models::Bucket; use crate::{gui::components::navigation, traits::Focusable}; mod bucket_group_name; mod bucket_name_and_balance; #[derive(Clone, Debug, PartialEq)] pub struct BucketNavigationEntry(Either); impl BucketNavigationEntry { pub fn from_bucket(bucket: &Bucket) -> Self { Self(Either::Left(BucketNameAndBalance::new(bucket))) } pub fn from_group_closed(group: &str) -> Self { Self(Either::Right(BucketGroupName::new(group, false))) } pub fn from_group_open(group: &str) -> Self { Self(Either::Right(BucketGroupName::new(group, true))) } pub fn is_bucket_and

(&self, pred: P) -> bool where P: Fn(&Bucket) -> bool, { self.0 .as_ref() .map_left(|bucket_name_and_balance| pred(&bucket_name_and_balance.bucket)) .left_or(false) } pub fn is_group_and

(&self, pred: P) -> bool where P: Fn(&BucketGroupName) -> bool, { self.0.as_ref().map_right(pred).right_or(false) } pub fn to_string(&self) -> String { self.0 .as_ref() .map_left(|bucket_name_and_balance| bucket_name_and_balance.bucket.name.clone()) .map_right(|bucket_group_name| bucket_group_name.group.clone()) .into_inner() } pub fn group(&self) -> String { self.0 .as_ref() .map_left(|bucket_name_and_balance| bucket_name_and_balance.bucket.group.clone()) .map_right(|bucket_group_name| bucket_group_name.group.clone()) .into_inner() } } impl<'a> From<&'a BucketNavigationEntry> for Element<'a, navigation::Message> { fn from(bucket_navigation_entry: &'a BucketNavigationEntry) -> Self { bucket_navigation_entry.0.as_ref().either_into() } } impl Focusable for BucketNavigationEntry { fn focus(&mut self) { match self.0.as_mut() { Either::Left(group) => group.focus(), Either::Right(bucket) => bucket.focus(), }; } fn unfocus(&mut self) { match self.0.as_mut() { Either::Left(group) => group.unfocus(), Either::Right(bucket) => bucket.unfocus(), }; } }