1use std::path::PathBuf;
4
5use crate::result::LatchRequest;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct JobId(pub u64);
10
11impl std::fmt::Display for JobId {
12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13 write!(f, "{}", self.0)
14 }
15}
16
17#[non_exhaustive]
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum JobStatus {
21 Running,
23 Stopped,
25 Done,
27 Latched,
32 Failed,
34}
35
36impl std::fmt::Display for JobStatus {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 match self {
39 JobStatus::Running => write!(f, "Running"),
40 JobStatus::Stopped => write!(f, "Stopped"),
41 JobStatus::Done => write!(f, "Done"),
42 JobStatus::Latched => write!(f, "Latched"),
43 JobStatus::Failed => write!(f, "Failed"),
44 }
45 }
46}
47
48#[non_exhaustive]
50#[derive(Debug, Clone)]
51pub struct JobInfo {
52 pub id: JobId,
54 pub command: String,
56 pub status: JobStatus,
58 pub output_file: Option<PathBuf>,
60 pub pid: Option<u32>,
62 pub latch: Option<LatchRequest>,
67}
68
69impl JobInfo {
70 pub fn new(id: JobId, command: impl Into<String>, status: JobStatus) -> Self {
76 Self {
77 id,
78 command: command.into(),
79 status,
80 output_file: None,
81 pid: None,
82 latch: None,
83 }
84 }
85
86 pub fn with_output_file(mut self, output_file: Option<PathBuf>) -> Self {
88 self.output_file = output_file;
89 self
90 }
91
92 pub fn with_pid(mut self, pid: Option<u32>) -> Self {
94 self.pid = pid;
95 self
96 }
97
98 pub fn with_latch(mut self, latch: Option<LatchRequest>) -> Self {
100 self.latch = latch;
101 self
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn new_defaults_optional_fields_to_none() {
111 let info = JobInfo::new(JobId(1), "echo hi", JobStatus::Running);
112 assert_eq!(info.id, JobId(1));
113 assert_eq!(info.command, "echo hi");
114 assert_eq!(info.status, JobStatus::Running);
115 assert!(info.output_file.is_none());
116 assert!(info.pid.is_none());
117 assert!(info.latch.is_none());
118 }
119
120 #[test]
121 fn with_setters_chain_and_override_defaults() {
122 let info = JobInfo::new(JobId(2), "sleep 1", JobStatus::Done)
123 .with_output_file(Some(PathBuf::from("job-output.txt")))
124 .with_pid(Some(1234))
125 .with_latch(None);
126 assert_eq!(info.output_file, Some(PathBuf::from("job-output.txt")));
127 assert_eq!(info.pid, Some(1234));
128 assert!(info.latch.is_none());
129 }
130}