Function preview

Source
pub fn preview<const N: usize>(
    inventory: &Inventory<N>,
    inputs: &[(u32, u32)],
    outputs: &[Stack],
) -> CraftPreview
Expand description

Inspect the same trusted recipe accepted by craft without changing inventory.

Availability includes consuming all inputs before checking outputs, so freed slots can be reused and outputs cannot pay for missing inputs. All ingredient shortfalls are reported, including those after the first failed removal. Duplicate input totals use u64; overflowing output amounts yield LoopError::Capacity, just as they do in craft.

use ferravex_play::gameplay::{
    LoopError,
    crafting::{craft, preview, MissingIngredient},
    inventory::{Inventory, Stack},
};

let mut inventory = Inventory::<1>::new()?;
inventory.add(Stack { item: 1, amount: 3, limit: 10 })?;
let inputs = [(1, 2), (1, 3)];
let outputs = [Stack { item: 2, amount: 1, limit: 5 }];
let report = preview(&inventory, &inputs, &outputs);
assert!(!report.is_available());
assert_eq!(report.result(), Err(LoopError::Unavailable));
assert_eq!(report.missing_ingredients(), &[MissingIngredient {
    item: 1, required: 5, available: 3, missing: 2,
}]);
assert_eq!(inventory.count(1), 3);

inventory.add(Stack { item: 1, amount: 2, limit: 10 })?;
let ready = preview(&inventory, &inputs, &outputs);
assert!(ready.is_available()); // Spending all ore frees the only slot.
assert!(ready.missing_ingredients().is_empty());
assert_eq!(craft(&mut inventory, &inputs, &outputs), ready.result());
assert_eq!(inventory.count(2), 1);