ctb_formats_utilities/
platform.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Platform operating system compatibility helpers and host platform inference.
21
22#[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/// Determines whether a format's associated operating system is compatible
32/// with a target operating system context.
33///
34/// Uses format implication hierarchies declared via `@implies(...)` directives
35/// in the format database. A candidate OS matches a target OS if they are identical
36/// or if either directly or transitively implies the other.
37#[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/// Infers the host platform operating system as an authoritative `FormatId`.
45#[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}