kernel/artifacts/
gallery.rs1use super::artifact::Artifact;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum GallerySort {
10 NewestFirst,
11 OldestFirst,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub struct GalleryModel {
17 pub id: String,
18 pub name: String,
19}
20
21pub struct Gallery;
23
24impl Gallery {
25 pub fn models(artifacts: &[Artifact]) -> Vec<GalleryModel> {
28 let mut sorted: Vec<&Artifact> = artifacts.iter().collect();
29 sorted.sort_by(|a, b| newest(a, b));
30 let mut seen = std::collections::HashSet::new();
31 sorted
32 .into_iter()
33 .filter(|artifact| seen.insert(artifact.model_id.clone()))
34 .map(|artifact| GalleryModel {
35 id: artifact.model_id.clone(),
36 name: artifact.model.clone(),
37 })
38 .collect()
39 }
40
41 pub fn arrange(
43 artifacts: &[Artifact],
44 model_id: Option<&str>,
45 sort: GallerySort,
46 ) -> Vec<Artifact> {
47 let mut filtered: Vec<Artifact> = artifacts
48 .iter()
49 .filter(|artifact| model_id.is_none_or(|id| artifact.model_id == id))
50 .cloned()
51 .collect();
52 match sort {
53 GallerySort::NewestFirst => filtered.sort_by(newest),
54 GallerySort::OldestFirst => filtered.sort_by(|a, b| newest(b, a)),
55 }
56 filtered
57 }
58}
59
60pub(crate) fn newest(a: &Artifact, b: &Artifact) -> std::cmp::Ordering {
63 (b.created_at, &b.id).cmp(&(a.created_at, &a.id))
64}