scribe-core 0.5.1

Core types and utilities for the Scribe code analysis library
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
//! File-related types and utilities.
//!
//! Provides comprehensive file metadata structures, language detection,
//! and file classification utilities for the Scribe analysis pipeline.

use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use crate::error::{Result, ScribeError};

/// Binary file extensions that should typically be excluded from text analysis
pub const BINARY_EXTENSIONS: &[&str] = &[
    // Images
    ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".svg", ".ico", ".tiff",
    // Documents
    ".pdf", ".doc", ".docx", ".ppt", ".pptx", ".xls", ".xlsx", // Archives
    ".zip", ".tar", ".gz", ".bz2", ".xz", ".7z", ".rar", // Media
    ".mp3", ".mp4", ".mov", ".avi", ".mkv", ".wav", ".ogg", ".flac", // Fonts
    ".ttf", ".otf", ".eot", ".woff", ".woff2", // Executables and libraries
    ".so", ".dll", ".dylib", ".class", ".jar", ".exe", ".bin", ".app",
];

/// Markdown file extensions
pub const MARKDOWN_EXTENSIONS: &[&str] = &[".md", ".markdown", ".mdown", ".mkd", ".mkdn"];

/// MIME types under `application/*` that should be treated as plain text.
const TEXTUAL_APPLICATION_MIME_TYPES: &[&str] = &[
    "application/json",
    "application/ld+json",
    "application/graphql",
    "application/javascript",
    "application/x-javascript",
    "application/typescript",
    "application/x-typescript",
    "application/xml",
    "application/xhtml+xml",
    "application/x-sh",
    "application/x-shellscript",
    "application/x-bash",
    "application/x-zsh",
    "application/x-python",
    "application/x-ruby",
    "application/x-perl",
    "application/x-php",
    "application/x-httpd-php",
    "application/x-toml",
    "application/toml",
    "application/x-yaml",
    "application/yaml",
    "application/x-sql",
    "application/sql",
    "application/x-rust",
    "application/x-go",
    "application/x-java",
    "application/x-scala",
    "application/x-kotlin",
    "application/x-swift",
    "application/x-dart",
    "application/x-haskell",
    "application/x-clojure",
    "application/x-ocaml",
    "application/x-lisp",
    "application/x-r",
    "application/x-matlab",
    "application/x-tex",
    "application/x-empty",
];

/// Keywords inside MIME subtypes that indicate textual content even if the
/// top-level type is `application/*`.
const TEXTUAL_APPLICATION_KEYWORDS: &[&str] = &[
    "+json",
    "+xml",
    "json",
    "xml",
    "yaml",
    "yml",
    "toml",
    "graphql",
    "javascript",
    "typescript",
    "ecmascript",
    "shellscript",
    "shell",
    "bash",
    "zsh",
    "sh",
    "python",
    "ruby",
    "perl",
    "php",
    "rust",
    "go",
    "java",
    "scala",
    "kotlin",
    "swift",
    "dart",
    "haskell",
    "clojure",
    "ocaml",
    "lisp",
    "sql",
    "graphql",
    "tex",
    "rscript",
    "matlab",
];

/// Decision about whether to include a file in analysis
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenderDecision {
    /// Whether to include the file in analysis
    pub include: bool,
    /// Human-readable reason for the decision
    pub reason: String,
    /// Optional additional context
    pub context: Option<String>,
}

impl RenderDecision {
    /// Create a decision to include the file
    pub fn include<S: Into<String>>(reason: S) -> Self {
        Self {
            include: true,
            reason: reason.into(),
            context: None,
        }
    }

    /// Create a decision to exclude the file
    pub fn exclude<S: Into<String>>(reason: S) -> Self {
        Self {
            include: false,
            reason: reason.into(),
            context: None,
        }
    }

    /// Add context to the decision
    pub fn with_context<S: Into<String>>(mut self, context: S) -> Self {
        self.context = Some(context.into());
        self
    }

    /// Check if the file should be included
    pub fn should_include(&self) -> bool {
        self.include
    }

    /// Get the reason as a standard category
    pub fn reason_category(&self) -> RenderDecisionCategory {
        match self.reason.as_str() {
            "ok" => RenderDecisionCategory::Ok,
            "binary" => RenderDecisionCategory::Binary,
            "too_large" => RenderDecisionCategory::TooLarge,
            "ignored" => RenderDecisionCategory::Ignored,
            "empty" => RenderDecisionCategory::Empty,
            _ => RenderDecisionCategory::Other,
        }
    }
}

/// Standard categories for render decisions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RenderDecisionCategory {
    Ok,
    Binary,
    TooLarge,
    Ignored,
    Empty,
    Other,
}

/// Programming language classification
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Language {
    // Systems languages
    Rust,
    C,
    Cpp,
    Go,
    Zig,

    // Web languages
    JavaScript,
    TypeScript,
    HTML,
    CSS,
    SCSS,
    SASS,

    // Backend languages
    Python,
    Java,
    CSharp,
    Kotlin,
    Scala,
    Ruby,
    PHP,

    // Functional languages
    Haskell,
    OCaml,
    FSharp,
    Erlang,
    Elixir,
    Clojure,

    // Configuration and markup
    JSON,
    YAML,
    TOML,
    XML,
    Markdown,

    // Database
    SQL,

    // Shell and scripts
    Bash,
    PowerShell,
    Batch,

    // Data science
    R,
    Julia,
    Matlab,

    // Mobile
    Swift,
    ObjectiveC,
    Dart,

    // Other
    Unknown,
}

impl Language {
    /// Detect language from file extension
    pub fn from_extension(ext: &str) -> Self {
        match ext.to_lowercase().as_str() {
            "rs" => Language::Rust,
            "c" | "h" => Language::C,
            "cpp" | "cxx" | "cc" | "hpp" | "hxx" => Language::Cpp,
            "go" => Language::Go,
            "zig" => Language::Zig,
            "js" | "mjs" | "cjs" => Language::JavaScript,
            "ts" | "mts" | "cts" => Language::TypeScript,
            "html" | "htm" => Language::HTML,
            "css" => Language::CSS,
            "scss" => Language::SCSS,
            "sass" => Language::SASS,
            "py" | "pyi" | "pyw" => Language::Python,
            "java" => Language::Java,
            "cs" => Language::CSharp,
            "kt" | "kts" => Language::Kotlin,
            "scala" | "sc" => Language::Scala,
            "rb" => Language::Ruby,
            "php" => Language::PHP,
            "hs" | "lhs" => Language::Haskell,
            "ml" | "mli" => Language::OCaml,
            "fs" | "fsi" | "fsx" => Language::FSharp,
            "erl" | "hrl" => Language::Erlang,
            "ex" | "exs" => Language::Elixir,
            "clj" | "cljs" | "cljc" => Language::Clojure,
            "json" => Language::JSON,
            "yaml" | "yml" => Language::YAML,
            "toml" => Language::TOML,
            "xml" => Language::XML,
            "md" | "markdown" | "mdown" | "mkd" | "mkdn" => Language::Markdown,
            "sql" => Language::SQL,
            "sh" | "bash" => Language::Bash,
            "ps1" | "psm1" | "psd1" => Language::PowerShell,
            "bat" | "cmd" => Language::Batch,
            "r" => Language::R,
            "jl" => Language::Julia,
            "swift" => Language::Swift,
            "dart" => Language::Dart,
            // Handle ambiguous .m extension - could be Matlab or Objective-C
            // Default to Objective-C as it's more common in modern development
            "m" | "mm" => Language::ObjectiveC,
            _ => Language::Unknown,
        }
    }

    /// Check if this language is typically used for documentation
    pub fn is_documentation(&self) -> bool {
        matches!(self, Language::Markdown | Language::HTML)
    }

    /// Check if this language is typically used for configuration
    pub fn is_configuration(&self) -> bool {
        matches!(
            self,
            Language::JSON | Language::YAML | Language::TOML | Language::XML
        )
    }

    /// Check if this is a programming language (not markup/config)
    pub fn is_programming(&self) -> bool {
        !matches!(
            self,
            Language::Markdown
                | Language::HTML
                | Language::JSON
                | Language::YAML
                | Language::TOML
                | Language::XML
                | Language::Unknown
        )
    }

    /// Display name used for user-facing messaging
    pub fn display_name(&self) -> &'static str {
        match self {
            Language::Rust => "Rust",
            Language::C => "C",
            Language::Cpp => "C++",
            Language::Go => "Go",
            Language::Zig => "Zig",
            Language::JavaScript => "JavaScript",
            Language::TypeScript => "TypeScript",
            Language::HTML => "HTML",
            Language::CSS => "CSS",
            Language::SCSS => "SCSS",
            Language::SASS => "SASS",
            Language::Python => "Python",
            Language::Java => "Java",
            Language::CSharp => "C#",
            Language::Kotlin => "Kotlin",
            Language::Scala => "Scala",
            Language::Ruby => "Ruby",
            Language::PHP => "PHP",
            Language::Haskell => "Haskell",
            Language::OCaml => "OCaml",
            Language::FSharp => "F#",
            Language::Erlang => "Erlang",
            Language::Elixir => "Elixir",
            Language::Clojure => "Clojure",
            Language::JSON => "JSON",
            Language::YAML => "YAML",
            Language::TOML => "TOML",
            Language::XML => "XML",
            Language::Markdown => "Markdown",
            Language::SQL => "SQL",
            Language::Bash => "Bash",
            Language::PowerShell => "PowerShell",
            Language::Batch => "Batch",
            Language::R => "R",
            Language::Julia => "Julia",
            Language::Matlab => "Matlab",
            Language::Swift => "Swift",
            Language::ObjectiveC => "Objective-C",
            Language::Dart => "Dart",
            Language::Bash => "Bash",
            Language::Unknown => "Unknown",
        }
    }

    /// Get the typical file extensions for this language
    pub fn extensions(&self) -> &'static [&'static str] {
        match self {
            Language::Rust => &["rs"],
            Language::C => &["c", "h"],
            Language::Cpp => &["cpp", "cxx", "cc", "hpp", "hxx"],
            Language::Go => &["go"],
            Language::Zig => &["zig"],
            Language::JavaScript => &["js", "mjs", "cjs"],
            Language::TypeScript => &["ts", "mts", "cts"],
            Language::HTML => &["html", "htm"],
            Language::CSS => &["css"],
            Language::SCSS => &["scss"],
            Language::SASS => &["sass"],
            Language::Python => &["py", "pyi", "pyw"],
            Language::Java => &["java"],
            Language::CSharp => &["cs"],
            Language::Kotlin => &["kt", "kts"],
            Language::Scala => &["scala", "sc"],
            Language::Ruby => &["rb"],
            Language::PHP => &["php"],
            Language::Haskell => &["hs", "lhs"],
            Language::OCaml => &["ml", "mli"],
            Language::FSharp => &["fs", "fsi", "fsx"],
            Language::Erlang => &["erl", "hrl"],
            Language::Elixir => &["ex", "exs"],
            Language::Clojure => &["clj", "cljs", "cljc"],
            Language::JSON => &["json"],
            Language::YAML => &["yaml", "yml"],
            Language::TOML => &["toml"],
            Language::XML => &["xml"],
            Language::Markdown => &["md", "markdown", "mdown", "mkd", "mkdn"],
            Language::SQL => &["sql"],
            Language::Bash => &["sh", "bash"],
            Language::PowerShell => &["ps1", "psm1", "psd1"],
            Language::Batch => &["bat", "cmd"],
            Language::R => &["r"],
            Language::Julia => &["jl"],
            Language::Matlab => &["m"], // Note: .m conflicts with Objective-C
            Language::Swift => &["swift"],
            Language::ObjectiveC => &["m", "mm"],
            Language::Dart => &["dart"],
            Language::Unknown => &[],
        }
    }
}

/// File type classification for analysis purposes
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FileType {
    /// Source code files
    Source { language: Language },
    /// Documentation files
    Documentation { format: DocumentationFormat },
    /// Configuration files
    Configuration { format: ConfigurationFormat },
    /// Test files
    Test { language: Language },
    /// Binary files that should be excluded
    Binary,
    /// Generated or built files
    Generated,
    /// Unknown or unclassified
    Unknown,
}

impl FileType {
    pub fn display_label(&self) -> &'static str {
        match self {
            FileType::Source { .. } => "Source",
            FileType::Documentation { .. } => "Documentation",
            FileType::Configuration { .. } => "Configuration",
            FileType::Test { .. } => "Test",
            FileType::Binary => "Binary",
            FileType::Generated => "Generated",
            FileType::Unknown => "Unknown",
        }
    }
}

/// Documentation format classification
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DocumentationFormat {
    Markdown,
    Html,
    PlainText,
    Rst,
    Asciidoc,
}

/// Configuration format classification
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConfigurationFormat {
    Json,
    Yaml,
    Toml,
    Xml,
    Ini,
    Dotenv,
}

/// Comprehensive file metadata structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileInfo {
    /// Absolute path to the file on disk
    pub path: PathBuf,

    /// Path relative to repository root (forward slash separated)
    pub relative_path: String,

    /// File size in bytes
    pub size: u64,

    /// File modification time
    pub modified: Option<SystemTime>,

    /// Analysis decision (include/exclude)
    pub decision: RenderDecision,

    /// Detected file type
    pub file_type: FileType,

    /// Detected programming language
    pub language: Language,

    /// File content (loaded on demand)
    pub content: Option<String>,

    /// Estimated token count for LLM processing
    pub token_estimate: Option<usize>,

    /// Line count (if text file)
    pub line_count: Option<usize>,

    /// Character count (if text file)
    pub char_count: Option<usize>,

    /// Whether the file is likely binary
    pub is_binary: bool,

    /// Git status information (if available)
    pub git_status: Option<GitStatus>,

    /// PageRank centrality score (0.0-1.0, higher means more important)
    pub centrality_score: Option<f64>,
}

/// Git status information for a file
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GitStatus {
    /// Working tree status
    pub working_tree: GitFileStatus,
    /// Index/staging area status
    pub index: GitFileStatus,
}

/// Git file status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum GitFileStatus {
    Unmodified,
    Modified,
    Added,
    Deleted,
    Renamed,
    Copied,
    Unmerged,
    Untracked,
    Ignored,
}

impl FileInfo {
    /// Create a new FileInfo from a path
    pub fn new<P: AsRef<Path>>(
        path: P,
        relative_path: String,
        decision: RenderDecision,
    ) -> Result<Self> {
        let path = path.as_ref();
        let metadata = std::fs::metadata(path)
            .map_err(|e| ScribeError::path_with_source("Failed to read file metadata", path, e))?;

        let size = metadata.len();
        let modified = metadata.modified().ok();

        let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");

        let language = Language::from_extension(extension);
        let is_binary = Self::detect_binary_with_hint(path, extension);
        let file_type =
            Self::classify_file_type_with_binary(&relative_path, &language, extension, is_binary);

        Ok(Self {
            path: path.to_path_buf(),
            relative_path,
            size,
            modified,
            decision,
            file_type,
            language,
            content: None,
            token_estimate: None,
            line_count: None,
            char_count: None,
            is_binary,
            git_status: None,
            centrality_score: None,
        })
    }

    /// Load file content and compute statistics
    pub fn load_content(&mut self) -> Result<()> {
        if self.is_binary || !self.decision.should_include() {
            return Ok(());
        }

        let content = std::fs::read_to_string(&self.path).map_err(|e| {
            ScribeError::analysis(format!("Failed to read file content: {}", e), &self.path)
        })?;

        // Compute statistics
        let line_count = content.lines().count();
        let char_count = content.chars().count();
        let token_estimate = Self::estimate_tokens(&content);

        self.content = Some(content);
        self.line_count = Some(line_count);
        self.char_count = Some(char_count);
        self.token_estimate = Some(token_estimate);

        Ok(())
    }

    /// Estimate token count for LLM processing using tiktoken
    ///
    /// This method uses the shared global TokenCounter instance for optimal performance.
    /// If tiktoken fails, it falls back to the legacy character-based estimation.
    pub fn estimate_tokens(content: &str) -> usize {
        use crate::tokenization::{utils, TokenCounter};

        // Use the shared global instance for optimal performance
        match TokenCounter::global().count_tokens(content) {
            Ok(tokens) => tokens,
            Err(_) => {
                // Fall back to legacy estimation if tiktoken fails
                utils::estimate_tokens_legacy(content)
            }
        }
    }

    /// Estimate token count for LLM processing with file context
    ///
    /// This method uses the file path to apply language-specific multipliers
    /// for more accurate token estimation.
    pub fn estimate_tokens_with_path(content: &str, file_path: &std::path::Path) -> usize {
        use crate::tokenization::TokenCounter;

        // Use the shared global instance for optimal performance
        match TokenCounter::global().estimate_file_tokens(content, file_path) {
            Ok(tokens) => tokens,
            Err(_) => Self::estimate_tokens(content), // Fall back to basic estimation
        }
    }

    /// Detect whether a file is binary using libmagic-compatible signatures with
    /// sensible fallbacks for small or unknown files.
    pub fn detect_binary(path: &Path) -> bool {
        let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");
        Self::detect_binary_with_hint(path, extension)
    }

    /// Detect whether a file is binary, allowing the caller to provide an
    /// extension hint for fallback heuristics.
    pub fn detect_binary_with_hint(path: &Path, extension: &str) -> bool {
        if let Some(mime) = tree_magic_mini::from_filepath(path) {
            if !Self::is_textual_mime(mime) {
                return true;
            }
            return false;
        }

        if let Ok(mut file) = File::open(path) {
            let mut buffer = [0u8; 8192];
            if let Ok(read) = file.read(&mut buffer) {
                if read == 0 {
                    return false;
                }

                let slice = &buffer[..read];
                let mime = tree_magic_mini::from_u8(slice);
                if !Self::is_textual_mime(mime) {
                    return true;
                }

                if slice.iter().any(|byte| *byte == 0) {
                    return true;
                }
            }
        }

        Self::detect_binary_by_extension(extension)
    }

    /// Detect whether in-memory content represents a binary file.
    pub fn detect_binary_from_bytes(bytes: &[u8], extension: Option<&str>) -> bool {
        if bytes.is_empty() {
            return false;
        }

        let mime = tree_magic_mini::from_u8(bytes);
        if !Self::is_textual_mime(mime) {
            return true;
        }

        if bytes.iter().any(|byte| *byte == 0) {
            return true;
        }

        extension
            .map(Self::detect_binary_by_extension)
            .unwrap_or(false)
    }

    /// Check if file extension indicates binary content (fallback heuristic).
    pub fn detect_binary_by_extension(extension: &str) -> bool {
        if extension.is_empty() {
            return false;
        }

        let trimmed = extension.trim_start_matches('.');
        let lower = trimmed.to_lowercase();
        let prefixed = format!(".{}", lower);

        BINARY_EXTENSIONS.contains(&prefixed.as_str())
    }

    #[inline]
    fn is_textual_mime(mime: &str) -> bool {
        let canonical = mime
            .split(';')
            .next()
            .unwrap_or(mime)
            .trim()
            .to_ascii_lowercase();
        let mime = canonical.as_str();

        if mime.starts_with("text/") || mime.starts_with("inode/") || mime.starts_with("message/") {
            return true;
        }

        if mime.starts_with("application/") {
            if TEXTUAL_APPLICATION_MIME_TYPES.contains(&mime) {
                return true;
            }

            if TEXTUAL_APPLICATION_KEYWORDS
                .iter()
                .any(|keyword| mime.contains(keyword))
            {
                return true;
            }
        }

        false
    }

    /// Classify file type based on path and language
    pub fn classify_file_type(path: &str, language: &Language, extension: &str) -> FileType {
        let is_binary = Self::detect_binary_by_extension(extension);
        Self::classify_file_type_with_binary(path, language, extension, is_binary)
    }

    /// Classify file type when the binary state is already known.
    pub fn classify_file_type_with_binary(
        path: &str,
        language: &Language,
        extension: &str,
        is_binary: bool,
    ) -> FileType {
        let path_lower = path.to_lowercase();

        if is_binary {
            return FileType::Binary;
        }

        // Test files
        if is_test_path(Path::new(path)) {
            return FileType::Test {
                language: language.clone(),
            };
        }

        // Documentation
        if language.is_documentation() {
            let format = match extension {
                "md" | "markdown" => DocumentationFormat::Markdown,
                "html" | "htm" => DocumentationFormat::Html,
                "rst" => DocumentationFormat::Rst,
                "txt" => DocumentationFormat::PlainText,
                _ => DocumentationFormat::Markdown,
            };
            return FileType::Documentation { format };
        }

        // Configuration
        if language.is_configuration() {
            let format = match extension {
                "json" => ConfigurationFormat::Json,
                "yaml" | "yml" => ConfigurationFormat::Yaml,
                "toml" => ConfigurationFormat::Toml,
                "xml" => ConfigurationFormat::Xml,
                "ini" => ConfigurationFormat::Ini,
                "env" => ConfigurationFormat::Dotenv,
                _ => ConfigurationFormat::Json,
            };
            return FileType::Configuration { format };
        }

        // Generated files (common patterns)
        if path_lower.contains("generated")
            || path_lower.contains("build")
            || path_lower.contains("dist")
            || path_lower.contains("target")
        {
            return FileType::Generated;
        }

        // Source code
        if language.is_programming() {
            return FileType::Source {
                language: language.clone(),
            };
        }

        FileType::Unknown
    }

    /// Get human-readable size
    pub fn human_size(&self) -> String {
        bytes_to_human(self.size)
    }

    /// Check if file should be included in analysis
    pub fn should_include(&self) -> bool {
        self.decision.should_include()
    }

    /// Get file name (last component of path)
    pub fn file_name(&self) -> Option<&str> {
        self.path.file_name()?.to_str()
    }

    /// Get file stem (name without extension)
    pub fn file_stem(&self) -> Option<&str> {
        self.path.file_stem()?.to_str()
    }

    /// Get file extension
    pub fn extension(&self) -> Option<&str> {
        self.path.extension()?.to_str()
    }
}

/// Convert bytes to human-readable format
pub fn bytes_to_human(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KiB", "MiB", "GiB", "TiB"];
    const THRESHOLD: f64 = 1024.0;

    if bytes == 0 {
        return "0 B".to_string();
    }

    let mut size = bytes as f64;
    let mut unit_idx = 0;

    while size >= THRESHOLD && unit_idx < UNITS.len() - 1 {
        size /= THRESHOLD;
        unit_idx += 1;
    }

    if unit_idx == 0 {
        format!("{} {}", bytes, UNITS[unit_idx])
    } else {
        format!("{:.1} {}", size, UNITS[unit_idx])
    }
}

/// Detect language from a file path based on extension or special names
pub fn detect_language_from_path(path: &Path) -> Language {
    path.extension()
        .and_then(|ext| ext.to_str())
        .map(Language::from_extension)
        .unwrap_or(Language::Unknown)
}

/// Convenience helper returning the human-friendly language name
pub fn language_display_name(language: &Language) -> &'static str {
    language.display_name()
}

/// Heuristic test-file detection based on path segments and naming conventions
pub fn is_test_path(path: &Path) -> bool {
    let path_lower = path.to_string_lossy().to_lowercase();
    let file_name = path
        .file_name()
        .and_then(|s| s.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();

    if file_name == "output.md" || file_name.starts_with("output.") {
        return true;
    }

    let segments: Vec<&str> = path_lower
        .split(|c| c == '/' || c == '\\')
        .filter(|segment| !segment.is_empty())
        .collect();

    const TEST_DIR_MARKERS: &[&str] = &[
        "test",
        "tests",
        "testing",
        "__tests__",
        "integration-tests",
        "integration_test",
        "integrationtests",
        "e2e",
        "qa",
        "spec",
    ];

    if segments
        .iter()
        .any(|segment| TEST_DIR_MARKERS.contains(segment))
    {
        return true;
    }

    const TEST_PREFIXES: &[&str] = &["test_", "spec_", "itest_", "integration_"];
    if TEST_PREFIXES
        .iter()
        .any(|prefix| file_name.starts_with(prefix))
    {
        return true;
    }

    const TEST_SUFFIXES: &[&str] = &["_test", "_tests", "_spec", "_itest", "_integration", "_e2e"];
    if TEST_SUFFIXES
        .iter()
        .any(|suffix| file_name.strip_suffix(suffix).is_some())
    {
        return true;
    }

    if file_name.contains(".test.") || file_name.contains(".spec.") {
        return true;
    }

    let ext = path
        .extension()
        .and_then(|s| s.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();

    match ext.as_str() {
        "rs" => file_name.ends_with("_test.rs") || segments.iter().any(|seg| *seg == "tests"),
        "py" => file_name.starts_with("test_") || file_name.ends_with("_test.py"),
        "go" => file_name.ends_with("_test.go"),
        "java" | "kt" => {
            file_name.ends_with("test.java")
                || file_name.ends_with("tests.java")
                || file_name.ends_with("test.kt")
                || file_name.ends_with("tests.kt")
        }
        "php" => file_name.ends_with("test.php"),
        "rb" => file_name.ends_with("_spec.rb") || file_name.ends_with("_test.rb"),
        "js" | "jsx" | "ts" | "tsx" => {
            file_name.contains(".test.")
                || file_name.contains(".spec.")
                || file_name.ends_with("_test.ts")
        }
        _ => false,
    }
}

/// Heuristic entrypoint detection based on common file names per language
pub fn is_entrypoint_path(path: &Path, language: &Language) -> bool {
    let path_lower = path.to_string_lossy().to_lowercase();
    let file_name = path
        .file_name()
        .and_then(|s| s.to_str())
        .map(|s| s.to_lowercase())
        .unwrap_or_default();

    match language {
        Language::Rust => file_name == "main.rs" || file_name == "lib.rs",
        Language::Python => {
            file_name == "main.py"
                || path_lower.contains("/__main__.py")
                || path_lower.contains("/manage.py")
                || file_name == "app.py"
                || file_name == "__init__.py"
        }
        Language::JavaScript | Language::TypeScript => {
            file_name == "index.js"
                || file_name == "index.ts"
                || path_lower.contains("/app.js")
                || path_lower.contains("/server.js")
        }
        Language::Go => file_name == "main.go",
        Language::Java => file_name == "main.java" || path_lower.contains("/main.java"),
        _ => file_name.starts_with("main.") || file_name.starts_with("index."),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_language_detection() {
        assert_eq!(Language::from_extension("rs"), Language::Rust);
        assert_eq!(Language::from_extension("py"), Language::Python);
        assert_eq!(Language::from_extension("js"), Language::JavaScript);
        assert_eq!(Language::from_extension("unknown"), Language::Unknown);
    }

    #[test]
    fn test_binary_detection() {
        assert!(FileInfo::detect_binary_by_extension("png"));
        assert!(FileInfo::detect_binary_by_extension("exe"));
        assert!(!FileInfo::detect_binary_by_extension("rs"));
        assert!(!FileInfo::detect_binary_by_extension("py"));
    }

    #[test]
    fn test_detect_binary_magic_on_files() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut text_file = NamedTempFile::new().unwrap();
        writeln!(text_file, "fn main() {{ println!(\"hi\"); }}").unwrap();

        assert!(!FileInfo::detect_binary(text_file.path()));

        let mut binary_file = NamedTempFile::new().unwrap();
        binary_file
            .write_all(&[0u8, 159, 146, 150, 0, 1, 2])
            .unwrap();

        assert!(FileInfo::detect_binary(binary_file.path()));
    }

    #[test]
    fn test_detect_binary_from_bytes() {
        let text_bytes = b"#!/usr/bin/env python3\nprint('hello')\n";
        assert!(!FileInfo::detect_binary_from_bytes(text_bytes, Some("py")));

        let binary_bytes = [0u8, 255, 1, 2, 3, 4, 5];
        assert!(FileInfo::detect_binary_from_bytes(&binary_bytes, None));
    }

    #[test]
    fn test_file_type_classification() {
        let rust_lang = Language::Rust;
        let py_lang = Language::Python;
        let md_lang = Language::Markdown;

        // Test Rust source files
        assert!(matches!(
            FileInfo::classify_file_type("src/lib.rs", &rust_lang, "rs"),
            FileType::Source { .. }
        ));

        assert!(matches!(
            FileInfo::classify_file_type("scribe-rs/src/lib.rs", &rust_lang, "rs"),
            FileType::Source { .. }
        ));

        // Test Python source files
        assert!(matches!(
            FileInfo::classify_file_type("script.py", &py_lang, "py"),
            FileType::Source { .. }
        ));

        // Test that is_programming works correctly
        assert!(rust_lang.is_programming());
        assert!(py_lang.is_programming());
        assert!(!md_lang.is_programming());
    }

    #[test]
    fn test_integration_file_classification() {
        // Test the full pipeline: extension -> language -> file_type

        // Test Rust files
        let rust_lang = Language::from_extension("rs");
        assert_eq!(rust_lang, Language::Rust);
        assert!(rust_lang.is_programming());

        let rust_file_type = FileInfo::classify_file_type("src/lib.rs", &rust_lang, "rs");
        assert!(matches!(rust_file_type, FileType::Source { .. }));

        // Test Python files
        let py_lang = Language::from_extension("py");
        assert_eq!(py_lang, Language::Python);
        assert!(py_lang.is_programming());

        let py_file_type = FileInfo::classify_file_type("script.py", &py_lang, "py");
        assert!(matches!(py_file_type, FileType::Source { .. }));

        // Test that Unknown language doesn't become Source
        let unknown_lang = Language::from_extension("xyz");
        assert_eq!(unknown_lang, Language::Unknown);
        assert!(!unknown_lang.is_programming());

        let unknown_file_type = FileInfo::classify_file_type("file.xyz", &unknown_lang, "xyz");
        assert!(matches!(unknown_file_type, FileType::Unknown));

        // Test Markdown files
        let md_lang = Language::from_extension("md");
        assert_eq!(md_lang, Language::Markdown);
        assert!(!md_lang.is_programming());

        let md_file_type = FileInfo::classify_file_type("README.md", &md_lang, "md");
        assert!(matches!(md_file_type, FileType::Documentation { .. }));
    }

    #[test]
    fn test_bytes_to_human() {
        assert_eq!(bytes_to_human(0), "0 B");
        assert_eq!(bytes_to_human(512), "512 B");
        assert_eq!(bytes_to_human(1024), "1.0 KiB");
        assert_eq!(bytes_to_human(1536), "1.5 KiB");
        assert_eq!(bytes_to_human(1048576), "1.0 MiB");
    }

    #[test]
    fn test_token_estimation() {
        let content = "Hello world, this is a test.";
        let tokens = FileInfo::estimate_tokens(content);
        assert!(tokens > 0);
        assert!(tokens < 20); // Should be reasonable estimate
    }

    #[test]
    fn test_render_decision() {
        let include = RenderDecision::include("valid file");
        assert!(include.should_include());
        assert_eq!(include.reason_category(), RenderDecisionCategory::Other);

        let exclude = RenderDecision::exclude("binary").with_context("detected by extension");
        assert!(!exclude.should_include());
        assert_eq!(exclude.reason_category(), RenderDecisionCategory::Binary);
        assert!(exclude.context.is_some());
    }
}