pogo-masterfile 0.1.0

Runtime API for the Pokémon GO masterfile. Loads, indexes, and queries entries with per-group narrow-typed accessors.
Documentation
//! Generated from Pokémon GO masterfile — accessor for "partyPlayGeneralSettings".

use std::collections::HashMap;

use pogo_masterfile_types::{
    MasterfileEntry,
    party_play_general_settings::{
        PartyPlayGeneralSettingsEntry, PartyPlayGeneralSettingsTemplateId,
    },
};

pub struct PartyPlayGeneralSettingsAccessor<'a> {
    pub(crate) entries: &'a [MasterfileEntry],
    pub(crate) index: &'a HashMap<PartyPlayGeneralSettingsTemplateId, usize>,
    pub(crate) order: &'a [usize],
}

impl<'a> PartyPlayGeneralSettingsAccessor<'a> {
    /// Look up an entry by its templateId. Accepts either the typed
    /// `PartyPlayGeneralSettingsTemplateId` enum variant (compile-time validated) or
    /// `&str` (runtime-parsed via `FromStr`). Returns `None` if the string
    /// fails to parse OR no entry exists for the ID.
    pub fn get<I>(&self, id: I) -> Option<&'a PartyPlayGeneralSettingsEntry>
    where
        I: TryInto<PartyPlayGeneralSettingsTemplateId>,
    {
        let typed = id.try_into().ok()?;
        let idx = *self.index.get(&typed)?;
        match &self.entries[idx] {
            MasterfileEntry::PartyPlayGeneralSettings(e) => Some(e),
            _ => None,
        }
    }

    pub fn has<I>(&self, id: I) -> bool
    where
        I: TryInto<PartyPlayGeneralSettingsTemplateId>,
    {
        self.get(id).is_some()
    }

    pub fn iter(&self) -> impl Iterator<Item = &'a PartyPlayGeneralSettingsEntry> + '_ {
        self.order
            .iter()
            .filter_map(|&idx| match &self.entries[idx] {
                MasterfileEntry::PartyPlayGeneralSettings(e) => Some(e),
                _ => None,
            })
    }

    pub fn template_ids(&self) -> impl Iterator<Item = PartyPlayGeneralSettingsTemplateId> + '_ {
        self.index.keys().copied()
    }

    pub fn len(&self) -> usize {
        self.order.len()
    }

    pub fn is_empty(&self) -> bool {
        self.order.is_empty()
    }
}