ic_backup/model/download_journal/view/
mod.rs1use super::{ArtifactStateRecord, DownloadJournalRecord};
4use serde::Serialize;
5
6#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
8pub enum ResumeAction {
9 Download,
11 VerifyChecksum,
13 Finalize,
15 Skip,
17}
18
19#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
21pub struct DownloadArtifactView {
22 pub canister_id: String,
24 pub snapshot_id: String,
26 pub state: ArtifactStateRecord,
28 pub resume_action: ResumeAction,
30}
31
32#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
34pub struct DownloadJournalView {
35 pub intent: String,
37 pub pending_artifacts: usize,
39 pub is_complete: bool,
41 pub artifacts: Vec<DownloadArtifactView>,
43}
44
45impl DownloadJournalRecord {
46 #[must_use]
48 pub fn resume_view(&self) -> DownloadJournalView {
49 let artifacts: Vec<_> = self
50 .artifacts()
51 .iter()
52 .map(|artifact| DownloadArtifactView {
53 canister_id: artifact.canister_id().to_owned(),
54 snapshot_id: artifact.snapshot_id().to_owned(),
55 state: artifact.state(),
56 resume_action: match artifact.state() {
57 ArtifactStateRecord::Created => ResumeAction::Download,
58 ArtifactStateRecord::Downloaded => ResumeAction::VerifyChecksum,
59 ArtifactStateRecord::ChecksumVerified => ResumeAction::Finalize,
60 ArtifactStateRecord::Durable => ResumeAction::Skip,
61 },
62 })
63 .collect();
64 let pending_artifacts = artifacts
65 .iter()
66 .filter(|entry| entry.state != ArtifactStateRecord::Durable)
67 .count();
68 DownloadJournalView {
69 intent: self.intent().to_owned(),
70 pending_artifacts,
71 is_complete: pending_artifacts == 0,
72 artifacts,
73 }
74 }
75}