places/storage/bookmarks/
conversions.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 super::{
6    BookmarkPosition, BookmarkUpdateInfo, InvalidPlaceInfo, UpdatableBookmark, UpdatableFolder,
7    UpdatableItem, UpdatableSeparator, UpdateTreeLocation,
8};
9
10use crate::error::Result;
11use crate::types::BookmarkType;
12use sync_guid::Guid as SyncGuid;
13use url::Url;
14
15impl BookmarkUpdateInfo {
16    /// The functions exposed over the FFI use the same type for all inserts.
17    /// This function converts that into the type our update API uses.
18    pub fn into_updatable(self, ty: BookmarkType) -> Result<(SyncGuid, UpdatableItem)> {
19        // Check the things that otherwise would be enforced by the type system.
20
21        if self.title.is_some() && ty == BookmarkType::Separator {
22            return Err(InvalidPlaceInfo::IllegalChange("title", ty).into());
23        }
24
25        if self.url.is_some() && ty != BookmarkType::Bookmark {
26            return Err(InvalidPlaceInfo::IllegalChange("url", ty).into());
27        }
28
29        let location = match (self.parent_guid, self.position) {
30            (None, None) => UpdateTreeLocation::None,
31            (None, Some(pos)) => UpdateTreeLocation::Position {
32                pos: BookmarkPosition::Specific { pos },
33            },
34            (Some(parent_guid), pos) => UpdateTreeLocation::Parent {
35                guid: parent_guid,
36                pos: pos.map_or(BookmarkPosition::Append, |p| BookmarkPosition::Specific {
37                    pos: p,
38                }),
39            },
40        };
41
42        let updatable = match ty {
43            BookmarkType::Bookmark => UpdatableItem::Bookmark {
44                b: UpdatableBookmark {
45                    location,
46                    title: self.title,
47                    url: self.url.map(|u| Url::parse(&u)).transpose()?,
48                },
49            },
50            BookmarkType::Separator => UpdatableItem::Separator {
51                s: UpdatableSeparator { location },
52            },
53            BookmarkType::Folder => UpdatableItem::Folder {
54                f: UpdatableFolder {
55                    location,
56                    title: self.title,
57                },
58            },
59        };
60
61        Ok((self.guid, updatable))
62    }
63}