ai_agent/utils/
platform.rs1#![allow(dead_code)]
3
4use std::collections::HashMap;
5
6pub const SUPPORTED_PLATFORMS: &[&str] = &["macos", "linux", "windows"];
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum Platform {
12 Macos,
13 Linux,
14 Windows,
15 FreeBSD,
16 Unknown,
17}
18
19impl Platform {
20 pub fn as_str(&self) -> &'static str {
21 match self {
22 Platform::Macos => "macos",
23 Platform::Linux => "linux",
24 Platform::Windows => "windows",
25 Platform::FreeBSD => "freebsd",
26 Platform::Unknown => "unknown",
27 }
28 }
29}
30
31impl From<&str> for Platform {
32 fn from(s: &str) -> Self {
33 match s {
34 "macos" => Platform::Macos,
35 "linux" => Platform::Linux,
36 "windows" => Platform::Windows,
37 "freebsd" => Platform::FreeBSD,
38 _ => Platform::Unknown,
39 }
40 }
41}
42
43pub fn get_platform() -> &'static str {
45 detect_platform()
46}
47
48pub fn detect_platform() -> &'static str {
49 #[cfg(target_os = "windows")]
50 return "windows";
51 #[cfg(target_os = "macos")]
52 return "macos";
53 #[cfg(target_os = "linux")]
54 return "linux";
55 #[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))]
56 return "freebsd";
57 #[cfg(target_os = "android")]
58 return "android";
59 #[cfg(target_os = "ios")]
60 return "ios";
61 #[allow(unreachable_code)]
62 "unknown"
63}
64
65pub fn is_windows() -> bool {
66 detect_platform() == "windows"
67}
68pub fn is_mac() -> bool {
69 detect_platform() == "macos"
70}
71pub fn is_linux() -> bool {
72 detect_platform() == "linux"
73}
74
75pub fn detect_arch() -> &'static str {
76 #[cfg(target_arch = "x86_64")]
77 return "x86_64";
78 #[cfg(target_arch = "aarch64")]
79 return "aarch64";
80 #[cfg(target_arch = "arm")]
81 return "arm";
82 #[cfg(target_arch = "riscv64")]
83 return "riscv64";
84 #[allow(unreachable_code)]
85 "unknown"
86}
87
88pub fn is_64bit() -> bool {
89 std::mem::size_of::<usize>() == 8
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn test_platform() {
98 let p = detect_platform();
99 assert!(!p.is_empty());
100 }
101
102 #[test]
103 fn test_64bit() {
104 let _ = is_64bit();
105 }
106}