athena_rs 0.75.4

WIP Database API gateway
Documentation
//! Helpers that map table names to their resource ID columns via `table_id_map.yaml`.
use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_yaml::from_str;
use std::collections::HashMap;

static TABLE_ID_YAML: &str = include_str!("../../../../table_id_map.yaml");

#[derive(Debug, Deserialize)]
/// Deserializes the YAML mapping between table names and ID columns.
struct TableIdConfig {
    /// Entries loaded from `table_id_map.yaml`.
    mappings: HashMap<String, String>,
}

/// Lazy-loaded map of table names -> resource ID column names.
static TABLE_ID_MAP: Lazy<HashMap<String, String>> = Lazy::new(|| {
    let cfg: TableIdConfig = from_str(TABLE_ID_YAML).expect("Invalid table_id_map.yaml");
    cfg.mappings
});

/// Retrieves the ID column name for the given table, returning `"id"` when missing.
///
/// # Parameters
/// - `table_name`: Table to look up inside `table_id_map.yaml`.
///
/// # Returns
/// Resource ID column name used during UUID enrichment.
///
/// # Example
/// ```text
/// get_resource_id_key("users") // -> "user_id"
/// ```
pub fn get_resource_id_key(table_name: &str) -> &str {
    TABLE_ID_MAP
        .get(table_name)
        .map(|s| s.as_str())
        .unwrap_or("id")
}

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

    #[test]
    fn returns_user_id_for_users_table() {
        assert_eq!(get_resource_id_key("users"), "user_id");
    }

    #[test]
    fn defaults_to_id_when_unknown_table() {
        assert_eq!(get_resource_id_key("abracadabra_table"), "id");
    }
}