embeddenator-testkit 0.21.0

Comprehensive testing utilities and performance benchmarking for embeddenator VSA operations
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
//! Format handlers for various file types
//!
//! Provides metadata extraction and content processing for:
//! - Media formats (images, video, audio)
//! - Document formats (PDF, HTML, XML)
//! - Code formats (multi-language)
//! - Structured data (JSON, CSV, etc.)

use serde::{Deserialize, Serialize};
use std::path::Path;

/// Information extracted from a media file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaInfo {
    /// File format (e.g., "png", "mp4", "mp3")
    pub format: String,
    /// MIME type
    pub mime_type: String,
    /// File size in bytes
    pub size: u64,
    /// Duration in seconds (for video/audio)
    pub duration_secs: Option<f64>,
    /// Width in pixels (for images/video)
    pub width: Option<u32>,
    /// Height in pixels (for images/video)
    pub height: Option<u32>,
    /// Sample rate in Hz (for audio)
    pub sample_rate: Option<u32>,
    /// Number of channels (for audio)
    pub channels: Option<u8>,
    /// Bit depth (for audio)
    pub bit_depth: Option<u8>,
    /// Frame rate (for video)
    pub frame_rate: Option<f64>,
    /// Codec name
    pub codec: Option<String>,
    /// Raw bytes for VSA encoding (extracted frames, samples, or pixels)
    pub raw_data: Vec<u8>,
}

/// Supported media formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MediaFormat {
    // Images
    Png,
    Jpeg,
    WebP,
    Gif,
    Bmp,
    Tiff,
    // Video
    Mp4,
    WebM,
    Avi,
    Mkv,
    Mov,
    // Audio
    Mp3,
    Flac,
    Wav,
    Ogg,
    Aac,
}

impl MediaFormat {
    /// Get format from file extension
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_lowercase().as_str() {
            "png" => Some(Self::Png),
            "jpg" | "jpeg" => Some(Self::Jpeg),
            "webp" => Some(Self::WebP),
            "gif" => Some(Self::Gif),
            "bmp" => Some(Self::Bmp),
            "tiff" | "tif" => Some(Self::Tiff),
            "mp4" | "m4v" => Some(Self::Mp4),
            "webm" => Some(Self::WebM),
            "avi" => Some(Self::Avi),
            "mkv" => Some(Self::Mkv),
            "mov" => Some(Self::Mov),
            "mp3" => Some(Self::Mp3),
            "flac" => Some(Self::Flac),
            "wav" => Some(Self::Wav),
            "ogg" | "oga" => Some(Self::Ogg),
            "aac" | "m4a" => Some(Self::Aac),
            _ => None,
        }
    }

    /// Check if this is an image format
    pub fn is_image(&self) -> bool {
        matches!(
            self,
            Self::Png | Self::Jpeg | Self::WebP | Self::Gif | Self::Bmp | Self::Tiff
        )
    }

    /// Check if this is a video format
    pub fn is_video(&self) -> bool {
        matches!(
            self,
            Self::Mp4 | Self::WebM | Self::Avi | Self::Mkv | Self::Mov
        )
    }

    /// Check if this is an audio format
    pub fn is_audio(&self) -> bool {
        matches!(
            self,
            Self::Mp3 | Self::Flac | Self::Wav | Self::Ogg | Self::Aac
        )
    }

    /// Get MIME type for format
    pub fn mime_type(&self) -> &'static str {
        match self {
            Self::Png => "image/png",
            Self::Jpeg => "image/jpeg",
            Self::WebP => "image/webp",
            Self::Gif => "image/gif",
            Self::Bmp => "image/bmp",
            Self::Tiff => "image/tiff",
            Self::Mp4 => "video/mp4",
            Self::WebM => "video/webm",
            Self::Avi => "video/x-msvideo",
            Self::Mkv => "video/x-matroska",
            Self::Mov => "video/quicktime",
            Self::Mp3 => "audio/mpeg",
            Self::Flac => "audio/flac",
            Self::Wav => "audio/wav",
            Self::Ogg => "audio/ogg",
            Self::Aac => "audio/aac",
        }
    }
}

/// Supported document formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DocumentFormat {
    Pdf,
    Html,
    Xml,
    Markdown,
    Rst,
    Latex,
    Docx,
    Odt,
}

impl DocumentFormat {
    /// Get format from file extension
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_lowercase().as_str() {
            "pdf" => Some(Self::Pdf),
            "html" | "htm" => Some(Self::Html),
            "xml" => Some(Self::Xml),
            "md" | "markdown" => Some(Self::Markdown),
            "rst" => Some(Self::Rst),
            "tex" | "latex" => Some(Self::Latex),
            "docx" => Some(Self::Docx),
            "odt" => Some(Self::Odt),
            _ => None,
        }
    }
}

/// Supported code/programming language formats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CodeFormat {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    C,
    Cpp,
    Java,
    Go,
    Ruby,
    Php,
    Swift,
    Kotlin,
    Scala,
    Haskell,
    Shell,
    Sql,
}

impl CodeFormat {
    /// Get format from file extension
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_lowercase().as_str() {
            "rs" => Some(Self::Rust),
            "py" | "pyw" => Some(Self::Python),
            "js" | "mjs" | "cjs" => Some(Self::JavaScript),
            "ts" | "tsx" => Some(Self::TypeScript),
            "c" | "h" => Some(Self::C),
            "cpp" | "cc" | "cxx" | "hpp" | "hxx" => Some(Self::Cpp),
            "java" => Some(Self::Java),
            "go" => Some(Self::Go),
            "rb" => Some(Self::Ruby),
            "php" => Some(Self::Php),
            "swift" => Some(Self::Swift),
            "kt" | "kts" => Some(Self::Kotlin),
            "scala" | "sc" => Some(Self::Scala),
            "hs" => Some(Self::Haskell),
            "sh" | "bash" | "zsh" => Some(Self::Shell),
            "sql" => Some(Self::Sql),
            _ => None,
        }
    }
}

/// Unified format handler for all supported formats
pub struct FormatHandler;

impl FormatHandler {
    /// Extract media information from a file
    #[cfg(feature = "media-formats")]
    pub fn extract_media_info(path: &Path) -> anyhow::Result<MediaInfo> {
        use std::fs;

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

        let format = MediaFormat::from_extension(ext)
            .ok_or_else(|| anyhow::anyhow!("Unsupported media format: {}", ext))?;

        let metadata = fs::metadata(path)?;
        let size = metadata.len();

        if format.is_image() {
            Self::extract_image_info(path, format, size)
        } else if format.is_video() {
            Self::extract_video_info(path, format, size)
        } else if format.is_audio() {
            Self::extract_audio_info(path, format, size)
        } else {
            Err(anyhow::anyhow!("Unknown media format"))
        }
    }

    #[cfg(feature = "media-formats")]
    fn extract_image_info(
        path: &Path,
        format: MediaFormat,
        size: u64,
    ) -> anyhow::Result<MediaInfo> {
        use image::GenericImageView;

        let img = image::open(path)?;
        let (width, height) = img.dimensions();

        // Convert to raw RGB bytes for VSA encoding
        let raw_data = img.to_rgb8().into_raw();

        Ok(MediaInfo {
            format: format!("{:?}", format).to_lowercase(),
            mime_type: format.mime_type().into(),
            size,
            duration_secs: None,
            width: Some(width),
            height: Some(height),
            sample_rate: None,
            channels: None,
            bit_depth: None,
            frame_rate: None,
            codec: None,
            raw_data,
        })
    }

    #[cfg(feature = "media-formats")]
    fn extract_video_info(
        _path: &Path,
        format: MediaFormat,
        size: u64,
    ) -> anyhow::Result<MediaInfo> {
        // Note: symphonia is primarily an audio library.
        // For full video support, consider adding ffmpeg-next or similar.
        // For now, we provide basic metadata without decoding.

        Ok(MediaInfo {
            format: format!("{:?}", format).to_lowercase(),
            mime_type: format.mime_type().into(),
            size,
            duration_secs: None,
            width: None,
            height: None,
            sample_rate: None,
            channels: None,
            bit_depth: None,
            frame_rate: None,
            codec: None,
            raw_data: Vec::new(),
        })
    }

    #[cfg(feature = "media-formats")]
    fn extract_audio_info(
        path: &Path,
        format: MediaFormat,
        size: u64,
    ) -> anyhow::Result<MediaInfo> {
        use symphonia::core::formats::FormatOptions;
        use symphonia::core::io::MediaSourceStream;
        use symphonia::core::meta::MetadataOptions;
        use symphonia::core::probe::Hint;

        let file = std::fs::File::open(path)?;
        let mss = MediaSourceStream::new(Box::new(file), Default::default());

        let mut hint = Hint::new();
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            hint.with_extension(ext);
        }

        let format_opts = FormatOptions::default();
        let metadata_opts = MetadataOptions::default();

        let probed =
            symphonia::default::get_probe().format(&hint, mss, &format_opts, &metadata_opts)?;

        let reader = probed.format;

        // Find audio track
        let track = reader
            .default_track()
            .ok_or_else(|| anyhow::anyhow!("No audio track found"))?;

        let codec_params = track.codec_params.clone();

        let duration = codec_params.n_frames.and_then(|frames| {
            codec_params
                .sample_rate
                .map(|rate| frames as f64 / rate as f64)
        });

        // For now, we skip decoding audio samples to avoid complex borrow issues.
        // Audio data can be decoded on-demand when needed for VSA encoding.
        // This just extracts metadata for now.
        let raw_samples: Vec<u8> = Vec::new();

        Ok(MediaInfo {
            format: format!("{:?}", format).to_lowercase(),
            mime_type: format.mime_type().into(),
            size,
            duration_secs: duration,
            width: None,
            height: None,
            sample_rate: codec_params.sample_rate,
            channels: codec_params.channels.map(|c| c.count() as u8),
            bit_depth: codec_params.bits_per_sample.map(|b| b as u8),
            frame_rate: None,
            codec: Some(format!("{:?}", codec_params.codec)),
            raw_data: raw_samples,
        })
    }

    /// Stub for when media-formats feature is not enabled
    #[cfg(not(feature = "media-formats"))]
    pub fn extract_media_info(_path: &Path) -> anyhow::Result<MediaInfo> {
        Err(anyhow::anyhow!(
            "Media format support not enabled. Enable the 'media-formats' feature."
        ))
    }

    /// Detect format from file path
    pub fn detect_format(path: &Path) -> FormatType {
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");

        if MediaFormat::from_extension(ext).is_some() {
            FormatType::Media
        } else if DocumentFormat::from_extension(ext).is_some() {
            FormatType::Document
        } else if CodeFormat::from_extension(ext).is_some() {
            FormatType::Code
        } else {
            match ext.to_lowercase().as_str() {
                "json" | "jsonl" | "ndjson" | "csv" | "tsv" | "yaml" | "yml" | "toml" => {
                    FormatType::Structured
                }
                "txt" | "text" | "log" => FormatType::PlainText,
                _ => FormatType::Binary,
            }
        }
    }
}

/// High-level format category
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatType {
    /// Plain text files
    PlainText,
    /// Structured data (JSON, CSV, etc.)
    Structured,
    /// Source code
    Code,
    /// Documents (PDF, HTML, etc.)
    Document,
    /// Media files (images, video, audio)
    Media,
    /// Unknown binary format
    Binary,
}

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

    #[test]
    fn test_media_format_detection() {
        assert!(MediaFormat::from_extension("png").unwrap().is_image());
        assert!(MediaFormat::from_extension("mp4").unwrap().is_video());
        assert!(MediaFormat::from_extension("mp3").unwrap().is_audio());
        assert!(MediaFormat::from_extension("txt").is_none());
    }

    #[test]
    fn test_code_format_detection() {
        assert_eq!(CodeFormat::from_extension("rs"), Some(CodeFormat::Rust));
        assert_eq!(CodeFormat::from_extension("py"), Some(CodeFormat::Python));
        assert_eq!(CodeFormat::from_extension("unknown"), None);
    }

    #[test]
    fn test_format_type_detection() {
        use std::path::PathBuf;

        assert_eq!(
            FormatHandler::detect_format(&PathBuf::from("test.png")),
            FormatType::Media
        );
        assert_eq!(
            FormatHandler::detect_format(&PathBuf::from("test.rs")),
            FormatType::Code
        );
        assert_eq!(
            FormatHandler::detect_format(&PathBuf::from("test.json")),
            FormatType::Structured
        );
    }
}