use hxcfe::{FileSystemId, Hxcfe};
#[cfg(not(target_family = "wasm"))]
use std::io::Read;
#[cfg(target_family = "wasm")]
fn main() {
println!("HxC WASM-Compatible Example");
println!("============================");
println!("This example is designed for WebAssembly targets.");
println!("Build with: cargo build --example wasm_compat --target wasm32-unknown-unknown");
}
#[cfg(not(target_family = "wasm"))]
fn main() {
println!("HxC WASM-Compatible Example");
println!("============================");
let image_data = load_image_data_from_file("tests/EXPERTS.DSK");
let result = process_floppy_image(&image_data);
match result {
Ok(info) => {
println!("\n✅ Image processed successfully!");
println!("{}", info);
}
Err(e) => {
eprintln!("\n❌ Error: {}", e);
}
}
}
fn process_floppy_image(image_data: &[u8]) -> Result<String, String> {
let hxc = Hxcfe::get();
#[cfg(not(target_family = "wasm"))]
{
use std::io::Write;
let temp_path = std::env::temp_dir().join("temp_image.dsk");
let mut file = std::fs::File::create(&temp_path)
.map_err(|e| format!("Failed to create temp file: {}", e))?;
file.write_all(image_data)
.map_err(|e| format!("Failed to write temp file: {}", e))?;
let img = hxc
.load(&temp_path)
.map_err(|e| format!("Failed to load image: {:?}", e))?;
let _ = std::fs::remove_file(&temp_path);
let mut info = String::new();
info.push_str(&format!("Number of tracks: {}\n", img.nb_tracks()));
info.push_str(&format!("Number of sides: {}\n", img.nb_sides()));
info.push_str(&format!("Total size: {} bytes\n", img.size()));
if let Some(interface) = img.interface_mode() {
info.push_str(&format!("Interface mode: {}\n", interface.name()));
} else {
info.push_str("Interface mode: Unknown\n");
}
let fs_manager = hxc
.file_system_manager()
.ok_or_else(|| "Failed to init FS manager".to_string())?;
fs_manager.select_fs(FileSystemId::Atari720KbFat12);
let mount_ret = fs_manager.mount(&img);
if mount_ret >= 0 {
info.push_str("\n✅ Filesystem mounted successfully\n");
match fs_manager.open_dir("/") {
Ok(dir) => {
info.push_str("Root directory contents:\n");
let mut count = 0;
loop {
match dir.read() {
Ok(entry) => {
let type_char = if entry.is_dir() { "d" } else { "-" };
info.push_str(&format!(
" {} {:8} {}\n",
type_char,
entry.size(),
entry.entry_name()
));
count += 1;
}
Err(_) => break,
}
}
dir.close();
info.push_str(&format!("Total: {} entries\n", count));
}
Err(e) => {
info.push_str(&format!("⚠️ Failed to open root directory: {}\n", e));
}
}
fs_manager.umount();
} else {
info.push_str(&format!(
"⚠️ Failed to mount filesystem (code: {})\n",
mount_ret
));
}
Ok(info)
}
#[cfg(target_family = "wasm")]
{
Ok(format!(
"WASM mode: {} bytes of image data received\nWASM memory-based loading would process this data...",
image_data.len()
))
}
}
#[cfg(not(target_family = "wasm"))]
fn load_image_data_from_file(path: &str) -> Vec<u8> {
match std::fs::File::open(path) {
Ok(mut file) => {
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("Failed to read file");
buffer
}
Err(_) => {
eprintln!("⚠️ Test file '{}' not found, using empty buffer", path);
vec![0u8; 737280] }
}
}
#[cfg(target_family = "wasm")]
fn load_image_data_from_file(_path: &str) -> Vec<u8> {
eprintln!("WASM: Image data should be provided by JavaScript FileReader API");
vec![0u8; 737280] }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process_empty_buffer() {
let empty_data = vec![0u8; 737280]; let result = process_floppy_image(&empty_data);
assert!(result.is_ok() || result.is_err()); }
#[test]
fn test_wasm_compat_api() {
let hxc = Hxcfe::get();
assert!(hxc.version().len() > 0);
}
}