Skip to main content

waypoints/
waypoints.rs

1use halbu::waypoints::{Waypoint, WaypointError};
2use halbu::{Save, Strictness};
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let bytes = std::fs::read("assets/test/Joe.d2s")?;
6    let parsed = Save::parse(&bytes, Strictness::Strict)?;
7    let mut save = parsed.save;
8
9    // Read by index.
10    let catacombs_was_unlocked = save.waypoints.hell.act1.get_by_index(8)?;
11    println!("Hell Act I / Catacombs unlocked: {catacombs_was_unlocked}");
12
13    // Set by waypoint id.
14    save.waypoints.hell.act1.set(Waypoint::Catacombs, true)?;
15    let catacombs_is_unlocked = save.waypoints.hell.act1.get(Waypoint::Catacombs)?;
16    println!("Hell Act I / Catacombs now unlocked: {catacombs_is_unlocked}");
17
18    // Set by index.
19    save.waypoints.normal.act4.set_by_index(2, true)?;
20    println!(
21        "Normal Act IV / River of Flames unlocked: {}",
22        save.waypoints.normal.act4.get_by_index(2)?
23    );
24
25    // Bulk update one difficulty.
26    save.waypoints.nightmare.set_all(true);
27    println!(
28        "Nightmare Act II / Sewers unlocked after set_all: {}",
29        save.waypoints.nightmare.act2.get_by_index(1)?
30    );
31
32    // Wrong-act usage returns an explicit error.
33    match save.waypoints.normal.act1.set(Waypoint::LutGholein, true) {
34        Err(WaypointError::WrongAct { waypoint, expected, actual }) => {
35            println!("WrongAct: {waypoint:?} belongs to {actual:?}, expected {expected:?}.")
36        }
37        Ok(_) => println!("Unexpected: wrong-act set was accepted."),
38        Err(error) => println!("Unexpected waypoint error: {error}"),
39    }
40
41    // Out-of-range reads return an explicit error.
42    match save.waypoints.normal.act4.get_by_index(3) {
43        Err(WaypointError::IndexOutOfRange { act, index, max_index }) => {
44            println!("IndexOutOfRange: {act:?} index {index} (max {max_index}).")
45        }
46        Ok(_) => println!("Unexpected: out-of-range read was accepted."),
47        Err(error) => println!("Unexpected waypoint error: {error}"),
48    }
49
50    Ok(())
51}