1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Progress and cancellation hooks for the apply layer.
//!
//! [`ApplyObserver`] is the user-visible instrumentation surface for a long-
//! running [`apply_patch`](crate::ApplyConfig::apply_patch) call. It is designed
//! around two concrete needs from the downstream `gaveloc-patcher` consumer:
//!
//! - **Smooth UI progress** — a desktop launcher needs to drive a progress
//! bar while applying multi-GB FFXIV patches. Per-chunk events plus a
//! running byte counter are enough to compute "X of Y bytes applied"
//! without ever buffering the full patch in memory.
//! - **User-initiated cancellation** — a single [`SqpkFile`] `AddFile`
//! chunk can carry hundreds of megabytes of DEFLATE blocks. Chunk-
//! boundary checks are not enough; cancellation must be observable
//! *inside* the long-running block loop. The cheap
//! [`ApplyObserver::should_cancel`] predicate is polled between blocks.
//!
//! # Trait, not closure
//!
//! The observer is a **trait** rather than a closure so that callers can
//! own state across event methods (e.g. a UI handle, an [`mpsc::Sender`],
//! a cancellation token) without juggling shared captures. Every method has
//! a no-op default, so implementors only override what they care about.
//!
//! There is **no** blanket impl for closures. The trait carries
//! [`ApplyObserver::should_cancel`] alongside
//! [`ApplyObserver::on_chunk_applied`], and a closure can only carry one of
//! the two — silently filling in the "never cancel" default would disable
//! the cancellation path the consumer almost certainly wants. Implementors
//! pass a struct (typically owning an [`AtomicBool`](std::sync::atomic::AtomicBool)
//! cancellation flag) and override exactly the methods they need.
//!
//! # Default observer
//!
//! [`ApplyConfig`](crate::ApplyConfig) defaults to a no-op observer when
//! none is configured. Parsing-only consumers
//! ([`ZiPatchReader`](crate::ZiPatchReader) without
//! [`apply_patch`](crate::ApplyConfig::apply_patch)) pay nothing — the observer
//! is exclusively an apply-layer concept.
//!
//! [`SqpkFile`]: crate::chunk::sqpk::SqpkFile
//! [`mpsc::Sender`]: std::sync::mpsc::Sender
use crateChunkTag;
use ControlFlow;
/// One chunk-applied event delivered to an [`ApplyObserver`].
///
/// Fired after each top-level chunk's apply has completed successfully. The
/// `index` field is the zero-based ordinal of the chunk in the patch stream,
/// counting every chunk yielded by [`ZiPatchReader`](crate::ZiPatchReader) (the
/// internal `EOF_` terminator is not yielded and is not counted). The `kind`
/// is the 4-byte ASCII wire tag of the chunk, which lets the consumer
/// categorise events without needing to match on the [`Chunk`](crate::Chunk)
/// enum directly. `bytes_read` is the running total of bytes consumed from
/// the patch stream up to and including this chunk's frame (length prefix,
/// tag, body, and CRC32).
///
/// # Stability
///
/// The struct is `#[non_exhaustive]`. New fields may be added in future minor
/// versions (per-chunk elapsed time, decompressed byte counts, etc.).
/// Construct in tests via the [`ChunkEvent::new`] associated function rather
/// than struct-literal syntax.
/// Hook trait for observing apply-time progress and signalling cancellation.
///
/// All methods
/// have no-op defaults so implementors override only what they need.
///
/// # Threading
///
/// An observer is borrowed mutably by the apply driver for the lifetime of
/// the [`apply_patch`](crate::ApplyConfig::apply_patch) call. There is no
/// internal synchronisation: implementors that need to forward events to
/// another thread should do so via channels they own.
///
/// The trait has `Send + Sync` supertrait bounds so a boxed observer can
/// be constructed on one thread and driven on another — the typical UI
/// pattern is to construct the observer (often around an
/// [`mpsc::Sender`](std::sync::mpsc::Sender) or an
/// [`AtomicBool`](std::sync::atomic::AtomicBool) cancellation flag) on the
/// UI thread, hand it to an [`ApplyConfig`](crate::ApplyConfig), and ship
/// the context to an apply worker. `Sync` costs nothing for the realistic
/// implementations (channel senders, atomics, `Arc<Mutex<_>>`) and lets the
/// observer be shared by reference if a downstream consumer ever needs to.
///
/// # Async usage
///
/// Both [`Self::on_chunk_applied`] and [`Self::should_cancel`] run inline
/// with the apply loop and are intentionally synchronous — see the
/// crate-level "Async usage" section for the rationale. The cancellation
/// poll in particular is called once per `SqpkFile` `AddFile` block and
/// must be cheap (an atomic-bool load is the canonical implementation),
/// which makes it a poor fit for `async` even hypothetically.
///
/// Async consumers wrap the whole apply call in
/// `tokio::task::spawn_blocking` and use an
/// [`AtomicBool`](std::sync::atomic::AtomicBool) cancellation flag the
/// async side can flip from a `tokio::select!` arm or a cancellation
/// token. Per-chunk events that need to reach an async UI go through a
/// channel whose `Sender` lives inside [`Self::on_chunk_applied`] and
/// whose `Receiver` is polled from the async task.
///
/// # Example
///
/// ```no_run
/// use std::ops::ControlFlow;
/// use zipatch_rs::{ApplyConfig, ApplyObserver, ChunkEvent, open_patch};
///
/// struct Progress {
/// total: u64,
/// applied: u64,
/// }
///
/// impl ApplyObserver for Progress {
/// fn on_chunk_applied(&mut self, ev: ChunkEvent) -> ControlFlow<(), ()> {
/// self.applied = ev.bytes_read;
/// println!("progress: {}/{}", self.applied, self.total);
/// ControlFlow::Continue(())
/// }
/// }
///
/// let mut ctx = ApplyConfig::new("/opt/ffxiv/game")
/// .with_observer(Progress { total: 12_345_678, applied: 0 });
/// let reader = open_patch("patch.patch").unwrap();
/// ctx.apply_patch(reader).unwrap();
/// ```
/// No-op observer used by [`ApplyConfig`](crate::ApplyConfig) when none
/// is configured.
///
/// Public because [`ApplyConfig::with_observer`](crate::ApplyConfig::with_observer)
/// is generic over `impl ApplyObserver + 'static` and callers occasionally need
/// to name the default in `Box<dyn ApplyObserver>`-typed fields. All trait
/// methods use the trait defaults.
;