1use rhai::{CustomType, TypeBuilder};
2
3#[derive(Debug, Clone, CustomType)]
4pub struct SystemInfo {
5 #[rhai_type(readonly)]
6 hostname: String,
7 #[rhai_type(readonly)]
8 username: String,
9 #[rhai_type(readonly)]
10 distro: String,
11 #[rhai_type(readonly)]
12 device_name: String,
13 #[rhai_type(readonly)]
14 arch: String,
15 #[rhai_type(readonly)]
16 desktop_env: String,
17 #[rhai_type(readonly)]
18 platform: String,
19 #[rhai_type(readonly)]
20 paths: SystemInfoPaths,
21}
22
23#[derive(Debug, Clone, CustomType)]
24pub struct SystemInfoPaths {
25 #[rhai_type(readonly)]
26 cache_dir: String,
27 #[rhai_type(readonly)]
28 config_dir: String,
29 #[rhai_type(readonly)]
30 home_dir: String,
31}
32
33impl SystemInfo {
34 pub fn generate() -> Self {
35 #[cfg(test)]
36 return Self::canonical();
37 #[cfg(not(test))]
38 Self {
39 hostname: whoami::fallible::hostname().unwrap_or_else(|_| "unknown".to_string()),
40 username: whoami::username(),
41 distro: whoami::distro().to_string(),
42 device_name: whoami::fallible::devicename().unwrap_or_else(|_| "unknown".to_string()),
43 arch: whoami::arch().to_string(),
44 desktop_env: whoami::desktop_env().to_string(),
45 platform: whoami::platform().to_string(),
46 paths: SystemInfoPaths {
47 cache_dir: dirs::cache_dir()
48 .map(|x| x.to_string_lossy().to_string())
49 .unwrap_or_else(|| "unknown".into()),
50 config_dir: dirs::config_dir()
51 .map(|x| x.to_string_lossy().to_string())
52 .unwrap_or_else(|| "unknown".into()),
53 home_dir: dirs::home_dir()
54 .map(|x| x.to_string_lossy().to_string())
55 .unwrap_or_else(|| "unknown".into()),
56 },
57 }
58 }
59
60 pub fn canonical() -> Self {
61 Self {
62 hostname: "canonical-hostname".to_string(),
63 username: "canonical-username".to_string(),
64 paths: SystemInfoPaths {
65 cache_dir: "/canonical/cache".to_string(),
66 config_dir: "/canonical/config".to_string(),
67 home_dir: "/canonical/home".to_string(),
68 },
69 distro: "distro".to_string(),
70 device_name: "devicename".to_string(),
71 arch: "x86_64".to_string(),
72 desktop_env: "gnome".to_string(),
73 platform: "linux".to_string(),
74 }
75 }
76}