switch.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //! The `VICTORY_*` on/off switches (`VICTORY_D3D12_DEBUG=1` and its siblings).
  2. use std::ffi::OsStr;
  3. /// Whether a switch's value turns it on: it is set, and is not `0`.
  4. ///
  5. /// A switch used to be on whenever its variable existed, so `VICTORY_D3D12_WARP=0` (typed to switch it off, or left
  6. /// over from comparing two runs) still selected the software adapter. Any other value keeps the old meaning.
  7. fn value_is_on(value: Option<&OsStr>) -> bool {
  8. value.is_some_and(|v| v != "0")
  9. }
  10. /// Whether the environment variable `name` switches something on.
  11. pub(crate) fn on(name: &str) -> bool {
  12. value_is_on(std::env::var_os(name).as_deref())
  13. }
  14. #[cfg(test)]
  15. mod tests {
  16. use super::value_is_on;
  17. use std::ffi::OsStr;
  18. #[test]
  19. fn unset_is_off() {
  20. assert!(!value_is_on(None));
  21. }
  22. #[test]
  23. fn zero_is_off() {
  24. assert!(!value_is_on(Some(OsStr::new("0"))));
  25. }
  26. #[test]
  27. fn one_is_on() {
  28. assert!(value_is_on(Some(OsStr::new("1"))));
  29. }
  30. #[test]
  31. fn other_values_keep_their_old_meaning() {
  32. // Documented as `=1`; anything else that was on before stays on: only `0` is new.
  33. for value in ["", "true", "yes", "2", "00", " 0", "0 "] {
  34. assert!(value_is_on(Some(OsStr::new(value))), "{value:?}");
  35. }
  36. }
  37. }