use grid1d::prelude::*;
use sorted_vec::partial::SortedSet;
fn main() {
type Real = RealNative64StrictFinite;
println!("=== Coords1D Serialization Example ===\n");
let coords = Coords1D::try_from(SortedSet::from_unsorted(vec![
Real::try_new(0.0).unwrap(),
Real::try_new(2.5).unwrap(),
Real::try_new(1.0).unwrap(),
Real::try_new(5.0).unwrap(),
Real::try_new(3.7).unwrap(),
]))
.unwrap();
println!("Original coordinates:");
for (i, coord) in coords.iter().enumerate() {
println!(" [{}] = {}", i, coord);
}
let json = serde_json::to_string(&coords).unwrap();
println!("\nSerialized to JSON:");
println!(" {}", json);
let json_pretty = serde_json::to_string_pretty(&coords).unwrap();
println!("\nSerialized to JSON (pretty):");
println!("{}", json_pretty);
let deserialized: Coords1D<Real> = serde_json::from_str(&json).unwrap();
println!("\nDeserialized coordinates:");
for (i, coord) in deserialized.iter().enumerate() {
println!(" [{}] = {}", i, coord);
}
assert_eq!(coords, deserialized);
println!("\n✓ Coordinates match after round-trip serialization!");
println!("\n=== File I/O Example ===");
let filename = "/tmp/coords1d_example.json";
std::fs::write(filename, json_pretty).unwrap();
println!("\n✓ Saved coordinates to: {}", filename);
let json_from_file = std::fs::read_to_string(filename).unwrap();
let coords_from_file: Coords1D<Real> = serde_json::from_str(&json_from_file).unwrap();
println!("\n✓ Loaded coordinates from file:");
for (i, coord) in coords_from_file.iter().enumerate() {
println!(" [{}] = {}", i, coord);
}
assert_eq!(coords, coords_from_file);
println!("\n✓ File I/O successful!");
std::fs::remove_file(filename).ok();
println!("\n=== Error Handling Example ===");
let empty_json = "[]";
let result: Result<Coords1D<Real>, _> = serde_json::from_str(empty_json);
match result {
Ok(_) => println!("✗ Unexpected success with empty array"),
Err(e) => println!("✓ Correctly rejected empty array: {}", e),
}
println!("\n=== Example Complete ===");
}