ctb_formats_utilities/
platform.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate::format_id::FormatId;
30
31#[must_use]
38pub fn is_os_match(candidate_os: FormatId, target_os: FormatId) -> bool {
39 candidate_os == target_os
40 || candidate_os.implies().contains(&target_os)
41 || target_os.implies().contains(&candidate_os)
42}
43
44#[must_use]
46pub fn current_platform_os() -> Option<FormatId> {
47 #[cfg(target_os = "macos")]
48 {
49 Some(FormatId::MacOs)
50 }
51 #[cfg(target_os = "windows")]
52 {
53 Some(FormatId::Windows)
54 }
55 #[cfg(target_os = "linux")]
56 {
57 Some(FormatId::GnuLinux)
58 }
59 #[cfg(all(unix, not(any(target_os = "macos", target_os = "linux"))))]
60 {
61 Some(FormatId::Unix)
62 }
63 #[cfg(not(any(target_os = "macos", target_os = "windows", unix)))]
64 {
65 None
66 }
67}
68
69#[cfg(test)]
70#[allow(
71 clippy::panic,
72 clippy::expect_used,
73 clippy::unwrap_used,
74 clippy::unwrap_in_result,
75 clippy::panic_in_result_fn,
76 clippy::indexing_slicing,
77 clippy::arithmetic_side_effects,
78 reason = "Standard repository test boilerplate"
79)]
80mod tests {
81 use super::*;
82
83 #[crate::ctb_test]
84 fn test_os_match_matrix() {
85 assert!(is_os_match(FormatId::Linux, FormatId::Unix));
86 assert!(is_os_match(FormatId::Unix, FormatId::Linux));
87 assert!(is_os_match(FormatId::MacOs, FormatId::MacOsDarwin));
88 assert!(is_os_match(FormatId::MacOsDarwin, FormatId::MacOs));
89 assert!(is_os_match(FormatId::WinClassic, FormatId::Windows));
90 assert!(is_os_match(FormatId::Windows, FormatId::WinClassic));
91 assert!(is_os_match(FormatId::WinNt, FormatId::Windows));
92 assert!(is_os_match(FormatId::Windows, FormatId::WinNt));
93 assert!(is_os_match(FormatId::Linux, FormatId::GnuLinux));
94 assert!(is_os_match(FormatId::GnuLinux, FormatId::Linux));
95 assert!(is_os_match(FormatId::GnuLinux, FormatId::Unix));
96 assert!(is_os_match(FormatId::Unix, FormatId::GnuLinux));
97 assert!(is_os_match(FormatId::FreeBsd, FormatId::Unix));
98 assert!(is_os_match(FormatId::Unix, FormatId::FreeBsd));
99 assert!(is_os_match(FormatId::Debian, FormatId::Linux));
100 assert!(is_os_match(FormatId::Debian, FormatId::Unix));
101 assert!(!is_os_match(FormatId::Windows, FormatId::Linux));
102 assert!(!is_os_match(FormatId::FreeBsd, FormatId::Linux));
103 assert!(!is_os_match(FormatId::Linux, FormatId::FreeBsd));
104 assert!(!is_os_match(FormatId::Windows, FormatId::Unix));
105 assert!(!is_os_match(FormatId::MacOs, FormatId::Linux));
106 }
107
108 #[crate::ctb_test]
109 fn test_current_platform_os_is_consistent() {
110 let os = current_platform_os();
111 #[cfg(target_os = "linux")]
112 assert_eq!(os, Some(FormatId::GnuLinux));
113 #[cfg(target_os = "macos")]
114 assert_eq!(os, Some(FormatId::MacOs));
115 #[cfg(target_os = "windows")]
116 assert_eq!(os, Some(FormatId::Windows));
117 }
118}