//! 字根 → letter lookup, backed by a compile-time `phf::Map<char, u8>`
//! generated by `build.rs` from `data/zigen86.txt`.

include!(concat!(env!("OUT_DIR"), "/zigen.phf.rs"));

/// Look up the key letter (lowercase ASCII) for a 字根.
///
/// Compiles to a perfect-hash probe with no allocation, no synchronization.
#[inline]
pub fn lookup(zigen: char) -> Option<u8> {
    ZIGEN.get(&zigen).copied()
}

/// Iterate all (字根, key letter) pairs in the embedded table.
/// Order is unspecified (PHF internal).
pub fn iter() -> impl Iterator<Item = (char, u8)> + 'static {
    ZIGEN.entries().map(|(k, v)| (*k, *v))
}

#[doc(hidden)]
pub fn entries_for_test() -> usize {
    ZIGEN.len()
}

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

    #[test]
    fn parses_canonical_25_plus_extras() {
        assert!(entries_for_test() >= 25);
        assert_eq!(lookup('王'), Some(b'g'));
        assert_eq!(lookup('土'), Some(b'f'));
        assert_eq!(lookup('大'), Some(b'd'));
        assert_eq!(lookup('纟'), Some(b'x'));
        assert_eq!(lookup('火'), Some(b'o'));
        assert_eq!(lookup('一'), Some(b'g'));
        assert_eq!(lookup('丨'), Some(b'h'));
        assert_eq!(lookup('丿'), Some(b't'));
        assert_eq!(lookup('丶'), Some(b'y'));
        assert_eq!(lookup('乙'), Some(b'n'));
    }

    #[test]
    fn unknown_returns_none() {
        assert_eq!(lookup('Z'), None);
        assert_eq!(lookup('🍌'), None);
    }
}
