use anyhow::{anyhow, Result};
use once_cell::sync::OnceCell;
static GAME: OnceCell<Game> = OnceCell::new();
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Game {
#[cfg(feature = "ck3")]
Ck3,
#[cfg(feature = "vic3")]
Vic3,
#[cfg(feature = "imperator")]
Imperator,
}
impl Game {
pub fn set(game: Game) -> Result<()> {
GAME.set(game).map_err(|_| anyhow!("tried to set game type twice"))?;
Ok(())
}
#[allow(clippy::self_named_constructors)] #[allow(unreachable_code)]
pub fn game() -> Game {
#[cfg(all(feature = "ck3", not(feature = "vic3"), not(feature = "imperator")))]
return Game::Ck3;
#[cfg(all(feature = "vic3", not(feature = "ck3"), not(feature = "imperator")))]
return Game::Vic3;
#[cfg(all(feature = "imperator", not(feature = "ck3"), not(feature = "vic3")))]
return Game::Imperator;
*GAME.get().expect("internal error: don't know which game we are validating")
}
pub(crate) fn is_ck3() -> bool {
#[cfg(not(feature = "ck3"))]
return false;
#[cfg(all(feature = "ck3", not(feature = "vic3"), not(feature = "imperator")))]
return true;
#[cfg(all(feature = "ck3", any(feature = "vic3", feature = "imperator")))]
return GAME.get() == Some(Game::Ck3);
}
pub(crate) fn is_vic3() -> bool {
#[cfg(not(feature = "vic3"))]
return false;
#[cfg(all(feature = "vic3", not(feature = "ck3"), not(feature = "imperator")))]
return true;
#[cfg(all(feature = "vic3", any(feature = "ck3", feature = "imperator")))]
return GAME.get() == Some(Game::Vic3);
}
pub(crate) fn is_imperator() -> bool {
#[cfg(not(feature = "imperator"))]
return false;
#[cfg(all(feature = "imperator", not(feature = "ck3"), not(feature = "vic3")))]
return true;
#[cfg(all(feature = "imperator", any(feature = "ck3", feature = "vic3")))]
return GAME.get() == Some(Game::Imperator);
}
}