tpt_gpu_model_optimizer/export/
detect.rs1use std::fs::File;
4use std::io::Read;
5use std::path::Path;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ModelFormat {
10 Tptf,
11 Gguf,
12 Exl2,
13 Unknown,
14}
15
16pub fn detect(path: &Path) -> ModelFormat {
18 let Ok(mut f) = File::open(path) else {
19 return ModelFormat::Unknown;
20 };
21 let mut magic = [0u8; 4];
22 if f.read_exact(&mut magic).is_err() {
23 return ModelFormat::Unknown;
24 }
25 match &magic {
26 b"TPTF" => ModelFormat::Tptf,
27 b"GGUF" => ModelFormat::Gguf,
28 [b'{', ..] if path.extension().and_then(|e| e.to_str()) == Some("exl2") => {
31 ModelFormat::Exl2
32 }
33 _ => ModelFormat::Unknown,
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40 use std::io::Write;
41
42 #[test]
43 fn detects_tptf() {
44 let dir = std::env::temp_dir();
45 let p = dir.join("test_detect.tptf");
46 let mut f = File::create(&p).unwrap();
47 f.write_all(b"TPTF\x01\x00\x00\x00").unwrap();
48 drop(f);
49 assert_eq!(detect(&p), ModelFormat::Tptf);
50 std::fs::remove_file(p).ok();
51 }
52
53 #[test]
54 fn detects_gguf() {
55 let dir = std::env::temp_dir();
56 let p = dir.join("test_detect.gguf");
57 let mut f = File::create(&p).unwrap();
58 f.write_all(b"GGUF\x03\x00\x00\x00").unwrap();
59 drop(f);
60 assert_eq!(detect(&p), ModelFormat::Gguf);
61 std::fs::remove_file(p).ok();
62 }
63
64 #[test]
65 fn unknown_for_empty() {
66 let dir = std::env::temp_dir();
67 let p = dir.join("test_detect_empty.bin");
68 File::create(&p).unwrap();
69 assert_eq!(detect(&p), ModelFormat::Unknown);
70 std::fs::remove_file(p).ok();
71 }
72}