workyard_state/records/task/
compatibility.rs1use super::{OperationRecord, ParticipantRecord, ProblemRecord, TaskRecord};
3use crate::path::StoredPath;
4use serde::{Deserialize, Deserializer, de::Error};
5
6#[derive(Deserialize)]
7#[serde(deny_unknown_fields)]
8struct Document {
9 version: u32,
10 #[serde(default)]
11 generation: u64,
12 #[serde(default)]
13 removal: Option<super::RemovalRecord>,
14 task: String,
15 tasks_root: StoredPath,
16 state_dir: StoredPath,
17 checkout: bool,
18 no_fetch: bool,
19 pull: bool,
20 #[serde(default)]
21 status: Option<String>,
22 #[serde(default)]
23 lifecycle: Option<String>,
24 #[serde(default)]
25 operation: Option<OperationRecord>,
26 participants: Vec<ParticipantRecord>,
27 problem: Option<ProblemRecord>,
28}
29
30impl<'de> Deserialize<'de> for TaskRecord {
31 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
32 let value = serde_json::Value::deserialize(deserializer)?;
33 let version = value.get("version").and_then(serde_json::Value::as_u64);
34 let mixed = match version {
35 Some(1) => {
36 value.get("lifecycle").is_some()
37 || value.get("operation").is_some()
38 || value.get("generation").is_some()
39 || value.get("removal").is_some()
40 }
41 Some(2) => {
42 value.get("status").is_some()
43 || value.get("generation").is_some()
44 || value.get("removal").is_some()
45 }
46 Some(3 | 4) => {
47 value.get("status").is_some()
48 || value.get("generation").is_none()
49 || value.get("removal").is_none()
50 }
51 _ => true,
52 };
53 if mixed {
54 return Err(D::Error::custom(
55 "Unsupported or mixed task document format",
56 ));
57 }
58 let mut document: Document = serde_json::from_value(value).map_err(D::Error::custom)?;
59 let (lifecycle, operation) = match document.version {
60 1 if document.lifecycle.is_none() && document.operation.is_none() => {
61 let status = document
62 .status
63 .as_deref()
64 .ok_or_else(|| D::Error::custom("Missing legacy task status"))?;
65 let outcome = match status {
66 "completed" => "succeeded",
67 "pending" | "failed" | "cancelled" => status,
68 _ => return Err(D::Error::custom("Invalid legacy task status")),
69 };
70 (
71 "active".into(),
72 OperationRecord {
73 kind: None,
74 continues: None,
75 outcome: outcome.into(),
76 },
77 )
78 }
79 2..=4 if document.status.is_none() => {
80 let lifecycle = document
81 .lifecycle
82 .ok_or_else(|| D::Error::custom("Missing task lifecycle"))?;
83 let operation = document
84 .operation
85 .ok_or_else(|| D::Error::custom("Missing task operation"))?;
86 (lifecycle, operation)
87 }
88 _ => {
89 return Err(D::Error::custom(
90 "Unsupported or mixed task document format",
91 ));
92 }
93 };
94
95 if document.pull && document.no_fetch {
98 return Err(D::Error::custom("Invalid legacy creation options"));
99 }
100 for options in document
101 .participants
102 .iter_mut()
103 .filter_map(|p| p.creation.as_mut())
104 {
105 if options.legacy_pull && options.no_fetch {
106 return Err(D::Error::custom(
107 "Invalid legacy participant creation options",
108 ));
109 }
110 options.legacy_pull = false;
111 }
112
113 Ok(TaskRecord {
114 version: document.version.max(3),
116 generation: document.generation,
117 removal: document.removal,
118 task: document.task,
119 tasks_root: document.tasks_root,
120 state_dir: document.state_dir,
121 checkout: document.checkout,
122 no_fetch: document.no_fetch,
123 legacy_pull: false,
124 lifecycle,
125 operation,
126 participants: document.participants,
127 problem: document.problem,
128 })
129 }
130}