s3-unspool 0.1.0-beta.6

Fast streaming extraction of large ZIP archives from S3 into S3 prefixes with conditional writes.
Documentation
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use async_zip::Compression;
use serde::{Deserialize, Serialize};

use crate::constants::*;
use crate::s3_uri::{S3Object, S3Prefix};

/// Compression method used for regular file entries when creating ZIP archives.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ZipCompression {
    /// Use Deflate, the default ZIP compression method supported by common tools.
    #[default]
    Deflate,
    /// Use Zstandard method 93 for regular file entries.
    #[cfg(feature = "zstd")]
    Zstd,
}

impl ZipCompression {
    pub(crate) fn to_async_zip(self) -> Compression {
        match self {
            ZipCompression::Deflate => Compression::Deflate,
            #[cfg(feature = "zstd")]
            ZipCompression::Zstd => Compression::Zstd,
        }
    }

    /// Returns a stable lowercase name for display or configuration.
    pub fn as_str(self) -> &'static str {
        match self {
            ZipCompression::Deflate => "deflate",
            #[cfg(feature = "zstd")]
            ZipCompression::Zstd => "zstd",
        }
    }
}

/// ZIP entry selection patterns for unzip APIs.
///
/// When no patterns are configured, unzip operations process every supported ZIP
/// entry. Patterns are matched against normalized ZIP paths, not local
/// filesystem paths or destination S3 keys. Use
/// [`UnzipSelection::new`]/[`UnzipSelection::include`] for builder-style
/// configuration, or pass an array such as `["docs/**", "!docs/drafts/**"]`
/// to `with_selection`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct UnzipSelection {
    patterns: Vec<String>,
}

impl UnzipSelection {
    /// Creates an empty selection that extracts every supported ZIP entry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a selection from ordered include/exclude patterns.
    ///
    /// Patterns use gitignore-style matching. Later patterns override earlier
    /// patterns, and patterns prefixed with `!` exclude matching ZIP paths.
    /// If only exclude patterns are configured, every non-excluded ZIP path is
    /// selected.
    pub fn patterns(patterns: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self {
            patterns: patterns.into_iter().map(Into::into).collect(),
        }
    }

    /// Adds an include pattern.
    ///
    /// Leading `!` and `#` characters are treated as literal path characters
    /// in the builder API. Use [`Self::patterns`] for raw selection lines.
    pub fn include(mut self, pattern: impl Into<String>) -> Self {
        self.patterns
            .push(escape_leading_gitignore_marker(pattern.into()));
        self
    }

    /// Adds an exclude pattern.
    ///
    /// Leading `!` and `#` characters are treated as literal path characters
    /// in the builder API. Use [`Self::patterns`] for raw selection lines.
    pub fn exclude(mut self, pattern: impl Into<String>) -> Self {
        self.patterns.push(format!(
            "!{}",
            escape_leading_gitignore_marker(pattern.into())
        ));
        self
    }

    /// Returns true when no selection patterns have been configured.
    pub fn is_empty(&self) -> bool {
        self.patterns.is_empty()
    }

    /// Returns the ordered selection patterns.
    pub fn as_patterns(&self) -> &[String] {
        &self.patterns
    }
}

impl<const N: usize> From<[&str; N]> for UnzipSelection {
    fn from(patterns: [&str; N]) -> Self {
        Self::patterns(patterns)
    }
}

impl From<Vec<String>> for UnzipSelection {
    fn from(patterns: Vec<String>) -> Self {
        Self { patterns }
    }
}

fn escape_leading_gitignore_marker(pattern: String) -> String {
    if pattern.starts_with('!') || pattern.starts_with('#') {
        format!("\\{pattern}")
    } else {
        pattern
    }
}

/// How an unzip-to-S3 operation treats destination objects that are not in the ZIP.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum DestinationCleanup {
    /// Keep destination objects that do not correspond to selected ZIP entries.
    #[default]
    KeepExtra,
    /// Delete destination objects under the destination prefix that are not in the ZIP.
    DeleteExtra,
}

impl DestinationCleanup {
    pub(crate) fn deletes_extra(self) -> bool {
        matches!(self, Self::DeleteExtra)
    }
}

/// How unzip operations compare ZIP entries with existing destination objects.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ComparisonMode {
    /// Use the embedded catalog when present, then fall back to entry hashing.
    #[default]
    CatalogThenHash,
    /// Ignore any embedded catalog and hash ZIP entries for comparison.
    HashEntries,
}

impl ComparisonMode {
    pub(crate) fn ignores_embedded_catalog(self) -> bool {
        matches!(self, Self::HashEntries)
    }
}

/// How unzip-to-S3 operations handle destination conditional write conflicts.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ConflictPolicy {
    /// Record conditional conflicts in the report and continue processing.
    #[default]
    ReportAndContinue,
    /// Return an error after the first conditional conflict is observed.
    FailFast,
}

impl ConflictPolicy {
    pub(crate) fn fails_fast(self) -> bool {
        matches!(self, Self::FailFast)
    }
}

/// Options for extracting a ZIP object from S3 into an S3 prefix.
#[derive(Clone, Debug)]
pub struct SyncOptions {
    /// Source ZIP object.
    pub(crate) source: S3Object,
    /// Destination prefix that receives ZIP entries.
    pub(crate) destination: S3Prefix,
    /// How destination objects outside the ZIP are handled.
    ///
    /// Deleting extra objects requires a non-empty destination prefix so a
    /// bucket root is never swept accidentally.
    pub(crate) cleanup: DestinationCleanup,
    /// ZIP entry selection. Empty selection extracts every supported entry.
    ///
    /// Selection cannot be combined with [`DestinationCleanup::DeleteExtra`].
    pub(crate) selection: UnzipSelection,
    /// Collect source scheduler diagnostics in the returned report.
    pub(crate) collect_diagnostics: bool,
    /// Comparison policy for embedded catalogs and entry hashing.
    pub(crate) comparison: ComparisonMode,
    /// Conditional write conflict handling policy.
    pub(crate) conflict_policy: ConflictPolicy,
    /// Collect one operation record per processed object in the returned report.
    pub(crate) collect_operations: bool,
    /// Maximum number of ZIP entries processed concurrently.
    ///
    /// Must be greater than zero.
    pub(crate) concurrency: usize,
    /// Maximum number of destination `PutObject` requests in flight.
    ///
    /// Must be greater than zero.
    pub(crate) put_concurrency: usize,
    /// Retry and backoff policy for destination `PutObject` attempts.
    pub(crate) put_retry_policy: PutRetryPolicy,
    /// Maximum size for planned source ZIP blocks.
    ///
    /// Must be greater than zero.
    pub(crate) source_block_size: usize,
    /// Maximum gap that can be read while coalescing adjacent source spans.
    pub(crate) source_block_merge_gap: usize,
    /// Maximum number of ranged source `GetObject` requests in flight.
    ///
    /// Must be greater than zero.
    pub(crate) source_get_concurrency: usize,
    /// Maximum bytes held by the planned source block window.
    ///
    /// When nonzero, this must be large enough to hold the effective source
    /// block size after clamping that block size to the source ZIP size.
    pub(crate) source_window_capacity: usize,
    /// Available memory budget, in MiB, used to derive the source block window.
    ///
    /// When set, extraction computes [`Self::source_window_capacity`] after the
    /// ZIP manifest is loaded, using the real source ZIP size and file count.
    /// This is useful for memory-bounded runtimes that want to assign otherwise
    /// idle memory to source block buffering while reserving space for catalog
    /// metadata and worker overhead.
    pub(crate) source_window_memory_budget_mb: Option<u64>,
    /// Buffer size used when streaming entry bodies to S3.
    ///
    /// Must be greater than zero and no larger than 16 MiB.
    pub(crate) body_chunk_size: usize,
    /// Capacity of the in-memory pipe between decompression and S3 upload.
    ///
    /// Must be greater than zero and no larger than 64 MiB.
    pub(crate) pipe_capacity: usize,
}

impl SyncOptions {
    /// Creates extract options for a source ZIP object and destination prefix.
    pub fn new(source: S3Object, destination: S3Prefix) -> Self {
        Self {
            source,
            destination,
            cleanup: DestinationCleanup::default(),
            selection: UnzipSelection::default(),
            collect_diagnostics: false,
            comparison: ComparisonMode::default(),
            conflict_policy: ConflictPolicy::default(),
            collect_operations: true,
            concurrency: DEFAULT_CONCURRENCY,
            put_concurrency: DEFAULT_PUT_CONCURRENCY,
            put_retry_policy: PutRetryPolicy::default(),
            source_block_size: DEFAULT_SOURCE_BLOCK_SIZE,
            source_block_merge_gap: DEFAULT_SOURCE_BLOCK_MERGE_GAP,
            source_get_concurrency: DEFAULT_SOURCE_GET_CONCURRENCY,
            source_window_capacity: DEFAULT_SOURCE_WINDOW_CAPACITY,
            source_window_memory_budget_mb: None,
            body_chunk_size: DEFAULT_BODY_CHUNK_SIZE,
            pipe_capacity: DEFAULT_PIPE_CAPACITY,
        }
    }

    /// Returns the source ZIP object.
    pub fn source(&self) -> &S3Object {
        &self.source
    }

    /// Returns the destination prefix.
    pub fn destination(&self) -> &S3Prefix {
        &self.destination
    }

    /// Returns the destination cleanup policy.
    pub fn cleanup(&self) -> DestinationCleanup {
        self.cleanup
    }

    /// Returns the ZIP entry selection patterns.
    pub fn selection(&self) -> &UnzipSelection {
        &self.selection
    }

    /// Returns whether source scheduler diagnostics are collected.
    pub fn collects_diagnostics(&self) -> bool {
        self.collect_diagnostics
    }

    /// Returns the ZIP entry comparison policy.
    pub fn comparison_mode(&self) -> ComparisonMode {
        self.comparison
    }

    /// Returns the conditional write conflict handling policy.
    pub fn conflict_policy(&self) -> ConflictPolicy {
        self.conflict_policy
    }

    /// Returns whether per-object operation records are collected.
    pub fn collects_operations(&self) -> bool {
        self.collect_operations
    }

    /// Returns the maximum number of ZIP entries processed concurrently.
    pub fn concurrency(&self) -> usize {
        self.concurrency
    }

    /// Returns the maximum number of destination `PutObject` requests in flight.
    pub fn put_concurrency(&self) -> usize {
        self.put_concurrency
    }

    /// Returns the retry and backoff policy for destination `PutObject` attempts.
    pub fn put_retry_policy(&self) -> &PutRetryPolicy {
        &self.put_retry_policy
    }

    /// Returns the maximum size for planned source ZIP blocks.
    pub fn source_block_size(&self) -> usize {
        self.source_block_size
    }

    /// Returns the maximum gap that can be read while coalescing adjacent source spans.
    pub fn source_block_merge_gap(&self) -> usize {
        self.source_block_merge_gap
    }

    /// Returns the maximum number of ranged source `GetObject` requests in flight.
    pub fn source_get_concurrency(&self) -> usize {
        self.source_get_concurrency
    }

    /// Returns the configured source block window capacity.
    ///
    /// When [`Self::with_source_window_memory_budget_mb`] is used, extraction
    /// derives the effective post-manifest value at runtime and reports it in
    /// [`crate::SyncDiagnostics::source_window_capacity`] when diagnostics are collected.
    pub fn source_window_capacity(&self) -> usize {
        self.source_window_capacity
    }

    /// Returns the available memory budget, in MiB, used to derive the source block window.
    pub fn source_window_memory_budget_mb(&self) -> Option<u64> {
        self.source_window_memory_budget_mb
    }

    /// Returns the buffer size used when streaming entry bodies to S3.
    pub fn body_chunk_size(&self) -> usize {
        self.body_chunk_size
    }

    /// Returns the in-memory pipe capacity between decompression and S3 upload.
    pub fn pipe_capacity(&self) -> usize {
        self.pipe_capacity
    }

    /// Sets ZIP entry selection patterns.
    pub fn with_selection(mut self, selection: impl Into<UnzipSelection>) -> Self {
        self.selection = selection.into();
        self
    }

    /// Deletes destination objects under the prefix that are not present in the ZIP.
    ///
    /// This requires a non-empty destination prefix and cannot be combined with
    /// a non-empty selection.
    pub fn delete_extra_objects(mut self) -> Self {
        self.cleanup = DestinationCleanup::DeleteExtra;
        self
    }

    /// Sets the destination cleanup policy.
    pub fn with_cleanup(mut self, cleanup: DestinationCleanup) -> Self {
        self.cleanup = cleanup;
        self
    }

    /// Collects source scheduler diagnostics in the returned report.
    pub fn collect_diagnostics(mut self) -> Self {
        self.collect_diagnostics = true;
        self
    }

    /// Ignores any embedded catalog and hashes ZIP entries for comparison.
    pub fn force_hash_comparison(mut self) -> Self {
        self.comparison = ComparisonMode::HashEntries;
        self
    }

    /// Sets the ZIP entry comparison policy.
    pub fn with_comparison_mode(mut self, comparison: ComparisonMode) -> Self {
        self.comparison = comparison;
        self
    }

    /// Returns an error after the first conditional write conflict is observed.
    pub fn fail_on_conflict(mut self) -> Self {
        self.conflict_policy = ConflictPolicy::FailFast;
        self
    }

    /// Sets the conditional write conflict handling policy.
    pub fn with_conflict_policy(mut self, conflict_policy: ConflictPolicy) -> Self {
        self.conflict_policy = conflict_policy;
        self
    }

    /// Omits per-object operation records from the returned report.
    pub fn without_operations(mut self) -> Self {
        self.collect_operations = false;
        self
    }

    /// Sets the maximum number of ZIP entries processed concurrently.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;
        self
    }

    /// Sets the maximum number of destination `PutObject` requests in flight.
    pub fn with_put_concurrency(mut self, put_concurrency: usize) -> Self {
        self.put_concurrency = put_concurrency;
        self
    }

    /// Sets the retry and backoff policy for destination `PutObject` attempts.
    pub fn with_put_retry_policy(mut self, put_retry_policy: PutRetryPolicy) -> Self {
        self.put_retry_policy = put_retry_policy;
        self
    }

    /// Sets the maximum size for planned source ZIP blocks.
    pub fn with_source_block_size(mut self, source_block_size: usize) -> Self {
        self.source_block_size = source_block_size;
        self
    }

    /// Sets the maximum gap that can be read while coalescing adjacent source spans.
    pub fn with_source_block_merge_gap(mut self, source_block_merge_gap: usize) -> Self {
        self.source_block_merge_gap = source_block_merge_gap;
        self
    }

    /// Sets the maximum number of ranged source `GetObject` requests in flight.
    pub fn with_source_get_concurrency(mut self, source_get_concurrency: usize) -> Self {
        self.source_get_concurrency = source_get_concurrency;
        self
    }

    /// Sets the maximum bytes held by the planned source block window.
    pub fn with_source_window_capacity(mut self, source_window_capacity: usize) -> Self {
        self.source_window_capacity = source_window_capacity;
        self
    }

    /// Sets the available memory budget, in MiB, used to derive the source block window.
    pub fn with_source_window_memory_budget_mb(
        mut self,
        source_window_memory_budget_mb: u64,
    ) -> Self {
        self.source_window_memory_budget_mb = Some(source_window_memory_budget_mb);
        self
    }

    /// Sets the buffer size used when streaming entry bodies to S3.
    pub fn with_body_chunk_size(mut self, body_chunk_size: usize) -> Self {
        self.body_chunk_size = body_chunk_size;
        self
    }

    /// Sets the in-memory pipe capacity between decompression and S3 upload.
    pub fn with_pipe_capacity(mut self, pipe_capacity: usize) -> Self {
        self.pipe_capacity = pipe_capacity;
        self
    }
}

/// Retry and backoff policy for destination `PutObject` attempts.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PutRetryPolicy {
    /// Maximum number of application-level `PutObject` attempts per object.
    ///
    /// Must be greater than zero.
    pub(crate) max_attempts: usize,
    /// Base delay for retryable non-throttling failures.
    pub(crate) base_delay: Duration,
    /// Maximum delay for retryable non-throttling failures.
    ///
    /// Must be greater than or equal to [`Self::base_delay`].
    pub(crate) max_delay: Duration,
    /// Base delay for throttling failures such as S3 `SlowDown`.
    pub(crate) slowdown_base_delay: Duration,
    /// Maximum delay for throttling failures such as S3 `SlowDown`.
    ///
    /// Must be greater than or equal to [`Self::slowdown_base_delay`].
    pub(crate) slowdown_max_delay: Duration,
    /// Jitter mode applied to computed retry delays.
    pub(crate) jitter: RetryJitter,
}

impl Default for PutRetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: PUT_OBJECT_MAX_ATTEMPTS,
            base_delay: Duration::from_millis(PUT_OBJECT_RETRY_BASE_DELAY_MS),
            max_delay: Duration::from_millis(PUT_OBJECT_RETRY_MAX_DELAY_MS),
            slowdown_base_delay: Duration::from_millis(PUT_OBJECT_SLOWDOWN_RETRY_BASE_DELAY_MS),
            slowdown_max_delay: Duration::from_millis(PUT_OBJECT_SLOWDOWN_RETRY_MAX_DELAY_MS),
            jitter: RetryJitter::Full,
        }
    }
}

impl PutRetryPolicy {
    /// Returns the maximum number of application-level `PutObject` attempts.
    pub fn max_attempts(&self) -> usize {
        self.max_attempts
    }

    /// Returns the base delay for retryable non-throttling failures.
    pub fn base_delay(&self) -> Duration {
        self.base_delay
    }

    /// Returns the maximum delay for retryable non-throttling failures.
    pub fn max_delay(&self) -> Duration {
        self.max_delay
    }

    /// Returns the base delay for throttling failures such as S3 `SlowDown`.
    pub fn slowdown_base_delay(&self) -> Duration {
        self.slowdown_base_delay
    }

    /// Returns the maximum delay for throttling failures such as S3 `SlowDown`.
    pub fn slowdown_max_delay(&self) -> Duration {
        self.slowdown_max_delay
    }

    /// Returns the jitter mode applied to computed retry delays.
    pub fn jitter(&self) -> RetryJitter {
        self.jitter
    }

    /// Sets the maximum number of application-level `PutObject` attempts.
    pub fn with_max_attempts(mut self, max_attempts: usize) -> Self {
        self.max_attempts = max_attempts;
        self
    }

    /// Sets the base delay for retryable non-throttling failures.
    pub fn with_base_delay(mut self, base_delay: Duration) -> Self {
        self.base_delay = base_delay;
        self
    }

    /// Sets the maximum delay for retryable non-throttling failures.
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }

    /// Sets the base delay for throttling failures such as S3 `SlowDown`.
    pub fn with_slowdown_base_delay(mut self, slowdown_base_delay: Duration) -> Self {
        self.slowdown_base_delay = slowdown_base_delay;
        self
    }

    /// Sets the maximum delay for throttling failures such as S3 `SlowDown`.
    pub fn with_slowdown_max_delay(mut self, slowdown_max_delay: Duration) -> Self {
        self.slowdown_max_delay = slowdown_max_delay;
        self
    }

    /// Sets the jitter mode applied to computed retry delays.
    pub fn with_jitter(mut self, jitter: RetryJitter) -> Self {
        self.jitter = jitter;
        self
    }
}

/// Jitter mode used for application-level destination `PutObject` retries.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RetryJitter {
    /// Use full jitter, selecting a random delay from zero to the computed cap.
    Full,
    /// Use deterministic exponential delays without jitter.
    None,
}

/// Computes adaptive source `GetObject` concurrency for a fixed memory envelope.
///
/// The policy scales source reads in the same direction as Lambda CPU: one
/// source request per 256 MiB of configured memory, capped at eight.
pub fn adaptive_source_get_concurrency(available_memory_mb: u64) -> usize {
    let slots = available_memory_mb / ADAPTIVE_SOURCE_GET_MEMORY_STEP_MB;
    usize::try_from(slots)
        .unwrap_or(usize::MAX)
        .clamp(1, ADAPTIVE_SOURCE_MAX_GET_CONCURRENCY)
}

/// Inputs for deriving an adaptive source block window capacity.
///
/// The capacity calculation reserves a fixed runtime baseline, a fixed amount
/// per extraction worker, and a fixed amount per ZIP file entry before assigning
/// otherwise idle memory to the source ZIP block window.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AdaptiveSourceWindow {
    /// Available runtime memory, in MiB.
    pub(crate) available_memory_mb: u64,
    /// Size of the source ZIP object, in bytes.
    pub(crate) source_zip_bytes: u64,
    /// Number of regular file entries in the ZIP.
    pub(crate) zip_file_count: usize,
    /// Maximum number of ZIP entries processed concurrently.
    pub(crate) concurrency: usize,
    /// Maximum size for planned source ZIP blocks.
    pub(crate) source_block_size: usize,
    /// Maximum number of ranged source `GetObject` requests in flight.
    pub(crate) source_get_concurrency: usize,
}

impl AdaptiveSourceWindow {
    /// Creates adaptive source window inputs with the crate defaults for scheduler knobs.
    pub fn new(available_memory_mb: u64, source_zip_bytes: u64, zip_file_count: usize) -> Self {
        Self {
            available_memory_mb,
            source_zip_bytes,
            zip_file_count,
            concurrency: DEFAULT_CONCURRENCY,
            source_block_size: DEFAULT_SOURCE_BLOCK_SIZE,
            source_get_concurrency: DEFAULT_SOURCE_GET_CONCURRENCY,
        }
    }

    /// Sets the maximum number of ZIP entries processed concurrently.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;
        self
    }

    /// Sets the maximum size for planned source ZIP blocks.
    pub fn with_source_block_size(mut self, source_block_size: usize) -> Self {
        self.source_block_size = source_block_size;
        self
    }

    /// Sets the maximum number of ranged source `GetObject` requests in flight.
    pub fn with_source_get_concurrency(mut self, source_get_concurrency: usize) -> Self {
        self.source_get_concurrency = source_get_concurrency;
        self
    }

    /// Computes the adaptive source block window capacity.
    pub fn capacity(self) -> usize {
        let Some(available_memory_bytes) = self.available_memory_mb.checked_mul(1024 * 1024) else {
            return usize::try_from(self.source_zip_bytes).unwrap_or(usize::MAX);
        };
        let concurrency = u64::try_from(self.concurrency.max(1)).unwrap_or(u64::MAX);
        let zip_file_count = u64::try_from(self.zip_file_count).unwrap_or(u64::MAX);
        let worker_budget = concurrency.saturating_mul(ADAPTIVE_CACHE_WORKER_OVERHEAD);
        let file_budget = zip_file_count.saturating_mul(ADAPTIVE_CACHE_FILE_OVERHEAD);
        let in_flight_budget = u64::try_from(self.source_get_concurrency.max(1))
            .unwrap_or(u64::MAX)
            .saturating_mul(u64::try_from(self.source_block_size).unwrap_or(u64::MAX));
        let reserved = ADAPTIVE_CACHE_BASE_OVERHEAD
            .saturating_add(worker_budget)
            .saturating_add(file_budget)
            .saturating_add(in_flight_budget);
        let capacity = available_memory_bytes
            .saturating_sub(reserved)
            .min(self.source_zip_bytes);
        let capacity = if capacity > ADAPTIVE_CACHE_LARGE_THRESHOLD {
            capacity.saturating_sub(ADAPTIVE_CACHE_LARGE_RSS_SLACK)
        } else {
            capacity
        }
        .min(ADAPTIVE_CACHE_MAX_WINDOW_CAPACITY);

        let minimum_block_capacity = u64::try_from(self.source_block_size.max(1))
            .unwrap_or(u64::MAX)
            .min(self.source_zip_bytes);
        let capacity = capacity.max(minimum_block_capacity);

        usize::try_from(capacity).unwrap_or(usize::MAX)
    }
}

/// Options for zipping a local directory and uploading it as an S3 object.
#[derive(Clone)]
pub struct UploadOptions {
    /// Local directory whose regular files and empty directories should be included recursively.
    pub(crate) source_dir: PathBuf,
    /// Destination ZIP object.
    pub(crate) destination: S3Object,
    /// Include the embedded update catalog at [`crate::EMBEDDED_CATALOG_PATH`].
    pub(crate) include_catalog: bool,
    /// Compression method for regular file entries.
    pub(crate) compression: ZipCompression,
    /// Buffer size used when streaming the ZIP body to S3.
    ///
    /// Must be greater than zero and no larger than 16 MiB.
    pub(crate) body_chunk_size: usize,
    /// Capacity of the in-memory pipe between ZIP production and S3 upload.
    ///
    /// Must be greater than zero and no larger than 64 MiB.
    pub(crate) pipe_capacity: usize,
    /// Optional progress callback invoked during upload preparation and ZIP streaming.
    pub(crate) progress: Option<UploadProgressHandler>,
}

/// Options for zipping a local directory to a local ZIP file.
#[derive(Clone)]
pub struct LocalZipOptions {
    /// Local directory whose regular files and empty directories should be included recursively.
    pub(crate) source_dir: PathBuf,
    /// Destination ZIP file path.
    pub(crate) destination_zip: PathBuf,
    /// Include the embedded update catalog at [`crate::EMBEDDED_CATALOG_PATH`].
    pub(crate) include_catalog: bool,
    /// Compression method for regular file entries.
    pub(crate) compression: ZipCompression,
    /// Optional progress callback invoked during upload preparation and ZIP streaming.
    pub(crate) progress: Option<UploadProgressHandler>,
}

/// Options for zipping an S3 prefix and uploading it as an S3 ZIP object.
#[derive(Clone)]
pub struct S3PrefixUploadOptions {
    /// Source prefix whose objects should be included recursively.
    pub(crate) source: S3Prefix,
    /// Destination ZIP object.
    pub(crate) destination: S3Object,
    /// Include the embedded update catalog at [`crate::EMBEDDED_CATALOG_PATH`].
    pub(crate) include_catalog: bool,
    /// Compression method for regular file entries.
    pub(crate) compression: ZipCompression,
    /// Buffer size used when streaming the ZIP body to S3.
    ///
    /// Must be greater than zero and no larger than 16 MiB.
    pub(crate) body_chunk_size: usize,
    /// Capacity of the in-memory pipe between ZIP production and S3 upload.
    ///
    /// Must be greater than zero and no larger than 64 MiB.
    pub(crate) pipe_capacity: usize,
    /// Optional progress callback invoked during source listing and ZIP streaming.
    pub(crate) progress: Option<UploadProgressHandler>,
}

/// Options for zipping an S3 prefix to a local ZIP file.
#[derive(Clone)]
pub struct S3PrefixLocalZipOptions {
    /// Source prefix whose objects should be included recursively.
    pub(crate) source: S3Prefix,
    /// Destination ZIP file path.
    pub(crate) destination_zip: PathBuf,
    /// Include the embedded update catalog at [`crate::EMBEDDED_CATALOG_PATH`].
    pub(crate) include_catalog: bool,
    /// Compression method for regular file entries.
    pub(crate) compression: ZipCompression,
    /// Optional progress callback invoked during source listing and ZIP streaming.
    pub(crate) progress: Option<UploadProgressHandler>,
}

/// Options for extracting a local ZIP file into an S3 prefix.
#[derive(Clone)]
pub struct LocalZipSyncOptions {
    /// Source ZIP file path.
    pub(crate) source_zip: PathBuf,
    /// Destination prefix that receives ZIP entries.
    pub(crate) destination: S3Prefix,
    /// How destination objects outside the ZIP are handled.
    ///
    /// Deleting extra objects requires a non-empty destination prefix so a
    /// bucket root is never treated as a sync deletion scope.
    pub(crate) cleanup: DestinationCleanup,
    /// ZIP entry selection. Empty selection extracts every supported entry.
    ///
    /// Selection cannot be combined with [`DestinationCleanup::DeleteExtra`].
    pub(crate) selection: UnzipSelection,
    /// Comparison policy for embedded catalogs and entry hashing.
    pub(crate) comparison: ComparisonMode,
    /// Conditional write conflict handling policy.
    pub(crate) conflict_policy: ConflictPolicy,
    /// Collect one operation record per processed object in the returned report.
    pub(crate) collect_operations: bool,
    /// Maximum number of ZIP entries processed concurrently.
    pub(crate) concurrency: usize,
    /// Buffer size used when streaming entry bodies to S3.
    pub(crate) body_chunk_size: usize,
    /// Capacity of the in-memory pipe between decompression and S3 upload.
    pub(crate) pipe_capacity: usize,
}

/// Options for extracting an S3 ZIP object into a local directory.
#[derive(Clone)]
pub struct S3ZipLocalUnzipOptions {
    /// Source ZIP object.
    pub(crate) source: S3Object,
    /// Destination local directory.
    pub(crate) destination_dir: PathBuf,
    /// ZIP entry selection. Empty selection extracts every supported entry.
    pub(crate) selection: UnzipSelection,
    /// Collect source scheduler diagnostics in the returned report.
    pub(crate) collect_diagnostics: bool,
    /// Comparison policy for embedded catalogs and entry hashing.
    pub(crate) comparison: ComparisonMode,
    /// Collect one operation record per processed entry in the returned report.
    pub(crate) collect_operations: bool,
    /// Maximum number of ZIP entries processed concurrently.
    pub(crate) concurrency: usize,
    /// Maximum size for planned source ZIP blocks.
    pub(crate) source_block_size: usize,
    /// Maximum gap that can be read while coalescing adjacent source spans.
    pub(crate) source_block_merge_gap: usize,
    /// Maximum number of ranged source `GetObject` requests in flight.
    pub(crate) source_get_concurrency: usize,
    /// Maximum bytes held by the planned source block window.
    pub(crate) source_window_capacity: usize,
    /// Available memory budget, in MiB, used to derive the source block window.
    pub(crate) source_window_memory_budget_mb: Option<u64>,
}

/// Options for extracting a local ZIP file into a local directory.
#[derive(Clone)]
pub struct LocalUnzipOptions {
    /// Source ZIP file path.
    pub(crate) source_zip: PathBuf,
    /// Destination local directory.
    pub(crate) destination_dir: PathBuf,
    /// ZIP entry selection. Empty selection extracts every supported entry.
    pub(crate) selection: UnzipSelection,
    /// Comparison policy for embedded catalogs and entry hashing.
    pub(crate) comparison: ComparisonMode,
    /// Collect one operation record per processed entry in the returned report.
    pub(crate) collect_operations: bool,
    /// Maximum number of ZIP entries processed concurrently.
    pub(crate) concurrency: usize,
}

impl S3PrefixUploadOptions {
    /// Creates upload options for an S3 source prefix and destination object.
    pub fn new(source: S3Prefix, destination: S3Object) -> Self {
        Self {
            source,
            destination,
            include_catalog: true,
            compression: ZipCompression::Deflate,
            body_chunk_size: DEFAULT_BODY_CHUNK_SIZE,
            pipe_capacity: DEFAULT_PIPE_CAPACITY,
            progress: None,
        }
    }

    /// Omits the embedded update catalog from the ZIP.
    pub fn without_catalog(mut self) -> Self {
        self.include_catalog = false;
        self
    }

    /// Sets the compression method used for regular file entries.
    pub fn with_compression(mut self, compression: ZipCompression) -> Self {
        self.compression = compression;
        self
    }

    /// Sets the buffer size used when streaming the ZIP body to S3.
    pub fn with_body_chunk_size(mut self, body_chunk_size: usize) -> Self {
        self.body_chunk_size = body_chunk_size;
        self
    }

    /// Sets the in-memory pipe capacity between ZIP production and S3 upload.
    pub fn with_pipe_capacity(mut self, pipe_capacity: usize) -> Self {
        self.pipe_capacity = pipe_capacity;
        self
    }

    /// Sets the progress callback invoked during source listing and ZIP streaming.
    pub fn with_progress(self, callback: impl Fn(UploadProgress) + Send + Sync + 'static) -> Self {
        self.with_progress_handler(UploadProgressHandler::new(callback))
    }

    /// Sets the progress handler invoked during source listing and ZIP streaming.
    pub fn with_progress_handler(mut self, progress: UploadProgressHandler) -> Self {
        self.progress = Some(progress);
        self
    }
}

impl LocalZipOptions {
    /// Creates options for a local source directory and local destination ZIP.
    pub fn new(source_dir: impl Into<PathBuf>, destination_zip: impl Into<PathBuf>) -> Self {
        Self {
            source_dir: source_dir.into(),
            destination_zip: destination_zip.into(),
            include_catalog: true,
            compression: ZipCompression::Deflate,
            progress: None,
        }
    }

    /// Omits the embedded update catalog from the ZIP.
    pub fn without_catalog(mut self) -> Self {
        self.include_catalog = false;
        self
    }

    /// Sets the compression method used for regular file entries.
    pub fn with_compression(mut self, compression: ZipCompression) -> Self {
        self.compression = compression;
        self
    }

    /// Sets the progress callback invoked during upload preparation and ZIP streaming.
    pub fn with_progress(self, callback: impl Fn(UploadProgress) + Send + Sync + 'static) -> Self {
        self.with_progress_handler(UploadProgressHandler::new(callback))
    }

    /// Sets the progress handler invoked during upload preparation and ZIP streaming.
    pub fn with_progress_handler(mut self, progress: UploadProgressHandler) -> Self {
        self.progress = Some(progress);
        self
    }
}

impl std::fmt::Debug for LocalZipOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LocalZipOptions")
            .field("source_dir", &self.source_dir)
            .field("destination_zip", &self.destination_zip)
            .field("include_catalog", &self.include_catalog)
            .field("compression", &self.compression)
            .field(
                "progress",
                &self.progress.as_ref().map(|_| "UploadProgressHandler"),
            )
            .finish()
    }
}

impl std::fmt::Debug for S3PrefixUploadOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("S3PrefixUploadOptions")
            .field("source", &self.source)
            .field("destination", &self.destination)
            .field("include_catalog", &self.include_catalog)
            .field("compression", &self.compression)
            .field("body_chunk_size", &self.body_chunk_size)
            .field("pipe_capacity", &self.pipe_capacity)
            .field(
                "progress",
                &self.progress.as_ref().map(|_| "UploadProgressHandler"),
            )
            .finish()
    }
}

impl S3PrefixLocalZipOptions {
    /// Creates options for an S3 source prefix and local destination ZIP.
    pub fn new(source: S3Prefix, destination_zip: impl Into<PathBuf>) -> Self {
        Self {
            source,
            destination_zip: destination_zip.into(),
            include_catalog: true,
            compression: ZipCompression::Deflate,
            progress: None,
        }
    }

    /// Omits the embedded update catalog from the ZIP.
    pub fn without_catalog(mut self) -> Self {
        self.include_catalog = false;
        self
    }

    /// Sets the compression method used for regular file entries.
    pub fn with_compression(mut self, compression: ZipCompression) -> Self {
        self.compression = compression;
        self
    }

    /// Sets the progress callback invoked during source listing and ZIP streaming.
    pub fn with_progress(self, callback: impl Fn(UploadProgress) + Send + Sync + 'static) -> Self {
        self.with_progress_handler(UploadProgressHandler::new(callback))
    }

    /// Sets the progress handler invoked during source listing and ZIP streaming.
    pub fn with_progress_handler(mut self, progress: UploadProgressHandler) -> Self {
        self.progress = Some(progress);
        self
    }
}

impl std::fmt::Debug for S3PrefixLocalZipOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("S3PrefixLocalZipOptions")
            .field("source", &self.source)
            .field("destination_zip", &self.destination_zip)
            .field("include_catalog", &self.include_catalog)
            .field("compression", &self.compression)
            .field(
                "progress",
                &self.progress.as_ref().map(|_| "UploadProgressHandler"),
            )
            .finish()
    }
}

impl UploadOptions {
    /// Creates upload options for a local source directory and destination object.
    pub fn new(source_dir: impl Into<PathBuf>, destination: S3Object) -> Self {
        Self {
            source_dir: source_dir.into(),
            destination,
            include_catalog: true,
            compression: ZipCompression::Deflate,
            body_chunk_size: DEFAULT_BODY_CHUNK_SIZE,
            pipe_capacity: DEFAULT_PIPE_CAPACITY,
            progress: None,
        }
    }

    /// Omits the embedded update catalog from the ZIP.
    pub fn without_catalog(mut self) -> Self {
        self.include_catalog = false;
        self
    }

    /// Sets the compression method used for regular file entries.
    pub fn with_compression(mut self, compression: ZipCompression) -> Self {
        self.compression = compression;
        self
    }

    /// Sets the buffer size used when streaming the ZIP body to S3.
    pub fn with_body_chunk_size(mut self, body_chunk_size: usize) -> Self {
        self.body_chunk_size = body_chunk_size;
        self
    }

    /// Sets the in-memory pipe capacity between ZIP production and S3 upload.
    pub fn with_pipe_capacity(mut self, pipe_capacity: usize) -> Self {
        self.pipe_capacity = pipe_capacity;
        self
    }

    /// Sets the progress callback invoked during upload preparation and ZIP streaming.
    pub fn with_progress(self, callback: impl Fn(UploadProgress) + Send + Sync + 'static) -> Self {
        self.with_progress_handler(UploadProgressHandler::new(callback))
    }

    /// Sets the progress handler invoked during upload preparation and ZIP streaming.
    pub fn with_progress_handler(mut self, progress: UploadProgressHandler) -> Self {
        self.progress = Some(progress);
        self
    }
}

impl LocalZipSyncOptions {
    /// Creates extract options for a local source ZIP file and destination prefix.
    pub fn new(source_zip: impl Into<PathBuf>, destination: S3Prefix) -> Self {
        Self {
            source_zip: source_zip.into(),
            destination,
            cleanup: DestinationCleanup::default(),
            selection: UnzipSelection::default(),
            comparison: ComparisonMode::default(),
            conflict_policy: ConflictPolicy::default(),
            collect_operations: true,
            concurrency: DEFAULT_CONCURRENCY,
            body_chunk_size: DEFAULT_BODY_CHUNK_SIZE,
            pipe_capacity: DEFAULT_PIPE_CAPACITY,
        }
    }

    /// Sets ZIP entry selection patterns.
    pub fn with_selection(mut self, selection: impl Into<UnzipSelection>) -> Self {
        self.selection = selection.into();
        self
    }

    /// Deletes destination objects under the prefix that are not present in the ZIP.
    ///
    /// This requires a non-empty destination prefix and cannot be combined with
    /// a non-empty selection.
    pub fn delete_extra_objects(mut self) -> Self {
        self.cleanup = DestinationCleanup::DeleteExtra;
        self
    }

    /// Sets the destination cleanup policy.
    pub fn with_cleanup(mut self, cleanup: DestinationCleanup) -> Self {
        self.cleanup = cleanup;
        self
    }

    /// Ignores any embedded catalog and hashes ZIP entries for comparison.
    pub fn force_hash_comparison(mut self) -> Self {
        self.comparison = ComparisonMode::HashEntries;
        self
    }

    /// Sets the ZIP entry comparison policy.
    pub fn with_comparison_mode(mut self, comparison: ComparisonMode) -> Self {
        self.comparison = comparison;
        self
    }

    /// Returns the conditional write conflict handling policy.
    pub fn conflict_policy(&self) -> ConflictPolicy {
        self.conflict_policy
    }

    /// Returns an error after the first conditional write conflict is observed.
    pub fn fail_on_conflict(mut self) -> Self {
        self.conflict_policy = ConflictPolicy::FailFast;
        self
    }

    /// Sets the conditional write conflict handling policy.
    pub fn with_conflict_policy(mut self, conflict_policy: ConflictPolicy) -> Self {
        self.conflict_policy = conflict_policy;
        self
    }

    /// Omits per-object operation records from the returned report.
    pub fn without_operations(mut self) -> Self {
        self.collect_operations = false;
        self
    }

    /// Sets the maximum number of ZIP entries processed concurrently.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;
        self
    }

    /// Sets the buffer size used when streaming entry bodies to S3.
    pub fn with_body_chunk_size(mut self, body_chunk_size: usize) -> Self {
        self.body_chunk_size = body_chunk_size;
        self
    }

    /// Sets the in-memory pipe capacity between decompression and S3 upload.
    pub fn with_pipe_capacity(mut self, pipe_capacity: usize) -> Self {
        self.pipe_capacity = pipe_capacity;
        self
    }
}

impl std::fmt::Debug for LocalZipSyncOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LocalZipSyncOptions")
            .field("source_zip", &self.source_zip)
            .field("destination", &self.destination)
            .field("cleanup", &self.cleanup)
            .field("selection", &self.selection)
            .field("comparison", &self.comparison)
            .field("conflict_policy", &self.conflict_policy)
            .field("collect_operations", &self.collect_operations)
            .field("concurrency", &self.concurrency)
            .field("body_chunk_size", &self.body_chunk_size)
            .field("pipe_capacity", &self.pipe_capacity)
            .finish()
    }
}

impl S3ZipLocalUnzipOptions {
    /// Creates extract options for a source ZIP object and local destination directory.
    pub fn new(source: S3Object, destination_dir: impl Into<PathBuf>) -> Self {
        Self {
            source,
            destination_dir: destination_dir.into(),
            selection: UnzipSelection::default(),
            collect_diagnostics: false,
            comparison: ComparisonMode::default(),
            collect_operations: true,
            concurrency: DEFAULT_CONCURRENCY,
            source_block_size: DEFAULT_SOURCE_BLOCK_SIZE,
            source_block_merge_gap: DEFAULT_SOURCE_BLOCK_MERGE_GAP,
            source_get_concurrency: DEFAULT_SOURCE_GET_CONCURRENCY,
            source_window_capacity: DEFAULT_SOURCE_WINDOW_CAPACITY,
            source_window_memory_budget_mb: None,
        }
    }

    /// Sets ZIP entry selection patterns.
    pub fn with_selection(mut self, selection: impl Into<UnzipSelection>) -> Self {
        self.selection = selection.into();
        self
    }

    /// Collects source scheduler diagnostics in the returned report.
    pub fn collect_diagnostics(mut self) -> Self {
        self.collect_diagnostics = true;
        self
    }

    /// Ignores any embedded catalog and hashes ZIP entries for comparison.
    pub fn force_hash_comparison(mut self) -> Self {
        self.comparison = ComparisonMode::HashEntries;
        self
    }

    /// Sets the ZIP entry comparison policy.
    pub fn with_comparison_mode(mut self, comparison: ComparisonMode) -> Self {
        self.comparison = comparison;
        self
    }

    /// Omits per-entry operation records from the returned report.
    pub fn without_operations(mut self) -> Self {
        self.collect_operations = false;
        self
    }

    /// Sets the maximum number of ZIP entries processed concurrently.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;
        self
    }

    /// Sets the maximum size for planned source ZIP blocks.
    pub fn with_source_block_size(mut self, source_block_size: usize) -> Self {
        self.source_block_size = source_block_size;
        self
    }

    /// Sets the maximum gap that can be read while coalescing adjacent source spans.
    pub fn with_source_block_merge_gap(mut self, source_block_merge_gap: usize) -> Self {
        self.source_block_merge_gap = source_block_merge_gap;
        self
    }

    /// Sets the maximum number of ranged source `GetObject` requests in flight.
    pub fn with_source_get_concurrency(mut self, source_get_concurrency: usize) -> Self {
        self.source_get_concurrency = source_get_concurrency;
        self
    }

    /// Sets the maximum bytes held by the planned source block window.
    pub fn with_source_window_capacity(mut self, source_window_capacity: usize) -> Self {
        self.source_window_capacity = source_window_capacity;
        self
    }

    /// Sets the available memory budget, in MiB, used to derive the source block window.
    pub fn with_source_window_memory_budget_mb(
        mut self,
        source_window_memory_budget_mb: u64,
    ) -> Self {
        self.source_window_memory_budget_mb = Some(source_window_memory_budget_mb);
        self
    }
}

impl std::fmt::Debug for S3ZipLocalUnzipOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("S3ZipLocalUnzipOptions")
            .field("source", &self.source)
            .field("destination_dir", &self.destination_dir)
            .field("selection", &self.selection)
            .field("collect_diagnostics", &self.collect_diagnostics)
            .field("comparison", &self.comparison)
            .field("collect_operations", &self.collect_operations)
            .field("concurrency", &self.concurrency)
            .field("source_block_size", &self.source_block_size)
            .field("source_block_merge_gap", &self.source_block_merge_gap)
            .field("source_get_concurrency", &self.source_get_concurrency)
            .field("source_window_capacity", &self.source_window_capacity)
            .field(
                "source_window_memory_budget_mb",
                &self.source_window_memory_budget_mb,
            )
            .finish()
    }
}

impl LocalUnzipOptions {
    /// Creates extract options for a source ZIP file and local destination directory.
    pub fn new(source_zip: impl Into<PathBuf>, destination_dir: impl Into<PathBuf>) -> Self {
        Self {
            source_zip: source_zip.into(),
            destination_dir: destination_dir.into(),
            selection: UnzipSelection::default(),
            comparison: ComparisonMode::default(),
            collect_operations: true,
            concurrency: DEFAULT_CONCURRENCY,
        }
    }

    /// Sets ZIP entry selection patterns.
    pub fn with_selection(mut self, selection: impl Into<UnzipSelection>) -> Self {
        self.selection = selection.into();
        self
    }

    /// Ignores any embedded catalog and hashes ZIP entries for comparison.
    pub fn force_hash_comparison(mut self) -> Self {
        self.comparison = ComparisonMode::HashEntries;
        self
    }

    /// Sets the ZIP entry comparison policy.
    pub fn with_comparison_mode(mut self, comparison: ComparisonMode) -> Self {
        self.comparison = comparison;
        self
    }

    /// Omits per-entry operation records from the returned report.
    pub fn without_operations(mut self) -> Self {
        self.collect_operations = false;
        self
    }

    /// Sets the maximum number of ZIP entries processed concurrently.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;
        self
    }
}

impl std::fmt::Debug for LocalUnzipOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("LocalUnzipOptions")
            .field("source_zip", &self.source_zip)
            .field("destination_dir", &self.destination_dir)
            .field("selection", &self.selection)
            .field("comparison", &self.comparison)
            .field("collect_operations", &self.collect_operations)
            .field("concurrency", &self.concurrency)
            .finish()
    }
}

impl std::fmt::Debug for UploadOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("UploadOptions")
            .field("source_dir", &self.source_dir)
            .field("destination", &self.destination)
            .field("include_catalog", &self.include_catalog)
            .field("compression", &self.compression)
            .field("body_chunk_size", &self.body_chunk_size)
            .field("pipe_capacity", &self.pipe_capacity)
            .field(
                "progress",
                &self.progress.as_ref().map(|_| "UploadProgressHandler"),
            )
            .finish()
    }
}

/// Upload progress callback wrapper.
///
/// The callback is invoked synchronously from the upload task whenever progress
/// state changes. Keep the callback lightweight; hand work off to another task
/// if it needs to perform I/O.
#[derive(Clone)]
pub struct UploadProgressHandler {
    callback: Arc<dyn Fn(UploadProgress) + Send + Sync + 'static>,
}

impl UploadProgressHandler {
    /// Creates an upload progress handler from a callback.
    pub fn new(callback: impl Fn(UploadProgress) + Send + Sync + 'static) -> Self {
        Self {
            callback: Arc::new(callback),
        }
    }

    pub(crate) fn emit(&self, progress: UploadProgress) {
        (self.callback)(progress);
    }
}

impl std::fmt::Debug for UploadProgressHandler {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("UploadProgressHandler")
    }
}

/// Progress event emitted while preparing and streaming an upload ZIP.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum UploadProgress {
    /// The source has been scanned and the total entry count is known.
    Planned {
        /// Total number of files and preserved directory entries included in the ZIP.
        total_files: usize,
        /// Total uncompressed bytes across all files.
        total_bytes: u64,
    },
    /// A file or preserved directory entry has started streaming into the ZIP writer.
    FileStarted {
        /// One-based index of the entry currently being streamed.
        current_file: usize,
        /// Total number of files and preserved directory entries included in the ZIP.
        total_files: usize,
        /// Number of entries that have finished streaming into the ZIP.
        processed_files: usize,
        /// Uncompressed bytes that have finished streaming into the ZIP.
        processed_bytes: u64,
        /// Total uncompressed bytes across all files.
        total_bytes: u64,
        /// ZIP path of the entry that just started.
        path: String,
    },
    /// A file is still streaming and byte progress has advanced.
    FileProgress {
        /// One-based index of the entry currently being streamed.
        current_file: usize,
        /// Total number of files and preserved directory entries included in the ZIP.
        total_files: usize,
        /// Number of entries that have finished streaming into the ZIP.
        processed_files: usize,
        /// Uncompressed bytes that have streamed into the ZIP producer so far.
        processed_bytes: u64,
        /// Total uncompressed bytes across all files.
        total_bytes: u64,
        /// ZIP path of the file currently being streamed.
        path: String,
    },
    /// One file or preserved directory entry has finished streaming into the ZIP writer.
    FileFinished {
        /// Number of entries that have finished streaming into the ZIP.
        processed_files: usize,
        /// Total number of files and preserved directory entries included in the ZIP.
        total_files: usize,
        /// Uncompressed bytes that have finished streaming into the ZIP.
        processed_bytes: u64,
        /// Total uncompressed bytes across all files.
        total_bytes: u64,
        /// ZIP path of the entry that just finished.
        path: String,
    },
    /// ZIP production has finished writing into the upload pipe.
    ///
    /// S3 multipart upload completion may still be in progress when this event
    /// is emitted.
    Finished {
        /// Total number of files and preserved directory entries included in the ZIP.
        total_files: usize,
        /// Total uncompressed bytes across all files.
        total_bytes: u64,
    },
}