Skip to main content

duc/
lib.rs

1#[allow(unused_imports)]
2#[allow(non_camel_case_types)]
3#[allow(non_snake_case)]
4#[allow(non_upper_case_globals)]
5pub mod parse;
6pub mod serialize;
7pub mod types;
8
9// SQLite storage layer and high-level document API
10pub mod db;
11pub mod api;
12
13#[cfg(test)]
14mod tests {
15    use std::fs;
16    use std::path::Path;
17
18    // Initialize logging for tests
19    fn init_logging() {
20        let _ = env_logger::builder()
21            .filter_level(log::LevelFilter::Info)
22            .is_test(true)
23            .try_init();
24    }
25
26    #[test]
27    fn test_parse_with_logging() {
28        init_logging();
29
30        // Try to load a test DUC file from assets folder
31        let test_file_path = Path::new("assets/testing/duc-files/plot_elements.duc");
32
33        if test_file_path.exists() {
34            match fs::read(test_file_path) {
35                Ok(data) => {
36                    println!("Loaded test file: {} bytes", data.len());
37
38                    // Call the parse function to trigger the logging
39                    match crate::parse::parse(&data) {
40                        Ok(parsed) => {
41                            println!("✅ Successfully parsed DUC file");
42                            println!("Layers count: {}", parsed.layers.len());
43                        }
44                        Err(e) => {
45                            println!("❌ Parse error: {}", e);
46                        }
47                    }
48                }
49                Err(e) => {
50                    println!("❌ Failed to read test file: {}", e);
51                }
52            }
53        } else {
54            println!("❌ Test file not found: {}", test_file_path.display());
55            // Create minimal test data to at least test the parsing pipeline
56            let minimal_data = vec![0u8; 100];
57            match crate::parse::parse(&minimal_data) {
58                Ok(_) => println!("✅ Minimal parse succeeded"),
59                Err(e) => println!("❌ Minimal parse failed: {}", e),
60            }
61        }
62    }
63
64    #[test]
65    fn test_serialize_roundtrip_compressed() {
66        init_logging();
67
68        let test_file_path = Path::new(env!("CARGO_MANIFEST_DIR"))
69            .join("../../assets/testing/duc-files/universal.duc");
70        if !test_file_path.exists() {
71            println!("Skipped — test file not found: {}", test_file_path.display());
72            return;
73        }
74
75        let original = fs::read(test_file_path).expect("read test file");
76        let parsed = crate::parse::parse(&original).expect("parse original");
77
78        let compressed = crate::serialize::serialize(&parsed).expect("serialize");
79
80        assert!(!compressed.is_empty(), "serialized output must not be empty");
81        println!(
82            "original={} bytes, compressed={} bytes ({:.1}%)",
83            original.len(),
84            compressed.len(),
85            compressed.len() as f64 / original.len() as f64 * 100.0
86        );
87
88        let reparsed = crate::parse::parse(&compressed).expect("parse compressed");
89        assert_eq!(parsed.elements.len(), reparsed.elements.len());
90        assert_eq!(parsed.layers.len(), reparsed.layers.len());
91        assert_eq!(parsed.blocks.len(), reparsed.blocks.len());
92        println!("Round-trip OK — {} elements preserved", reparsed.elements.len());
93    }
94}