use minlz::{decode, encode};
use std::fs;
use std::io::Write;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("S2 Rust <-> Go Interoperability Demo\n");
let test_data = b"This data will be compressed by Rust and can be decompressed by Go!";
println!("Original data:");
println!(" Size: {} bytes", test_data.len());
println!(" Content: {:?}\n", std::str::from_utf8(test_data)?);
let compressed = encode(test_data);
println!("Compressed with Rust:");
println!(" Size: {} bytes", compressed.len());
println!(
" Hex: {}\n",
hex::encode(&compressed[..compressed.len().min(40)])
);
let filename = "rust_compressed.s2";
let mut file = fs::File::create(filename)?;
file.write_all(&compressed)?;
println!(
"Saved to {} (Go can decompress this with s2.Decode)\n",
filename
);
let decompressed = decode(&compressed)?;
assert_eq!(test_data, &decompressed[..]);
println!("✓ Round-trip test in Rust: PASSED");
println!("\n--- Go Interoperability ---");
println!("To test with Go:");
println!("```go");
println!("data, _ := os.ReadFile(\"{}\")", filename);
println!("decompressed, _ := s2.Decode(nil, data)");
println!("fmt.Println(string(decompressed))");
println!("```");
Ok(())
}
mod hex {
pub fn encode(data: &[u8]) -> String {
data.iter()
.map(|b| format!("{:02x}", b))
.collect::<Vec<_>>()
.join("")
}
}