capsule_library/import/progress.rs
1use std::path::PathBuf;
2
3use uuid::Uuid;
4
5use crate::lifecycle::DerivativeStatus;
6
7/// Outcome for a single imported file.
8#[derive(Debug, Clone)]
9pub enum ImportOutcome {
10 /// The file was imported as a signed, encrypted, verifiable original.
11 ///
12 /// `derivatives` says whether a thumbnail/preview could be generated alongside it. It is
13 /// **not** a success/failure flag: an asset whose format has no codec in this build is a
14 /// fully successful import that happens to be missing its derivative.
15 Imported {
16 /// The asset the file became — what a Move-mode caller releases the source against.
17 asset_id: Uuid,
18 derivatives: DerivativeStatus,
19 /// How many `(tier, format)` pairs the tier table commits to and this build cannot
20 /// encode. Orthogonal to `derivatives`: a `Decoded` asset with a renderable JXL
21 /// thumbnail still reports the AVIF delivery variant and WebP as deferred, and that
22 /// count is how the gap shrinks visibly as codecs land rather than silently.
23 deferred_formats: u32,
24 },
25 DuplicateSkipped {
26 existing_uuid: String,
27 },
28 CorruptUnreadable(String),
29 CorruptTransfer,
30 PermissionDenied(String),
31 PartialStackImported {
32 imported: Vec<String>,
33 skipped: Vec<String>,
34 },
35 LivePhotoWithoutPair,
36}
37
38/// Progress events emitted during import execution.
39#[derive(Debug)]
40pub enum ImportProgressEvent {
41 ImportStarted {
42 total_candidates: u64,
43 total_files: u64,
44 },
45 CandidateStarted {
46 index: u64,
47 total: u64,
48 primary_path: PathBuf,
49 },
50 CandidateCompleted {
51 index: u64,
52 outcomes: Vec<(PathBuf, ImportOutcome)>,
53 },
54 ImportCompleted {
55 summary: ImportExecutionSummary,
56 },
57}
58
59/// Summary of a completed import run.
60#[derive(Debug, Default)]
61pub struct ImportExecutionSummary {
62 pub outcomes: Vec<(PathBuf, ImportOutcome)>,
63}
64
65impl ImportExecutionSummary {
66 pub fn imported_count(&self) -> usize {
67 self.outcomes
68 .iter()
69 .filter(|(_, o)| matches!(o, ImportOutcome::Imported { .. }))
70 .count()
71 }
72
73 /// How many of the imported assets landed **without** thumbnail/preview derivatives because
74 /// this build has no codec for their format.
75 ///
76 /// These are counted by [`imported_count`](Self::imported_count) too — they are successful
77 /// imports; the original is signed, encrypted and verifiable. This is the number a caller
78 /// reports as "N imported without derivatives" so a HEIC-only library does not look like it
79 /// silently lost its previews. Genuine decode failures of *supported* formats are excluded;
80 /// see [`decode_failed_count`](Self::decode_failed_count).
81 pub fn deferred_derivative_count(&self) -> usize {
82 self.outcomes
83 .iter()
84 .filter(|(_, o)| {
85 matches!(
86 o,
87 ImportOutcome::Imported {
88 derivatives: DerivativeStatus::DeferredNoCodec,
89 ..
90 }
91 )
92 })
93 .count()
94 }
95
96 /// The total number of `(tier, format)` pairs across the run that the tier table commits to
97 /// and this build cannot encode.
98 ///
99 /// **Not a failure count, and not comparable to
100 /// [`deferred_derivative_count`](Self::deferred_derivative_count).** That one counts *assets*
101 /// with no thumbnail at all; this one counts *format variants* missing from assets that do
102 /// have one. A library of decodable JPEGs reports zero deferred derivatives and two deferred
103 /// formats per asset — the JXL master and the AVIF delivery variant — which is exactly the
104 /// number that should fall to zero as the encoders land, and the reason it is reported rather
105 /// than left implicit in a doc.
106 pub fn deferred_format_count(&self) -> usize {
107 self.outcomes
108 .iter()
109 .map(|(_, o)| match o {
110 ImportOutcome::Imported {
111 deferred_formats, ..
112 } => *deferred_formats as usize,
113 _ => 0,
114 })
115 .sum()
116 }
117
118 /// How many imported assets are in a format this build *does* support but whose bytes did
119 /// not decode — unlike [`deferred_derivative_count`](Self::deferred_derivative_count) this
120 /// is a real problem worth surfacing, not an expected gap. The original is still imported.
121 pub fn decode_failed_count(&self) -> usize {
122 self.outcomes
123 .iter()
124 .filter(|(_, o)| {
125 matches!(
126 o,
127 ImportOutcome::Imported {
128 derivatives: DerivativeStatus::DecodeFailed,
129 ..
130 }
131 )
132 })
133 .count()
134 }
135
136 /// Every source file the library now holds, paired with the asset that holds it: each
137 /// imported file, and each file skipped as a duplicate of an asset already in the library.
138 ///
139 /// These are the sources a Move-mode import may delete, and only through the release gate
140 /// ([`Workspace::release_move_source`](crate::lifecycle::Workspace::release_move_source)):
141 /// the executor never deletes one itself. A duplicate is listed so a re-run of a Move import
142 /// whose first run kept its sources can release them; the gate's read-back check still
143 /// refuses any source whose bytes do not hash to that asset's signed original.
144 pub fn held_sources(&self) -> Vec<(PathBuf, Uuid)> {
145 self.outcomes
146 .iter()
147 .filter_map(|(path, outcome)| match outcome {
148 ImportOutcome::Imported { asset_id, .. } => Some((path.clone(), *asset_id)),
149 ImportOutcome::DuplicateSkipped { existing_uuid } => Uuid::parse_str(existing_uuid)
150 .ok()
151 .map(|asset_id| (path.clone(), asset_id)),
152 _ => None,
153 })
154 .collect()
155 }
156
157 pub fn duplicate_count(&self) -> usize {
158 self.outcomes
159 .iter()
160 .filter(|(_, o)| matches!(o, ImportOutcome::DuplicateSkipped { .. }))
161 .count()
162 }
163
164 pub fn error_count(&self) -> usize {
165 self.outcomes
166 .iter()
167 .filter(|(_, o)| {
168 matches!(
169 o,
170 ImportOutcome::CorruptUnreadable(_)
171 | ImportOutcome::CorruptTransfer
172 | ImportOutcome::PermissionDenied(_)
173 )
174 })
175 .count()
176 }
177}