1use std::collections::BTreeSet;
41
42use serde::{Deserialize, Serialize};
43use serde_json::Value as Json;
44
45use crate::jobs::{Job, JobBadge, JobState};
46
47pub const OWN_SOURCE: &str = "egui_pop.jobs";
50
51#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
53#[serde(rename_all = "kebab-case")]
54pub enum RowState {
55 Queued,
57 #[default]
59 Running,
60 Waiting,
62 Done,
64 Failed,
66}
67
68impl RowState {
69 pub fn active(self) -> bool {
71 matches!(
72 self,
73 RowState::Queued | RowState::Running | RowState::Waiting
74 )
75 }
76
77 pub fn name(self) -> &'static str {
79 match self {
80 RowState::Queued => "queued",
81 RowState::Running => "running",
82 RowState::Waiting => "waiting",
83 RowState::Done => "done",
84 RowState::Failed => "failed",
85 }
86 }
87
88 pub fn of_job(state: JobState) -> RowState {
91 match state {
92 JobState::Running => RowState::Running,
93 JobState::Paused | JobState::Cancelling => RowState::Waiting,
94 JobState::Done | JobState::Cancelled => RowState::Done,
95 JobState::Failed => RowState::Failed,
96 }
97 }
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
102#[serde(rename_all = "kebab-case")]
103pub enum RowActionKind {
104 Cancel,
105 Retry,
106 Open,
107}
108
109#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
113#[non_exhaustive]
114pub struct RowAction {
115 pub kind: RowActionKind,
116 pub command: String,
118 #[serde(default, skip_serializing_if = "Json::is_null")]
120 pub args: Json,
121 #[serde(default, skip_serializing_if = "Option::is_none")]
123 pub title: Option<String>,
124}
125
126impl RowAction {
127 pub fn new(kind: RowActionKind, command: impl Into<String>) -> Self {
128 Self {
129 kind,
130 command: command.into(),
131 args: Json::Null,
132 title: None,
133 }
134 }
135
136 pub fn args(mut self, args: Json) -> Self {
137 self.args = args;
138 self
139 }
140
141 pub fn title(mut self, title: impl Into<String>) -> Self {
142 self.title = Some(title.into());
143 self
144 }
145}
146
147#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
149#[non_exhaustive]
150pub struct JobRow {
151 pub id: String,
153 pub title: String,
154 #[serde(default)]
155 pub state: RowState,
156 #[serde(default, skip_serializing_if = "Option::is_none")]
158 pub progress: Option<f64>,
159 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub detail: Option<String>,
162 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub badge: Option<JobBadge>,
165 #[serde(default, skip_serializing_if = "Option::is_none")]
167 pub group: Option<String>,
168 #[serde(default, skip_serializing_if = "Vec::is_empty")]
169 pub actions: Vec<RowAction>,
170}
171
172impl JobRow {
173 pub fn new(id: impl Into<String>, title: impl Into<String>, state: RowState) -> Self {
174 Self {
175 id: id.into(),
176 title: title.into(),
177 state,
178 ..Self::default()
179 }
180 }
181
182 pub fn progress(mut self, fraction: f64) -> Self {
183 self.progress = Some(fraction.clamp(0.0, 1.0));
184 self
185 }
186
187 pub fn detail(mut self, detail: impl Into<String>) -> Self {
188 self.detail = Some(detail.into());
189 self
190 }
191
192 pub fn badge(mut self, badge: JobBadge) -> Self {
193 self.badge = Some(badge);
194 self
195 }
196
197 pub fn group(mut self, group: impl Into<String>) -> Self {
198 self.group = Some(group.into());
199 self
200 }
201
202 pub fn action(mut self, action: RowAction) -> Self {
203 self.actions.push(action);
204 self
205 }
206}
207
208#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
211#[non_exhaustive]
212pub struct RowSource {
213 pub id: String,
214 pub title: String,
215 pub owner: String,
218 pub rows: Vec<JobRow>,
219}
220
221impl RowSource {
222 pub fn new(id: impl Into<String>, title: impl Into<String>, owner: impl Into<String>) -> Self {
223 Self {
224 id: id.into(),
225 title: title.into(),
226 owner: owner.into(),
227 rows: Vec::new(),
228 }
229 }
230}
231
232#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
234#[serde(rename_all = "kebab-case")]
235pub enum StateFilter {
236 #[default]
237 All,
238 Active,
240 Done,
241 Failed,
242}
243
244impl StateFilter {
245 pub const ALL: [StateFilter; 4] = [
246 StateFilter::All,
247 StateFilter::Active,
248 StateFilter::Done,
249 StateFilter::Failed,
250 ];
251
252 pub fn admits(self, state: RowState) -> bool {
253 match self {
254 StateFilter::All => true,
255 StateFilter::Active => state.active(),
256 StateFilter::Done => state == RowState::Done,
257 StateFilter::Failed => state == RowState::Failed,
258 }
259 }
260}
261
262#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
265#[non_exhaustive]
266pub struct RowFilter {
267 #[serde(default)]
269 pub hidden: BTreeSet<String>,
270 #[serde(default)]
271 pub state: StateFilter,
272}
273
274impl RowFilter {
275 pub fn shows(&self, source: &str) -> bool {
277 !self.hidden.contains(source)
278 }
279
280 pub fn set_shown(&mut self, source: &str, shown: bool) {
282 if shown {
283 self.hidden.remove(source);
284 } else {
285 self.hidden.insert(source.into());
286 }
287 }
288
289 pub fn admits(&self, source: &str, state: RowState) -> bool {
291 self.shows(source) && self.state.admits(state)
292 }
293
294 pub fn admits_job(&self, job: &Job) -> bool {
296 self.admits(OWN_SOURCE, RowState::of_job(job.state))
297 }
298}
299
300#[cfg(test)]
301mod tests {
302 use super::*;
303 use serde_json::json;
304
305 #[test]
306 fn a_row_round_trips_and_takes_its_defaults() {
307 let row: JobRow = serde_json::from_value(json!({ "id": "a", "title": "A" })).unwrap();
308 assert_eq!(row.state, RowState::Running);
309 assert_eq!(
310 serde_json::to_value(&row).unwrap(),
311 json!({ "id": "a", "title": "A", "state": "running" })
312 );
313 }
314
315 #[test]
316 fn the_filter_hides_sources_and_states() {
317 let mut f = RowFilter::default();
318 assert!(f.admits("nodes", RowState::Queued));
319 f.set_shown("nodes", false);
320 assert!(!f.admits("nodes", RowState::Queued));
321 assert!(f.admits(OWN_SOURCE, RowState::Done));
322 f.state = StateFilter::Active;
323 assert!(!f.admits(OWN_SOURCE, RowState::Done));
324 assert!(f.admits(OWN_SOURCE, RowState::Waiting));
325 f.state = StateFilter::Failed;
326 assert!(f.admits(OWN_SOURCE, RowState::Failed));
327 assert_eq!(RowState::of_job(JobState::Cancelled), RowState::Done);
328 }
329}