1#[cfg(test)]
19mod tests;
20
21use std::collections::HashMap;
22
23use serde::Deserialize;
24use serde::de::IgnoredAny;
25use snafu::{ResultExt, ensure};
26
27use crate::error::{Result, SerdeJsonSnafu, UnsupportedOperationSnafu, Utf8Snafu};
28
29const EDIT_ACTION_TAG: &str = "Edit";
34
35#[derive(Deserialize)]
36struct TaskStateGuard {
37 table_compaction: Option<IgnoredAny>,
38 extensions: Option<IgnoredAny>,
39}
40
41impl TaskStateGuard {
42 fn validate(&self) -> Result<()> {
43 ensure!(self.table_compaction.is_none() && self.extensions.is_none(), UnsupportedOperationSnafu {
44 err_msg: "Legacy compaction task state requires explicit migration; it cannot be discarded".to_string(),
45 });
46 Ok(())
47 }
48}
49
50pub(crate) fn validate_manifest(bytes: &[u8]) -> Result<()> {
55 let data = std::str::from_utf8(bytes).context(Utf8Snafu)?;
56 let state: TaskStateGuard = serde_json::from_str(data).context(SerdeJsonSnafu)?;
57 state.validate()
58}
59
60pub(crate) fn validate_checkpoint(json: &str) -> Result<()> {
61 #[derive(Deserialize)]
62 struct CheckpointGuard {
63 checkpoint: Option<TaskStateGuard>,
64 }
65 let guard: CheckpointGuard = serde_json::from_str(json).context(SerdeJsonSnafu)?;
66 if let Some(state) = guard.checkpoint {
67 state.validate()?;
68 }
69 Ok(())
70}
71
72pub(crate) fn validate_actions(json: &str) -> Result<()> {
73 #[derive(Deserialize)]
74 struct ActionListGuard {
75 actions: Vec<HashMap<String, TaskStateGuard>>,
76 }
77 let guard: ActionListGuard = serde_json::from_str(json).context(SerdeJsonSnafu)?;
78 for action in guard.actions {
79 if let Some(edit) = action.get(EDIT_ACTION_TAG) {
80 edit.validate()?;
81 }
82 }
83 Ok(())
84}