places/storage/bookmarks/
root_guid.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use lazy_static::lazy_static;
6use sync_guid::Guid as SyncGuid;
7
8pub const USER_CONTENT_ROOTS: &[BookmarkRootGuid] = &[
9    BookmarkRootGuid::Menu,
10    BookmarkRootGuid::Toolbar,
11    BookmarkRootGuid::Unfiled,
12    BookmarkRootGuid::Mobile,
13];
14
15/// Special GUIDs associated with bookmark roots.
16/// It's guaranteed that the roots will always have these guids.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
18#[repr(u8)]
19pub enum BookmarkRootGuid {
20    Root,
21    Menu,
22    Toolbar,
23    Unfiled,
24    Mobile,
25}
26
27lazy_static! {
28    static ref GUIDS: [(BookmarkRootGuid, SyncGuid); 5] = [
29        (
30            BookmarkRootGuid::Root,
31            SyncGuid::from(BookmarkRootGuid::Root.as_str())
32        ),
33        (
34            BookmarkRootGuid::Menu,
35            SyncGuid::from(BookmarkRootGuid::Menu.as_str())
36        ),
37        (
38            BookmarkRootGuid::Toolbar,
39            SyncGuid::from(BookmarkRootGuid::Toolbar.as_str())
40        ),
41        (
42            BookmarkRootGuid::Unfiled,
43            SyncGuid::from(BookmarkRootGuid::Unfiled.as_str())
44        ),
45        (
46            BookmarkRootGuid::Mobile,
47            SyncGuid::from(BookmarkRootGuid::Mobile.as_str())
48        ),
49    ];
50}
51
52impl BookmarkRootGuid {
53    pub fn as_str(self) -> &'static str {
54        match self {
55            BookmarkRootGuid::Root => "root________",
56            BookmarkRootGuid::Menu => "menu________",
57            BookmarkRootGuid::Toolbar => "toolbar_____",
58            BookmarkRootGuid::Unfiled => "unfiled_____",
59            BookmarkRootGuid::Mobile => "mobile______",
60        }
61    }
62
63    pub fn guid(self) -> &'static SyncGuid {
64        &GUIDS[self as usize].1
65    }
66
67    pub fn as_guid(self) -> SyncGuid {
68        self.guid().clone()
69    }
70
71    pub fn well_known(guid: &str) -> Option<Self> {
72        GUIDS
73            .iter()
74            .find(|(_, sync_guid)| sync_guid.as_str() == guid)
75            .map(|(root, _)| *root)
76    }
77
78    pub fn from_guid(guid: &SyncGuid) -> Option<Self> {
79        Self::well_known(guid.as_ref())
80    }
81}
82
83impl From<BookmarkRootGuid> for SyncGuid {
84    fn from(item: BookmarkRootGuid) -> SyncGuid {
85        item.as_guid()
86    }
87}
88
89// Allow comparisons between BookmarkRootGuid and SyncGuids
90impl PartialEq<BookmarkRootGuid> for SyncGuid {
91    fn eq(&self, other: &BookmarkRootGuid) -> bool {
92        self.as_str().as_bytes() == other.as_str().as_bytes()
93    }
94}
95
96impl PartialEq<SyncGuid> for BookmarkRootGuid {
97    fn eq(&self, other: &SyncGuid) -> bool {
98        other.as_str().as_bytes() == self.as_str().as_bytes()
99    }
100}
101
102// Even if we have a reference to &SyncGuid
103impl PartialEq<BookmarkRootGuid> for &SyncGuid {
104    fn eq(&self, other: &BookmarkRootGuid) -> bool {
105        self.as_str().as_bytes() == other.as_str().as_bytes()
106    }
107}
108
109impl<'a> PartialEq<&'a SyncGuid> for BookmarkRootGuid {
110    fn eq(&self, other: &&'a SyncGuid) -> bool {
111        other.as_str().as_bytes() == self.as_str().as_bytes()
112    }
113}
114
115// And between BookmarkRootGuid and &str
116impl PartialEq<BookmarkRootGuid> for &str {
117    fn eq(&self, other: &BookmarkRootGuid) -> bool {
118        *self == other.as_str()
119    }
120}
121
122impl<'a> PartialEq<&'a str> for BookmarkRootGuid {
123    fn eq(&self, other: &&'a str) -> bool {
124        self.as_str() == *other
125    }
126}