ck3_history_extractor/parser/
tokens.rs

1use jomini::binary::TokenResolver;
2
3use phf::Map;
4
5// This is generated at build time by the build.rs script.
6#[cfg(feature = "tokens")]
7include!(concat!(env!("OUT_DIR"), "/token_data.rs"));
8
9/// A struct that translates tokens to strings
10/// Essentially a wrapper around a static map that is generated at build time.
11pub struct TokenTranslator {
12    tokens: Option<&'static Map<u16, &'static str>>,
13}
14
15impl TokenResolver for TokenTranslator {
16    fn resolve(&self, token: u16) -> Option<&str> {
17        self.tokens.as_ref().unwrap().get(&token).map(|v| &**v)
18    }
19
20    fn is_empty(&self) -> bool {
21        self.tokens.as_ref().unwrap().is_empty()
22    }
23}
24
25/// A static instance of the token translator.
26/// This is used to resolve tokens in the game data on runtime.
27/// If the `tokens` feature is not enabled, this will be a no-op.
28pub const TOKEN_TRANSLATOR: TokenTranslator = {
29    #[cfg(feature = "tokens")]
30    {
31        TokenTranslator {
32            tokens: Some(&TOKENS),
33        }
34    }
35    #[cfg(not(feature = "tokens"))]
36    {
37        TokenTranslator { tokens: None }
38    }
39};