use super::artifact::Artifact;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GallerySort {
NewestFirst,
OldestFirst,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GalleryModel {
pub id: String,
pub name: String,
}
pub struct Gallery;
impl Gallery {
pub fn models(artifacts: &[Artifact]) -> Vec<GalleryModel> {
let mut sorted: Vec<&Artifact> = artifacts.iter().collect();
sorted.sort_by(|a, b| newest(a, b));
let mut seen = std::collections::HashSet::new();
sorted
.into_iter()
.filter(|artifact| seen.insert(artifact.model_id.clone()))
.map(|artifact| GalleryModel {
id: artifact.model_id.clone(),
name: artifact.model.clone(),
})
.collect()
}
pub fn arrange(
artifacts: &[Artifact],
model_id: Option<&str>,
sort: GallerySort,
) -> Vec<Artifact> {
let mut filtered: Vec<Artifact> = artifacts
.iter()
.filter(|artifact| model_id.is_none_or(|id| artifact.model_id == id))
.cloned()
.collect();
match sort {
GallerySort::NewestFirst => filtered.sort_by(newest),
GallerySort::OldestFirst => filtered.sort_by(|a, b| newest(b, a)),
}
filtered
}
}
pub(crate) fn newest(a: &Artifact, b: &Artifact) -> std::cmp::Ordering {
(b.created_at, &b.id).cmp(&(a.created_at, &a.id))
}