use serde::{Deserialize, Serialize};
use crate::core::Timestamp;
use crate::core::macros::{tmf_entity, tmf_struct};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum JobState {
#[serde(rename = "Not Started")]
NotStarted,
#[serde(rename = "Running")]
Running,
#[serde(rename = "Succeeded")]
Succeeded,
#[serde(rename = "Failed")]
Failed,
#[serde(untagged)]
Other(String),
}
impl JobState {
#[must_use]
pub fn is_finished(&self) -> bool {
matches!(self, Self::Succeeded | Self::Failed)
}
#[must_use]
pub fn is_success(&self) -> bool {
matches!(self, Self::Succeeded)
}
}
tmf_struct! {
@name = "ImportJob";
pub struct ImportJob {
id: String,
href: String,
url: String,
path: String,
content_type: String,
status: JobState,
creation_date: Timestamp,
completion_date: Timestamp,
error_log: String,
}
}
tmf_struct! {
@name = "ImportJob";
pub struct ImportJobCreate {
@required {
url: String,
}
id: String,
path: String,
content_type: String,
status: JobState,
creation_date: Timestamp,
completion_date: Timestamp,
error_log: String,
}
}
tmf_struct! {
@name = "ExportJob";
pub struct ExportJob {
id: String,
href: String,
url: String,
path: String,
content_type: String,
query: String,
status: JobState,
creation_date: Timestamp,
completion_date: Timestamp,
error_log: String,
}
}
tmf_struct! {
@name = "ExportJob";
pub struct ExportJobCreate {
@required {
url: String,
}
id: String,
path: String,
content_type: String,
query: String,
status: JobState,
creation_date: Timestamp,
completion_date: Timestamp,
error_log: String,
}
}
tmf_entity!(ImportJob, ExportJob);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn job_state_round_trips_spec_spellings_with_spaces() {
let job: ImportJob = serde_json::from_str(r#"{"status":"Not Started"}"#).unwrap();
assert_eq!(job.status, Some(JobState::NotStarted));
assert!(!job.status.as_ref().unwrap().is_finished());
assert_eq!(serde_json::to_value(&job).unwrap()["status"], "Not Started");
}
#[test]
fn unknown_job_state_is_preserved_and_not_finished() {
let job: ImportJob = serde_json::from_str(r#"{"status":"Paused"}"#).unwrap();
assert_eq!(job.status, Some(JobState::Other("Paused".into())));
assert!(!job.status.as_ref().unwrap().is_finished());
}
#[test]
fn export_job_carries_a_query_but_import_does_not() {
let export = ExportJobCreate::builder()
.url("https://files/out.json")
.query("lifecycleStatus=Active")
.build();
assert_eq!(export.query.as_deref(), Some("lifecycleStatus=Active"));
}
}