use std::fmt;
use bevy::prelude::Resource;
#[derive(Resource, Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum AppStoreEnvironment {
#[default]
Pending,
Xcode,
Sandbox,
Production,
Unavailable,
Unknown,
}
impl AppStoreEnvironment {
pub const fn is_resolved(self) -> bool {
!matches!(self, Self::Pending)
}
pub const fn is_production(self) -> bool {
matches!(self, Self::Production)
}
}
impl fmt::Display for AppStoreEnvironment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Pending => "pending",
Self::Xcode => "xcode",
Self::Sandbox => "sandbox",
Self::Production => "production",
Self::Unavailable => "unavailable",
Self::Unknown => "unknown",
})
}
}
pub(crate) const fn environment_from_raw(value: i32) -> AppStoreEnvironment {
match value {
0 => AppStoreEnvironment::Pending,
1 => AppStoreEnvironment::Xcode,
2 => AppStoreEnvironment::Sandbox,
3 => AppStoreEnvironment::Production,
4 => AppStoreEnvironment::Unavailable,
_ => AppStoreEnvironment::Unknown,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wire_values_are_explicit_and_unknown_values_fail_closed() {
assert_eq!(environment_from_raw(0), AppStoreEnvironment::Pending);
assert_eq!(environment_from_raw(1), AppStoreEnvironment::Xcode);
assert_eq!(environment_from_raw(2), AppStoreEnvironment::Sandbox);
assert_eq!(environment_from_raw(3), AppStoreEnvironment::Production);
assert_eq!(environment_from_raw(4), AppStoreEnvironment::Unavailable);
assert_eq!(environment_from_raw(5), AppStoreEnvironment::Unknown);
assert_eq!(environment_from_raw(i32::MAX), AppStoreEnvironment::Unknown);
}
#[test]
fn only_production_enables_production_configuration() {
assert!(AppStoreEnvironment::Production.is_production());
for environment in [
AppStoreEnvironment::Pending,
AppStoreEnvironment::Xcode,
AppStoreEnvironment::Sandbox,
AppStoreEnvironment::Unavailable,
AppStoreEnvironment::Unknown,
] {
assert!(!environment.is_production(), "{environment}");
}
}
#[test]
fn pending_is_the_only_unresolved_state() {
assert!(!AppStoreEnvironment::Pending.is_resolved());
for environment in [
AppStoreEnvironment::Xcode,
AppStoreEnvironment::Sandbox,
AppStoreEnvironment::Production,
AppStoreEnvironment::Unavailable,
AppStoreEnvironment::Unknown,
] {
assert!(environment.is_resolved(), "{environment}");
}
}
}