1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
pub mod cookies;
pub mod info;

use strum::{EnumProperty, IntoEnumIterator};
use strum_macros::{AsRefStr, Display, EnumIter, EnumString};

#[derive(Clone, Copy)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
#[derive(Default)]
#[derive(Debug)]
#[derive(EnumIter, Display, EnumString, AsRefStr, strum_macros::EnumProperty)]
pub enum Browser {
    /// win, mac, Linux
    #[default]
    #[strum(ascii_case_insensitive, props(Based = "firefox"))]
    Firefox,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "firefox"))]
    Librewolf,

    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Chrome,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Edge,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Chromium,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Brave,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Yandex,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Vivaldi,
    /// win, mac, Linux
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Opera,
    /// win, mac
    #[cfg(not(target_os = "linux"))]
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    OperaGX,
    /// win, mac
    #[cfg(not(target_os = "linux"))]
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    CocCoc,
    /// win, mac, ?
    // #[cfg(not(target_os = "linux"))]
    #[cfg(target_os = "macos")]
    #[strum(ascii_case_insensitive, props(Based = "chromium"))]
    Arc,

    /// mac
    #[cfg(target_os = "macos")]
    #[strum(ascii_case_insensitive, props(Based = "safari"))]
    Safari,
}

impl Browser {
    pub fn is_chromium_base(self) -> bool {
        self.get_str("Based")
            .map_or(false, |base| base.eq_ignore_ascii_case("chromium"))
    }
    pub fn is_firefox_base(self) -> bool {
        self.get_str("Based")
            .map_or(false, |base| base.eq_ignore_ascii_case("firefox"))
    }
    /// return Based chromium browser iterator
    pub fn chromiums() -> impl Iterator<Item = Self> {
        Self::iter().filter(|v| {
            v.get_str("Based")
                .map_or(false, |base| base.eq_ignore_ascii_case("chromium"))
        })
    }
    /// return Based firefox browser iterator
    pub fn firefoxs() -> impl Iterator<Item = Self> {
        Self::iter().filter(|v| {
            v.get_str("Based")
                .map_or(false, |base| base.eq_ignore_ascii_case("firefox"))
        })
    }
}

impl Browser {
    /// for fetch password
    #[cfg(target_os = "macos")]
    pub const fn safe_name(&self) -> &str {
        match self {
            Self::Chromium => "Chromium",
            Self::Chrome => "Chrome",
            Self::Edge => "Microsoft Edge",
            Self::Brave => "Brave",
            Self::Yandex => "Yandex",
            Self::Vivaldi => "Vivaldi",
            Self::Opera => "Opera",
            #[cfg(not(target_os = "linux"))]
            Self::OperaGX => "Opera",
            #[cfg(not(target_os = "linux"))]
            Self::CocCoc => "CocCoc",
            #[cfg(not(target_os = "linux"))]
            Self::Arc => "Arc",
            _ => "Chrome",
        }
    }

    /// for fetch password
    #[cfg(not(target_os = "windows"))]
    pub const fn storage(&self) -> &str {
        match self {
            Self::Chromium => concat!("Chromium", " Safe Storage"),
            Self::Chrome => concat!("Chrome", " Safe Storage"),
            Self::Edge => concat!("Microsoft Edge", " Safe Storage"),
            Self::Brave => concat!("Brave", " Safe Storage"),
            Self::Yandex => concat!("Yandex", " Safe Storage"),
            Self::Vivaldi => concat!("Vivaldi", " Safe Storage"),
            Self::Opera => concat!("Opera", " Safe Storage"),
            #[cfg(not(target_os = "linux"))]
            Self::OperaGX => concat!("Opera", " Safe Storage"),
            #[cfg(not(target_os = "linux"))]
            Self::CocCoc => concat!("CocCoc", " Safe Storage"),
            #[cfg(not(target_os = "linux"))]
            Self::Arc => concat!("Arc", " Safe Storage"),
            _ => concat!("Chrome", " Safe Storage"),
        }
    }
}