1pub mod collection;
12mod util;
13
14use std::{
15 env, fmt, fs,
16 process::{Command, ExitStatus},
17};
18
19use collection::{CollectionData, CollectionError};
20use serde::{Deserialize, Serialize};
21
22#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
24pub enum OS {
25 #[serde(rename = "macos")]
27 MacOs,
28 #[serde(rename = "windows")]
30 Windows,
31 #[serde(rename = "linux")]
33 Linux,
34}
35
36impl OS {
37 pub fn get_system_os() -> Self {
45 match std::env::consts::OS {
46 "macos" => OS::MacOs,
47 "linux" => OS::Linux,
48 "windows" => OS::Windows,
49 _ => panic!("Unsupported OS!"),
50 }
51 }
52}
53
54impl fmt::Display for OS {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 match self {
57 OS::MacOs => write!(f, "macos"),
58 OS::Linux => write!(f, "linux"),
59 OS::Windows => write!(f, "windows"),
60 }
61 }
62}
63
64pub fn get_collection(os: OS) -> Result<CollectionData, CollectionError> {
72 CollectionData::from_file(format!("collections/{os}.yaml"))
73}
74
75pub fn run_script(
86 script_string: &str,
87 file_extension: Option<String>,
88) -> Result<ExitStatus, Box<dyn std::error::Error>> {
89 let mut tmp_file = env::temp_dir();
90 tmp_file.push("privacy-sexy");
91 if let Some(ext) = file_extension {
92 tmp_file.set_extension(ext);
93 }
94
95 fs::write(&tmp_file, script_string)?;
96
97 #[cfg(target_family = "unix")]
98 {
99 use std::os::unix::prelude::PermissionsExt;
100 fs::set_permissions(&tmp_file, fs::Permissions::from_mode(0o755))?;
101 }
102
103 Ok(Command::new(tmp_file.to_str().unwrap_or_default()).spawn()?.wait()?)
104}