amble_engine 0.67.0

Data-first interactive fiction engine and REPL for worlds authored in the Amble DSL.
Documentation
//! Deterministic ID helpers for world objects.
//!
//! IDs are now the string symbols from content files, so token -> ID is
//! a simple identity mapping. Namespaces remain for compatibility with
//! older loader call sites.

use crate::{Id, RoomId};
use std::sync::atomic::{AtomicUsize, Ordering};

pub const NAMESPACE_ROOM: &str = "room";
pub const NAMESPACE_ITEM: &str = "item";
pub const NAMESPACE_CHARACTER: &str = "character";

static NEXT_ID: AtomicUsize = AtomicUsize::new(1);

/// Generate a unique, human-readable id for tests and tooling.
pub fn new_id() -> Id {
    let next = NEXT_ID.fetch_add(1, Ordering::Relaxed);
    format!("id_{next}")
}

/// Generate a new, unique `RoomId` (typically only used for tests.)
pub fn new_room_id() -> RoomId {
    let next = NEXT_ID.fetch_add(1, Ordering::Relaxed);
    format!("id_{next}").into()
}

/// Convert a token id from content files into a runtime ID.
///
/// # Important Note
/// **This is semi-deprecated.** Earlier versions of the engine generated
/// namespaced UUIDs from the symbols in TOML files. `Id` is now just a type
/// alias for String. (We were having to keep track of the TOML symbols for
/// logging / debugging anyway, so it added a layer of complication without benefit).
/// I'm leaving this call in place for now for compatibility with the older code,
/// and because we could go back to UUIDs or possibly have newtypes (eg ItemId(&str),
/// NpcId(&str) etc) that could be handled here.
pub fn symbol_to_id(namespace: impl AsRef<str>, token: &str) -> Id {
    let _ = namespace.as_ref();
    token.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn uuid_from_token_is_identity_mapping() {
        let token = "test_item";
        let id = symbol_to_id(NAMESPACE_ITEM, token);
        assert_eq!(id, token);
    }
}