winget_types/installer/
scope.rs

1use serde::{Deserialize, Serialize};
2
3use crate::utils::RelativeDir;
4
5#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
6#[serde(rename_all = "lowercase")]
7pub enum Scope {
8    User,
9    Machine,
10}
11
12impl Scope {
13    #[must_use]
14    pub fn from_url(url: &str) -> Option<Self> {
15        match url.to_ascii_lowercase() {
16            url if url.contains("all-users") || url.contains("machine") => Some(Self::Machine),
17            url if url.contains("user") => Some(Self::User),
18            _ => None,
19        }
20    }
21
22    #[must_use]
23    pub fn from_install_dir(install_dir: &str) -> Option<Self> {
24        const USER_INSTALL_DIRS: [&str; 2] = [RelativeDir::APP_DATA, RelativeDir::LOCAL_APP_DATA];
25        const MACHINE_INSTALL_DIRS: [&str; 7] = [
26            RelativeDir::PROGRAM_FILES_64,
27            RelativeDir::PROGRAM_FILES_32,
28            RelativeDir::COMMON_FILES_64,
29            RelativeDir::COMMON_FILES_32,
30            RelativeDir::PROGRAM_DATA,
31            RelativeDir::WINDOWS_DIR,
32            RelativeDir::SYSTEM_ROOT,
33        ];
34
35        USER_INSTALL_DIRS
36            .iter()
37            .any(|directory| install_dir.starts_with(directory))
38            .then_some(Self::User)
39            .or_else(|| {
40                MACHINE_INSTALL_DIRS
41                    .iter()
42                    .any(|directory| install_dir.starts_with(directory))
43                    .then_some(Self::Machine)
44            })
45    }
46}