Skip to main content

krypt_core/paths/
platform.rs

1//! OS detection.
2
3use std::fmt;
4
5/// Operating system we're resolving paths for.
6///
7/// Auto-detected from `cfg!(target_os)` in [`Platform::current`], but can
8/// be set explicitly (mainly for tests).
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum Platform {
11    /// Linux (and Linux-likes — BSD, etc., until we have a reason to split).
12    Linux,
13    /// macOS.
14    Macos,
15    /// Windows.
16    Windows,
17}
18
19impl Platform {
20    /// The platform the current binary is running on.
21    ///
22    /// Resolves at compile time via `cfg!`. Falls back to [`Platform::Linux`]
23    /// on platforms we don't have a dedicated variant for, with the
24    /// understanding that the platform-gated vars (`${WIN_*}`, `${MAC_*}`)
25    /// will simply not resolve there.
26    pub const fn current() -> Self {
27        if cfg!(target_os = "windows") {
28            Platform::Windows
29        } else if cfg!(target_os = "macos") {
30            Platform::Macos
31        } else {
32            Platform::Linux
33        }
34    }
35
36    /// String slug used in `.krypt.toml` (`platform = "linux"`, etc.).
37    pub const fn as_str(self) -> &'static str {
38        match self {
39            Platform::Linux => "linux",
40            Platform::Macos => "macos",
41            Platform::Windows => "windows",
42        }
43    }
44}
45
46impl fmt::Display for Platform {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self.as_str())
49    }
50}