wsb 0.155.49

Workspace - A tool suite for file operations, version management, and development workflow automation
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
use anyhow::{Context, Result};
use content_inspector::{inspect, ContentType};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

/// Detects if a file is binary or text
pub struct BinaryDetector {
    /// Maximum number of bytes to read for detection
    max_sample_size: usize,
    /// Threshold for binary detection (percentage of non-printable characters)
    binary_threshold: f64,
}

impl Default for BinaryDetector {
    fn default() -> Self {
        Self {
            max_sample_size: 8192, // 8KB sample
            binary_threshold: 0.3, // 30% non-printable = binary
        }
    }
}

impl BinaryDetector {
    pub fn new(max_sample_size: usize, binary_threshold: f64) -> Self {
        Self {
            max_sample_size,
            binary_threshold,
        }
    }

    /// Check if a file is binary using multiple detection methods with extension fail-safe
    pub fn is_binary<P: AsRef<Path>>(&self, path: P) -> Result<bool> {
        let path = path.as_ref();

        // First, check file extension for known binary types (fail-safe for important files)
        if self.is_binary_by_extension(path) {
            return Ok(true);
        }

        // Check for binary signatures early (before content_inspector which can be fooled by compressed data)
        if let Ok(is_binary_by_signature) = self.check_binary_signature_only(path) {
            if is_binary_by_signature {
                return Ok(true);
            }
        }

        // Check using content_inspector crate (fast method)
        if let Ok(content_type) = self.detect_by_content_inspector(path) {
            match content_type {
                ContentType::BINARY => return Ok(true),
                ContentType::UTF_8
                | ContentType::UTF_8_BOM
                | ContentType::UTF_16LE
                | ContentType::UTF_16BE
                | ContentType::UTF_32LE
                | ContentType::UTF_32BE => return Ok(false),
            }
        }

        // Fallback to manual analysis with enhanced binary signatures
        self.is_binary_by_content_analysis(path)
    }

    /// Check if file is likely binary based on file extension (fail-safe)
    fn is_binary_by_extension(&self, path: &Path) -> bool {
        if let Some(extension) = path.extension() {
            if let Some(ext_str) = extension.to_str() {
                let ext_lower = ext_str.to_lowercase();
                return BINARY_EXTENSIONS.contains(&ext_lower.as_str());
            }
        }
        false
    }

    /// Check only for binary signatures (used before content_inspector to catch compressed data)
    fn check_binary_signature_only(&self, path: &Path) -> Result<bool> {
        let file = File::open(path).with_context(|| {
            format!(
                "Failed to open file for signature check: {}",
                path.display()
            )
        })?;

        let mut reader = BufReader::new(file);
        let mut buffer = vec![0; 32]; // Only need first 32 bytes for signatures

        let bytes_read = reader.read(&mut buffer).with_context(|| {
            format!(
                "Failed to read file for signature check: {}",
                path.display()
            )
        })?;

        if bytes_read == 0 {
            return Ok(false); // Empty files are treated as text
        }

        buffer.truncate(bytes_read);
        Ok(self.has_binary_signature(&buffer))
    }

    /// Use content_inspector crate for fast detection
    fn detect_by_content_inspector(&self, path: &Path) -> Result<ContentType> {
        let mut file = File::open(path).with_context(|| {
            format!(
                "Failed to open file for binary detection: {}",
                path.display()
            )
        })?;

        let mut buffer = vec![0; self.max_sample_size];
        let bytes_read = file.read(&mut buffer).with_context(|| {
            format!(
                "Failed to read file for binary detection: {}",
                path.display()
            )
        })?;

        buffer.truncate(bytes_read);
        Ok(inspect(&buffer))
    }

    /// Manual content analysis for edge cases
    fn is_binary_by_content_analysis(&self, path: &Path) -> Result<bool> {
        let file = File::open(path).with_context(|| {
            format!(
                "Failed to open file for content analysis: {}",
                path.display()
            )
        })?;

        let mut reader = BufReader::new(file);
        let mut buffer = vec![0; self.max_sample_size];

        let bytes_read = reader.read(&mut buffer).with_context(|| {
            format!(
                "Failed to read file for content analysis: {}",
                path.display()
            )
        })?;

        if bytes_read == 0 {
            return Ok(false); // Empty files are treated as text
        }

        buffer.truncate(bytes_read);

        // Check for common binary file signatures
        if self.has_binary_signature(&buffer) {
            return Ok(true);
        }

        // Check for null bytes (strong indicator of binary)
        if buffer.contains(&0) {
            return Ok(true);
        }

        // Try to validate as UTF-8 first
        if let Ok(text) = std::str::from_utf8(&buffer) {
            // If it's valid UTF-8, count actual non-printable characters (excluding valid Unicode)
            let non_printable_count = text
                .chars()
                .filter(|&c| c.is_control() && !matches!(c, '\t' | '\n' | '\r'))
                .count();
            let non_printable_ratio = non_printable_count as f64 / text.chars().count() as f64;
            return Ok(non_printable_ratio > self.binary_threshold);
        }

        // If not valid UTF-8, count non-printable bytes (old method)
        let non_printable_count = buffer
            .iter()
            .filter(|&&byte| !is_printable_ascii(byte) && !is_valid_utf8_start(byte))
            .count();

        let non_printable_ratio = non_printable_count as f64 / bytes_read as f64;
        Ok(non_printable_ratio > self.binary_threshold)
    }

    /// Check for common binary file signatures at the beginning of the file
    fn has_binary_signature(&self, buffer: &[u8]) -> bool {
        if buffer.len() < 2 {
            return false;
        }

        // Check for Protocol Buffer patterns first (they can be tricky)
        if self.is_protocol_buffer(buffer) {
            return true;
        }

        // Check for common binary signatures
        match &buffer[0..2] {
            // zlib compression (used by Git objects)
            [0x78, 0x01] | [0x78, 0x9c] | [0x78, 0xda] => true,
            // PNG
            [0x89, 0x50] if buffer.len() >= 8 && &buffer[0..8] == b"\x89PNG\r\n\x1a\n" => true,
            // JPEG
            [0xff, 0xd8] => true,
            // PDF
            [0x25, 0x50] if buffer.len() >= 4 && &buffer[0..4] == b"%PDF" => true,
            // ZIP/JAR/etc
            [0x50, 0x4b] => true,
            // ELF executable
            [0x7f, 0x45] if buffer.len() >= 4 && &buffer[0..4] == b"\x7fELF" => true,
            // Windows PE/COFF executable
            [0x4d, 0x5a] => true,
            // Mach-O executable (macOS)
            [0xfe, 0xed] | [0xfe, 0xec] | [0xce, 0xfa] | [0xcf, 0xfa] => true,
            // TAR
            _ if buffer.len() >= 262 && &buffer[257..262] == b"ustar" => true,
            // GZIP
            [0x1f, 0x8b] => true,
            // BZIP2
            [0x42, 0x5a] if buffer.len() >= 3 && buffer[2] == 0x68 => true,
            // XZ
            [0xfd, 0x37] if buffer.len() >= 6 && &buffer[0..6] == b"\xfd7zXZ\x00" => true,
            // 7-Zip
            [0x37, 0x7a] if buffer.len() >= 6 && &buffer[0..6] == b"7z\xbc\xaf\x27\x1c" => true,
            // Class files (Java)
            [0xca, 0xfe] => true,
            // DEX files (Android)
            [0x64, 0x65] if buffer.len() >= 8 && &buffer[0..8] == b"dex\n035\0" => true,
            _ => false,
        }
    }

    /// Detect Protocol Buffer files by their characteristic patterns
    fn is_protocol_buffer(&self, buffer: &[u8]) -> bool {
        if buffer.len() < 8 {
            return false;
        }

        // First, reject if it looks like text (contains mostly printable ASCII or valid UTF-8)
        let ascii_count = buffer
            .iter()
            .take(64)
            .filter(|&&b| b >= 0x20 && b <= 0x7E)
            .count();
        let ascii_ratio = ascii_count as f64 / buffer.len().min(64) as f64;

        // Also check if it's valid UTF-8 (which would indicate text, not binary protobuf)
        let is_valid_utf8 = std::str::from_utf8(buffer).is_ok();

        if ascii_ratio > 0.7 || is_valid_utf8 {
            return false; // Too much readable text or valid UTF-8, unlikely to be protobuf
        }

        // Protocol Buffers use varint encoding and have specific wire type patterns
        let mut valid_proto_sequences = 0;
        let mut i = 0;

        while i < buffer.len().min(32) {
            // Check first 32 bytes for protobuf patterns
            let byte = buffer[i];

            // Check for valid protobuf wire type patterns
            let wire_type = byte & 0x07;
            let field_number = byte >> 3;

            // Valid wire types: 0 (varint), 1 (64-bit), 2 (length-delimited), 5 (32-bit)
            if matches!(wire_type, 0 | 1 | 2 | 5) && field_number > 0 && field_number < 32 {
                match wire_type {
                    0 => {
                        // Varint - check for reasonable varint values
                        if i + 1 < buffer.len() {
                            let next = buffer[i + 1];
                            if next < 0x80 || (next & 0x80 != 0 && next & 0x7F < 0x40) {
                                valid_proto_sequences += 1;
                                i += 2; // Skip the varint value
                                continue;
                            }
                        }
                    }
                    2 => {
                        // Length-delimited - check for reasonable length
                        if i + 1 < buffer.len() {
                            let length = buffer[i + 1];
                            if length > 0
                                && length < 64
                                && (i + 2 + length as usize) <= buffer.len()
                            {
                                valid_proto_sequences += 2; // Length-delimited is a strong indicator
                                i += 2 + length as usize; // Skip the entire field
                                continue;
                            }
                        }
                    }
                    1 | 5 => {
                        // Fixed-width fields
                        valid_proto_sequences += 1;
                        i += if wire_type == 1 { 9 } else { 5 }; // Skip field + data
                        continue;
                    }
                    _ => {}
                }
            }

            i += 1;
        }

        // Require at least 2 valid protobuf sequences and check for non-text characteristics
        let has_null_bytes = buffer[..buffer.len().min(64)].contains(&0);
        let non_ascii_count = buffer
            .iter()
            .take(64)
            .filter(|&&b| b > 0x7E || (b < 0x20 && !matches!(b, 0x09 | 0x0A | 0x0D)))
            .count();
        let non_ascii_ratio = non_ascii_count as f64 / buffer.len().min(64) as f64;

        // Must have valid protobuf patterns AND binary characteristics
        valid_proto_sequences >= 2 && (has_null_bytes || non_ascii_ratio > 0.2)
    }

    /// Check if the file appears to be a text file that we should process
    pub fn is_text_file<P: AsRef<Path>>(&self, path: P) -> Result<bool> {
        Ok(!self.is_binary(path)?)
    }

    /// Get a description of why a file is considered binary
    pub fn get_binary_reason<P: AsRef<Path>>(&self, path: P) -> Result<Option<String>> {
        let path = path.as_ref();

        if self.is_binary_by_extension(path) {
            return Ok(Some("Binary file extension".to_string()));
        }

        if let Ok(content_type) = self.detect_by_content_inspector(path) {
            match content_type {
                ContentType::BINARY => {
                    return Ok(Some("Content inspection detected binary".to_string()))
                }
                _ => {}
            }
        }

        let file = File::open(path)?;
        let mut reader = BufReader::new(file);
        let mut buffer = vec![0; self.max_sample_size];
        let bytes_read = reader.read(&mut buffer)?;

        if bytes_read == 0 {
            return Ok(None);
        }

        buffer.truncate(bytes_read);

        // Check for binary signatures first
        if self.has_binary_signature(&buffer) {
            // Check for Protocol Buffer first since it doesn't have a fixed signature
            if self.is_protocol_buffer(&buffer) {
                return Ok(Some("Detected as Protocol Buffer binary data".to_string()));
            }

            let signature_desc = match &buffer[0..2.min(buffer.len())] {
                [0x78, 0x01] | [0x78, 0x9c] | [0x78, 0xda] => "zlib compressed data",
                [0x89, 0x50] => "PNG image",
                [0xff, 0xd8] => "JPEG image",
                [0x25, 0x50] => "PDF document",
                [0x50, 0x4b] => "ZIP archive",
                [0x7f, 0x45] => "ELF executable",
                [0x4d, 0x5a] => "Windows executable",
                [0xfe, 0xed] | [0xfe, 0xec] | [0xce, 0xfa] | [0xcf, 0xfa] => "Mach-O executable",
                [0x1f, 0x8b] => "GZIP compressed",
                [0x42, 0x5a] => "BZIP2 compressed",
                _ => "binary file signature",
            };
            return Ok(Some(format!("Detected as {}", signature_desc)));
        }

        if buffer.contains(&0) {
            return Ok(Some("Contains null bytes".to_string()));
        }

        let non_printable_count = buffer
            .iter()
            .filter(|&&byte| !is_printable_ascii(byte) && !is_valid_utf8_start(byte))
            .count();

        let non_printable_ratio = non_printable_count as f64 / bytes_read as f64;
        if non_printable_ratio > self.binary_threshold {
            return Ok(Some(format!(
                "High ratio of non-printable characters: {:.1}%",
                non_printable_ratio * 100.0
            )));
        }

        Ok(None)
    }
}

/// Check if a byte is printable ASCII
fn is_printable_ascii(byte: u8) -> bool {
    matches!(byte, 0x20..=0x7E | 0x09 | 0x0A | 0x0D) // printable ASCII + tab, newline, carriage return
}

/// Check if a byte could be the start of a valid UTF-8 sequence
fn is_valid_utf8_start(byte: u8) -> bool {
    // UTF-8 start bytes: 0xxxxxxx, 110xxxxx, 1110xxxx, 11110xxx
    byte < 0x80 || (byte >= 0xC0 && byte < 0xF8)
}

/// Comprehensive binary file extensions for diverse workspaces
const BINARY_EXTENSIONS: &[&str] = &[
    // Executables and system files
    "exe",
    "dll",
    "so",
    "dylib",
    "app",
    "deb",
    "rpm",
    "msi",
    "dmg",
    "run",
    "bin",
    "com",
    "bat",
    "cmd",
    "scr",
    "sys",
    "drv",
    "cpl",
    "ocx",
    "axl",
    "elf",
    // Archives and compressed files
    "zip",
    "tar",
    "gz",
    "bz2",
    "xz",
    "7z",
    "rar",
    "cab",
    "ace",
    "arc",
    "arj",
    "lzh",
    "lha",
    "zoo",
    "cpio",
    "shar",
    "lz",
    "lzma",
    "tgz",
    "tbz",
    "tbz2",
    "txz",
    "tlz",
    "Z",
    "deb",
    "rpm",
    "pkg",
    "apk",
    "snap",
    "flatpak",
    "appimage",
    "pb",
    "pbf",
    // Images and graphics
    "jpg",
    "jpeg",
    "png",
    "gif",
    "bmp",
    "tiff",
    "tif",
    "webp",
    "svg",
    "ico",
    "cur",
    "psd",
    "ai",
    "eps",
    "raw",
    "cr2",
    "nef",
    "orf",
    "sr2",
    "dng",
    "exr",
    "hdr",
    "tga",
    "pcx",
    "ppm",
    "pgm",
    "pbm",
    "xbm",
    "xpm",
    "jp2",
    "jpx",
    "j2k",
    "heic",
    "heif",
    "avif",
    "jxl",
    "wbmp",
    "emf",
    "wmf",
    "cgm",
    "pic",
    "pict",
    // Videos and multimedia
    "mp4",
    "avi",
    "mkv",
    "mov",
    "wmv",
    "flv",
    "webm",
    "m4v",
    "3gp",
    "3g2",
    "mpg",
    "mpeg",
    "asf",
    "rm",
    "rmvb",
    "vob",
    "mts",
    "m2ts",
    "f4v",
    "ogv",
    "divx",
    "xvid",
    "mxf",
    "amv",
    "drc",
    "gif",
    "gifv",
    "mng",
    "qt",
    "yuv",
    "roq",
    "svi",
    "viv",
    "vp8",
    "vp9",
    "av1",
    // Audio files
    "mp3",
    "wav",
    "flac",
    "aac",
    "ogg",
    "m4a",
    "wma",
    "aiff",
    "au",
    "ra",
    "opus",
    "ape",
    "ac3",
    "dts",
    "amr",
    "awb",
    "gsm",
    "m4r",
    "mpc",
    "tta",
    "wv",
    "8svx",
    "cda",
    "mid",
    "midi",
    "rmi",
    "kar",
    "mod",
    "s3m",
    "xm",
    "it",
    "669",
    "amf",
    "ams",
    "dbm",
    "dmf",
    "dsm",
    "far",
    "mdl",
    "med",
    "mtm",
    "okt",
    "ptm",
    "stm",
    "ult",
    "umx",
    "mt2",
    "psm",
    // Documents and office files
    "pdf",
    "doc",
    "docx",
    "xls",
    "xlsx",
    "ppt",
    "pptx",
    "odt",
    "ods",
    "odp",
    "rtf",
    "pages",
    "numbers",
    "key",
    "pub",
    "vsd",
    "vsdx",
    "mpp",
    "one",
    "wps",
    "wpd",
    "sxw",
    "sxc",
    "sxi",
    "sxd",
    "stw",
    "stc",
    "sti",
    "std",
    "odf",
    "otg",
    "otp",
    "ots",
    "ott",
    "odm",
    "oth",
    "odb",
    "oxt",
    // Databases (binary formats only - .sql is text)
    "db",
    "sqlite",
    "sqlite3",
    "mdb",
    "accdb",
    "dbf",
    "fdb",
    "gdb",
    "nsf",
    "odb",
    "pdb",
    "wdb",
    "bak",
    "dmp",
    "frm",
    "myd",
    "myi",
    "ibd",
    "fpt",
    "cdx",
    "idx",
    "ndx",
    "mdx",
    "fox",
    "prg",
    "fxp",
    "dbc",
    "dct",
    // Development and build artifacts
    "o",
    "obj",
    "lib",
    "a",
    "pdb",
    "ilk",
    "exp",
    "map",
    "res",
    "rc",
    "tlb",
    "manifest",
    "cache",
    "tmp",
    "temp",
    "bak",
    "swp",
    "swo",
    "orig",
    "rej",
    // Java ecosystem
    "class",
    "jar",
    "war",
    "ear",
    "jmod",
    "jimage",
    "hprof",
    "heap",
    "jfr",
    // .NET ecosystem
    "pdb",
    "mdb",
    "nupkg",
    "snupkg",
    "vsix",
    "msi",
    "msm",
    "msp",
    "mst",
    "wixlib",
    "wixobj",
    "wixpdb",
    "cab",
    "msu",
    "exe",
    "dll",
    "winmd",
    // Serialization
    "avro",
    "parquet",
    "orc",
    "arrow",
    "cbor",
    "msgpack",
    "bson",
    "pickle",
    "pkl",
    "p",
    "joblib",
    "npy",
    "npz",
    // Virtual machines and containers
    "iso",
    "img",
    "vdi",
    "vmdk",
    "qcow2",
    "vhd",
    "vhdx",
    "hdd",
    "parallels",
    "pvm",
    "ova",
    "ovf",
    "box",
    "vagrant",
    "docker",
    "tar",
    "tgz",
    // Game and 3D files
    "pak",
    "wad",
    "vpk",
    "gcf",
    "ncf",
    "xnb",
    "unity3d",
    "unitypackage",
    "fbx",
    "dae",
    "obj",
    "3ds",
    "max",
    "blend",
    "ma",
    "mb",
    "c4d",
    "lwo",
    "lws",
    "mesh",
    "x3d",
    "ply",
    "stl",
    "off",
    "ase",
    "mdl",
    "md2",
    "md3",
    // CAD and engineering
    "dwg",
    "dxf",
    "step",
    "stp",
    "iges",
    "igs",
    "catpart",
    "catproduct",
    "prt",
    "asm",
    "par",
    "psm",
    "x_t",
    "x_b",
    "sat",
    "3dm",
    "rvt",
    "rfa",
    "ifc",
    "skp",
    "kmz",
    "kml",
    // Scientific and data files
    "hdf5",
    "h5",
    "nc",
    "cdf",
    "fits",
    "fts",
    "mat",
    "sav",
    "dta",
    "por",
    "sas7bdat",
    "xpt",
    "rda",
    "rds",
    "feather",
    "arrow",
    "zarr",
    "blosc",
    // Fonts
    "ttf",
    "otf",
    "woff",
    "woff2",
    "eot",
    "fon",
    "fnt",
    "pfb",
    "pfm",
    "afm",
    "bdf",
    "pcf",
    "snf",
    "gsf",
    "t1",
    "t42",
    "cff",
    "cid",
    "dfont",
    "suit",
    // Blockchain and crypto
    "dat",
    "blk",
    "rev",
    "wallet",
    "keystore",
    "p12",
    "pfx",
    "jks",
    "bks",
    // Mobile and embedded
    "apk",
    "ipa",
    "xap",
    "appx",
    "msix",
    "aab",
    "dex",
    "odex",
    "vdex",
    "art",
    "oat",
    "hex",
    "bin",
    "elf",
    "axf",
    "out",
    "uf2",
    "hex",
    // Others and miscellaneous
    "bin",
    "dat",
    "dump",
    "raw",
    "rom",
    "firmware",
    "bios",
    "efi",
    "uefi",
    "swap",
    "hiberfil",
    "pagefile",
    "core",
    "crashdump",
    "mdmp",
    "dmp",
    "lock",
    "pid",
    "sock",
    "fifo",
    "pipe",
    "device",
    "special",
];

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::TempDir;

    #[test]
    fn test_binary_extension_detection() {
        let detector = BinaryDetector::default();

        // Test binary extensions
        assert!(detector.is_binary_by_extension(Path::new("test.exe")));
        assert!(detector.is_binary_by_extension(Path::new("test.jpg")));
        assert!(detector.is_binary_by_extension(Path::new("test.pdf")));
        assert!(detector.is_binary_by_extension(Path::new("TEST.EXE"))); // case insensitive

        // Test text extensions
        assert!(!detector.is_binary_by_extension(Path::new("test.txt")));
        assert!(!detector.is_binary_by_extension(Path::new("test.rs")));
        assert!(!detector.is_binary_by_extension(Path::new("test.py")));
        assert!(!detector.is_binary_by_extension(Path::new("Makefile")));
    }

    #[test]
    fn test_text_file_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a text file
        let text_file = temp_dir.path().join("test.txt");
        let mut file = File::create(&text_file)?;
        writeln!(file, "This is a text file with some content.")?;
        writeln!(
            file,
            "It has multiple lines and should be detected as text."
        )?;

        assert!(detector.is_text_file(&text_file)?);
        assert!(!detector.is_binary(&text_file)?);

        Ok(())
    }

    #[test]
    fn test_binary_file_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a binary file with null bytes
        let binary_file = temp_dir.path().join("test.bin");
        let mut file = File::create(&binary_file)?;
        file.write_all(&[0x00, 0x01, 0x02, 0x03, 0xFF, 0xFE, 0xFD])?;

        assert!(!detector.is_text_file(&binary_file)?);
        assert!(detector.is_binary(&binary_file)?);

        Ok(())
    }

    #[test]
    fn test_empty_file_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create an empty file
        let empty_file = temp_dir.path().join("empty.txt");
        File::create(&empty_file)?;

        // Empty files should be treated as text
        assert!(detector.is_text_file(&empty_file)?);
        assert!(!detector.is_binary(&empty_file)?);

        Ok(())
    }

    #[test]
    fn test_utf8_file_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a UTF-8 file with unicode content
        let utf8_file = temp_dir.path().join("unicode.txt");
        let mut file = File::create(&utf8_file)?;
        writeln!(file, "Hello, 世界! 🌍")?;
        writeln!(
            file,
            "This file contains émojis and unicode characters: 日本語"
        )?;
        drop(file);

        assert!(detector.is_text_file(&utf8_file)?);
        assert!(!detector.is_binary(&utf8_file)?);

        Ok(())
    }

    #[test]
    fn test_binary_reason() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Test extension-based detection
        let exe_file = temp_dir.path().join("test.exe");
        File::create(&exe_file)?;
        let reason = detector.get_binary_reason(&exe_file)?;
        assert!(reason.is_some());
        assert!(reason.unwrap().contains("extension"));

        // Test null byte detection
        let binary_file = temp_dir.path().join("test.bin"); // Use .bin extension to force binary detection first
        let mut file = File::create(&binary_file)?;
        file.write_all(b"some text\x00more text")?;
        drop(file); // Ensure file is closed
        let reason = detector.get_binary_reason(&binary_file)?;
        assert!(reason.is_some());
        let reason_str = reason.unwrap();
        assert!(
            reason_str.contains("extension")
                || reason_str.contains("null")
                || reason_str.contains("binary")
        );

        // Test text file (should not be binary)
        let text_file = temp_dir.path().join("test.txt");
        let mut file = File::create(&text_file)?;
        writeln!(file, "This is just text")?;
        let reason = detector.get_binary_reason(&text_file)?;
        assert!(reason.is_none());

        Ok(())
    }

    #[test]
    fn test_printable_ascii() {
        // Test printable characters
        assert!(is_printable_ascii(b' ')); // space
        assert!(is_printable_ascii(b'A')); // letter
        assert!(is_printable_ascii(b'0')); // digit
        assert!(is_printable_ascii(b'~')); // tilde
        assert!(is_printable_ascii(b'\t')); // tab
        assert!(is_printable_ascii(b'\n')); // newline
        assert!(is_printable_ascii(b'\r')); // carriage return

        // Test non-printable characters
        assert!(!is_printable_ascii(0x00)); // null
        assert!(!is_printable_ascii(0x01)); // control character
        assert!(!is_printable_ascii(0x7F)); // DEL
        assert!(!is_printable_ascii(0x80)); // extended ASCII
        assert!(!is_printable_ascii(0xFF)); // extended ASCII
    }

    #[test]
    fn test_utf8_start_detection() {
        // Valid UTF-8 start bytes
        assert!(is_valid_utf8_start(0x41)); // ASCII 'A'
        assert!(is_valid_utf8_start(0xC2)); // 2-byte UTF-8 start
        assert!(is_valid_utf8_start(0xE0)); // 3-byte UTF-8 start
        assert!(is_valid_utf8_start(0xF0)); // 4-byte UTF-8 start

        // Invalid UTF-8 start bytes
        assert!(!is_valid_utf8_start(0x80)); // continuation byte
        assert!(!is_valid_utf8_start(0xBF)); // continuation byte
        assert!(!is_valid_utf8_start(0xF8)); // invalid start byte
        assert!(!is_valid_utf8_start(0xFF)); // invalid start byte
    }

    #[test]
    fn test_protocol_buffer_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a mock Protocol Buffer file
        // Protocol Buffer wire format: field_number << 3 | wire_type
        let protobuf_data = vec![
            0x08, 0x96, 0x01, // field 1, varint, value 150
            0x12, 0x04, 0x74, 0x65, 0x73, 0x74, // field 2, string, "test"
            0x1a, 0x03, 0x66, 0x6f, 0x6f, // field 3, string, "foo"
            0x20, 0x42, // field 4, varint, value 66
        ];

        let protobuf_file = temp_dir.path().join("test.pb");
        let mut file = File::create(&protobuf_file)?;
        file.write_all(&protobuf_data)?;
        drop(file);

        // Should be detected as binary due to Protocol Buffer patterns
        assert!(detector.is_binary(&protobuf_file)?);
        assert!(!detector.is_text_file(&protobuf_file)?);

        // Test the reason detection
        let reason = detector.get_binary_reason(&protobuf_file)?;
        assert!(reason.is_some());
        let reason_str = reason.unwrap();
        assert!(
            reason_str.contains("Protocol Buffer")
                || reason_str.contains("binary")
                || reason_str.contains("extension")
        );

        Ok(())
    }

    #[test]
    fn test_protocol_buffer_patterns() {
        let detector = BinaryDetector::default();

        // Test typical Protocol Buffer patterns
        let protobuf_patterns = vec![
            // Varint field (field 1, wire type 0)
            0x08, 0x96, 0x01, // Length-delimited field (field 2, wire type 2)
            0x12, 0x04, 0x74, 0x65, 0x73, 0x74, // Another varint
            0x20, 0x42,
        ];

        assert!(detector.is_protocol_buffer(&protobuf_patterns));

        // Test non-protobuf data
        let text_data = b"This is plain text content";
        assert!(!detector.is_protocol_buffer(text_data));

        // Test empty or very short data
        assert!(!detector.is_protocol_buffer(&[]));
        assert!(!detector.is_protocol_buffer(&[0x01]));
    }

    #[test]
    fn test_protobuf_vs_text_distinction() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a file that might look like protobuf but is actually text
        let pseudo_protobuf_text = temp_dir.path().join("pseudo.txt");
        let mut file = File::create(&pseudo_protobuf_text)?;
        writeln!(
            file,
            "This file contains some bytes like \\x08 and \\x12 but is text"
        )?;
        drop(file);

        // Should be detected as text, not protobuf
        assert!(detector.is_text_file(&pseudo_protobuf_text)?);
        assert!(!detector.is_binary(&pseudo_protobuf_text)?);

        // Create an actual binary protobuf-like file
        let real_protobuf = temp_dir.path().join("real.pb");
        let protobuf_data = vec![
            0x08, 0x96, 0x01, // field 1, varint
            0x12, 0x04, 0x74, 0x65, 0x73, 0x74, // field 2, "test"
            0x1a, 0x06, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, // field 3, "binary"
            0x20, 0x2a, // field 4, varint
            0x2a, 0x08, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, // field 5, bytes
        ];
        let mut file = File::create(&real_protobuf)?;
        file.write_all(&protobuf_data)?;
        drop(file);

        // Should be detected as binary protobuf
        assert!(detector.is_binary(&real_protobuf)?);
        assert!(!detector.is_text_file(&real_protobuf)?);

        Ok(())
    }

    #[test]
    fn test_pb_file_detection() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create a .pb file (protobuf file)
        let pb_file = temp_dir.path().join("test.pb");
        let mut file = File::create(&pb_file)?;
        file.write_all(b"some protobuf binary data")?;
        drop(file);

        // .pb files should be detected as binary by extension
        assert!(
            detector.is_binary(&pb_file)?,
            ".pb file should be detected as binary"
        );
        assert!(
            !detector.is_text_file(&pb_file)?,
            ".pb file should NOT be detected as text"
        );

        let reason = detector.get_binary_reason(&pb_file)?;
        assert!(reason.is_some(), ".pb file should have a binary reason");
        assert!(
            reason.unwrap().contains("extension"),
            "Reason should mention extension"
        );

        Ok(())
    }

    #[test]
    fn test_discovery_phase_simulation() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let detector = BinaryDetector::default();

        // Create various file types to simulate a discovery phase
        let pb_file = temp_dir.path().join("test.pb");
        let txt_file = temp_dir.path().join("test.txt");
        let rs_file = temp_dir.path().join("test.rs");

        // Create .pb file with some content that might contain search string
        let mut file = File::create(&pb_file)?;
        file.write_all(b"oldstring should not be processed in binary files")?;
        drop(file);

        // Create text files
        let mut file = File::create(&txt_file)?;
        file.write_all(b"This contains oldstring and should be processed")?;
        drop(file);

        let mut file = File::create(&rs_file)?;
        file.write_all(b"fn main() { println!(\"oldstring\"); }")?;
        drop(file);

        // Simulate the file_needs_content_replacement logic
        let search_string = "oldstring";

        // Test .pb file - should NOT be added to content_files
        let pb_is_text = detector.is_text_file(&pb_file)?;
        println!("pb_file is_text_file: {}", pb_is_text);
        if pb_is_text {
            // This would be the bug - if we get here, .pb files are being misclassified
            let contains_string = std::fs::read_to_string(&pb_file)?.contains(search_string);
            println!(
                "ERROR: .pb file classified as text and contains search string: {}",
                contains_string
            );
            assert!(false, ".pb file should not be classified as text!");
        } else {
            println!(
                "OK: .pb file correctly classified as binary, will not be processed for content"
            );
        }

        // Test text files - should be added to content_files if they contain the string
        assert!(detector.is_text_file(&txt_file)?, "txt file should be text");
        assert!(detector.is_text_file(&rs_file)?, "rs file should be text");

        Ok(())
    }
}