fob_graph/analysis/extractors/common.rs
1//! Common types and traits for framework extractors.
2//!
3//! This module defines the unified interface for extracting JavaScript/TypeScript
4//! from framework-specific file formats.
5
6/// Represents JavaScript/TypeScript source code extracted from a framework file.
7///
8/// This is a unified type that replaces framework-specific `JavaScriptSource` types.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ExtractedScript<'a> {
11 /// The extracted JavaScript/TypeScript source code
12 pub source_text: &'a str,
13
14 /// Byte offset from the start of the original file
15 ///
16 /// This offset points to the beginning of the script content.
17 /// Used to translate parse errors back to the original file location.
18 pub source_offset: usize,
19
20 /// Context information about this script block
21 pub context: ScriptContext,
22
23 /// Language identifier (js, ts, jsx, tsx)
24 pub lang: &'a str,
25}
26
27impl<'a> ExtractedScript<'a> {
28 /// Creates a new extracted script with the given parameters.
29 pub fn new(
30 source_text: &'a str,
31 source_offset: usize,
32 context: ScriptContext,
33 lang: &'a str,
34 ) -> Self {
35 Self {
36 source_text,
37 source_offset,
38 context,
39 lang,
40 }
41 }
42}
43
44/// Context information about an extracted script block.
45///
46/// Different frameworks use different contexts to distinguish script blocks.
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum ScriptContext {
49 // Framework-specific contexts can be added here as needed
50}
51
52/// Unified error type for all extractors.
53#[derive(Debug, thiserror::Error)]
54pub enum ExtractorError {
55 /// File exceeds maximum allowed size
56 #[error("File too large: {size} bytes (max: {max} bytes)")]
57 FileTooLarge {
58 /// Actual file size in bytes
59 size: usize,
60 /// Maximum allowed size in bytes
61 max: usize,
62 },
63
64 /// Too many script tags found in the file
65 #[error("Too many script tags: {count} found (max: {max} allowed)")]
66 TooManyScriptTags {
67 /// Number of script tags found
68 count: usize,
69 /// Maximum allowed script tags
70 max: usize,
71 },
72
73 /// Script tag opened but never closed
74 #[error("Unclosed script tag starting at byte position {position}")]
75 UnclosedScriptTag {
76 /// Byte position where the unclosed tag begins
77 position: usize,
78 },
79
80 /// File contains invalid UTF-8
81 #[error("Invalid UTF-8 encoding in file")]
82 InvalidUtf8,
83
84 /// I/O error reading the file
85 #[error("I/O error: {0}")]
86 Io(#[from] std::io::Error),
87}
88
89/// Trait for framework-specific extractors.
90///
91/// Frameworks can implement this trait to provide script extraction capabilities.
92pub trait Extractor {
93 /// Extract JavaScript/TypeScript scripts from the given source code.
94 ///
95 /// # Arguments
96 ///
97 /// * `source` - The complete file content
98 ///
99 /// # Returns
100 ///
101 /// A vector of extracted scripts, or an error if extraction fails.
102 fn extract<'a>(&self, source: &'a str) -> Result<Vec<ExtractedScript<'a>>, ExtractorError>;
103
104 /// Get the file extension this extractor handles (e.g., ".js", ".ts").
105 fn file_extension(&self) -> &'static str;
106}
107
108// Constants moved to crate::config module
109// Re-export for backward compatibility
110pub use crate::analysis::config::{MAX_FILE_SIZE, MAX_SCRIPT_TAGS};