use std::collections::BTreeMap;
use crate::model::join::Conflict;
use crate::model::types::PaneKey;
use crate::view::lines::{Content, GroupKind, Item, Line, Place};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum Handle {
Bead(Place),
Elided(Place),
Group(GroupKind, Option<String>),
Item(ItemKey),
Project(String),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum ItemKey {
Pane(PaneKey),
Project(String),
Conflict(Conflict),
}
pub(super) fn item_key(item: &Item) -> Option<ItemKey> {
Some(match item {
Item::Loose(pane) => ItemKey::Pane(pane.pane.clone()),
Item::Unconfigured(pane) => ItemKey::Pane(pane.pane.clone()),
Item::Failed(failed) => ItemKey::Project(failed.project.clone()),
Item::Conflict(conflict) => ItemKey::Conflict(conflict.clone()),
})
}
#[derive(Default)]
pub(super) struct Folds(BTreeMap<Handle, bool>);
impl Folds {
pub(super) fn expanded(&self, handle: &Handle, resting: bool) -> bool {
self.0.get(handle).copied().unwrap_or(resting)
}
pub(super) fn set(&mut self, handle: Handle, open: bool) {
self.0.insert(handle, open);
}
pub(super) fn shut(&self) -> impl Iterator<Item = &Handle> {
self.0
.iter()
.filter(|(_, open)| !**open)
.map(|(handle, _)| handle)
}
pub(super) fn spend(&mut self, spent: &[Handle]) {
for handle in spent {
self.0.remove(handle);
}
}
pub(super) fn clear(&mut self) {
self.0.clear();
}
}
pub(super) fn handle_of(line: &Line) -> Option<Handle> {
match &line.content {
Content::Bead(_) | Content::Unread(_) => line.place.clone().map(Handle::Bead),
Content::Project(line) => Some(Handle::Project(line.project.clone())),
Content::Elided { under, .. } => Some(Handle::Elided(under.clone())),
Content::Group(group) => Some(Handle::Group(group.kind, group.project.clone())),
Content::Item(item) => item_key(item).map(Handle::Item),
Content::Note(_) | Content::Scoped { .. } => None,
}
}
pub(super) fn selectable(line: &Line) -> bool {
handle_of(line).is_some()
}