wow_spells 0.2.0

Spell definitions for World of Warcraft game servers
Documentation
#[allow(clippy::too_many_arguments)]
pub(crate) mod constructors;
#[allow(non_upper_case_globals)]
mod data;

// AUTOGENERATED_START
pub use wow_world_base::tbc::{
    AuraMod, Reagent, Spell, SpellEffects, Totem, TotemCategory,
};

/// Looks up spells and returns if found.
///
/// Prefer using this over [`all_spells`] since this utilizes a lookup array for very fast lookup.
pub const fn lookup_spell(id: u32) -> Option<&'static Spell> {
    if id < 1 || id > 53085 {
        return None;
    }

    let index = data::Z________LOOKUP[(id - 1) as usize];
    if index == u16::MAX || index as usize > (all_spells().len() - 1) {
        None
    } else {
        Some(&all_spells()[index as usize])
    }
}

/// Returns all spells.
///
/// Prefer using [`lookup_spell`] since it incorporates optimizations for lookup speed.
pub const fn all_spells() -> &'static [Spell] {
    data::Z________DATA
}

/// Returns all spells that contain `needle` in the name. The search is case insensitive.
pub fn lookup_spells_by_name(needle: &str) -> impl Iterator<Item = &'static Spell> + '_ {
    all_spells().iter().filter(move |spell| {
        let lower = spell.spell_name().to_ascii_lowercase();
        lower.contains(needle)
    })
}

/// Returns the first spell that contains `needle` in the name. The search is case insensitive.
pub fn lookup_spell_by_name(needle: &str) -> Option<&'static Spell> {
    let needle = needle.to_ascii_lowercase();

    for spell in all_spells() {
        let lower = spell.spell_name().to_ascii_lowercase();
        if lower.contains(&needle) {
            return Some(spell)
        }

    }

    None
}

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

    #[test]
    fn tests() {
        assert!(lookup_spell(u32::MIN).is_none());
        assert!(lookup_spell(u32::MAX).is_none());

        const MIN: u32 = 1;
        const MAX: u32 = 53085;
        assert_eq!(lookup_spell(MIN).unwrap().entry(), MIN);
        assert_eq!(lookup_spell(MAX).unwrap().entry(), MAX);

        assert!(lookup_spell(MIN - 1).is_none());
        assert!(lookup_spell(MAX + 1).is_none());

        for i in 0..=MAX + 10 {
            match lookup_spell(i) {
                None => {}
                Some(e) => assert_eq!(i, e.entry()),
            }
        }
    }
}
// AUTOGENERATED_END