diffai_core/parsers/
mod.rs1mod matlab;
2mod numpy;
3mod pytorch;
4mod safetensors;
5
6pub use matlab::parse_matlab_file;
7pub use numpy::parse_numpy_file;
8pub use pytorch::parse_pytorch_model;
9pub use safetensors::parse_safetensors_model;
10
11use anyhow::{anyhow, Result};
12use serde_json::Value;
13use std::path::Path;
14
15use crate::types::FileFormat;
16
17pub fn detect_format_from_path(path: &Path) -> Result<FileFormat> {
18 match path.extension().and_then(|ext| ext.to_str()) {
19 Some("pt") | Some("pth") => Ok(FileFormat::PyTorch),
20 Some("safetensors") => Ok(FileFormat::Safetensors),
21 Some("npy") | Some("npz") => Ok(FileFormat::NumPy),
22 Some("mat") => Ok(FileFormat::Matlab),
23 _ => {
24 let ext = path
25 .extension()
26 .and_then(|ext| ext.to_str())
27 .unwrap_or("unknown");
28 Err(anyhow!(
29 "Unsupported file format: '{}'. diffai only supports AI/ML file formats: .pt, .pth, .safetensors, .npy, .npz, .mat. For general structured data formats, please use diffx.",
30 ext
31 ))
32 }
33 }
34}
35
36pub fn parse_file_by_format(path: &Path, format: FileFormat) -> Result<Value> {
37 match format {
38 FileFormat::PyTorch => parse_pytorch_model(path),
39 FileFormat::Safetensors => parse_safetensors_model(path),
40 FileFormat::NumPy => parse_numpy_file(path),
41 FileFormat::Matlab => parse_matlab_file(path),
42 }
43}