#![allow(clippy::similar_names)]
use nulid::Nulid;
fn main() -> Result<(), Box<dyn core::error::Error>> {
println!("NULID WASM Example");
println!("===================\n");
println!("1. Generating NULID with WASM timing...");
let nulid1 = Nulid::new()?;
println!(" NULID: {nulid1}");
println!(" Timestamp (ns): {}", nulid1.nanos());
println!();
println!("2. Generating another NULID...");
let nulid2 = Nulid::new()?;
println!(" NULID: {nulid2}");
println!(" Timestamp (ns): {}", nulid2.nanos());
println!();
println!("3. Verifying monotonic ordering with WASM timing...");
if nulid1 < nulid2 {
println!(" {nulid1} < {nulid2} ok");
} else {
println!(" {nulid1} >= {nulid2}");
}
println!();
println!("4. Demonstrating nanosecond precision...");
let nanos1 = nulid1.nanos();
let nanos2 = nulid2.nanos();
let diff_ns = nanos2 - nanos1;
println!(" Time difference: {diff_ns} nanoseconds");
println!(
" Has sub-millisecond precision: {}",
if diff_ns < 1_000_000 { "yes" } else { "no" }
);
println!();
println!("5. Generating multiple NULIDs with WASM timing...");
let mut ids = Vec::new();
for i in 0..5 {
let nulid = Nulid::new()?;
println!(" [{:02}] {} (ns: {})", i + 1, nulid, nulid.nanos());
ids.push(nulid);
}
println!();
println!("6. Verifying uniqueness and sorting...");
let is_unique = ids.windows(2).all(|w| w[0] != w[1]);
let is_sorted = ids.windows(2).all(|w| w[0] < w[1]);
println!(" All unique: {}", if is_unique { "yes" } else { "no" });
println!(" Sorted: {}", if is_sorted { "yes" } else { "no" });
println!();
println!("7. String serialization (WASM-compatible)...");
let nulid_str = nulid1.to_string();
println!(" Original: {nulid_str}");
let parsed: Nulid = nulid_str.parse()?;
println!(" Parsed: {parsed}");
println!(" Match: {}", if nulid1 == parsed { "ok" } else { "FAIL" });
println!();
println!("WASM example completed successfully!");
Ok(())
}