use crate::Id;
use std::hash::Hash;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListEntryKind {
Item,
Header,
Separator,
}
#[derive(Clone, Copy, Debug)]
pub struct ListEntry<'a> {
pub key: Id,
pub kind: ListEntryKind,
pub enabled: bool,
pub text: &'a str,
}
impl<'a> ListEntry<'a> {
pub fn item(key: impl Hash, text: &'a str) -> Self {
Self {
key: Id::new(key),
kind: ListEntryKind::Item,
enabled: true,
text,
}
}
pub fn header(key: impl Hash, text: &'a str) -> Self {
Self {
kind: ListEntryKind::Header,
..Self::item(key, text)
}
}
pub fn separator(key: impl Hash) -> Self {
Self {
kind: ListEntryKind::Separator,
..Self::item(key, "")
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub(crate) fn selectable(&self) -> bool {
self.enabled && self.kind == ListEntryKind::Item
}
}
pub trait ListModel {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn entry(&self, index: usize) -> ListEntry<'_>;
fn revision(&self) -> u64 {
0
}
}
pub(super) struct FnModel<'a, F> {
pub len: usize,
pub revision: u64,
pub entry: F,
pub borrowed: std::marker::PhantomData<&'a ()>,
}
impl<'a, F: Fn(usize) -> ListEntry<'a>> ListModel for FnModel<'a, F> {
fn len(&self) -> usize {
self.len
}
fn entry(&self, index: usize) -> ListEntry<'_> {
(self.entry)(index)
}
fn revision(&self) -> u64 {
self.revision
}
}
pub(super) struct SliceModel<'a, T, F> {
pub items: &'a [T],
pub revision: u64,
pub entry: F,
}
impl<T, F: for<'x> Fn(&'x T) -> ListEntry<'x>> ListModel for SliceModel<'_, T, F> {
fn len(&self) -> usize {
self.items.len()
}
fn entry(&self, index: usize) -> ListEntry<'_> {
(self.entry)(&self.items[index])
}
fn revision(&self) -> u64 {
self.revision
}
}