places/storage/bookmarks/
conversions.rs

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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use super::{
    BookmarkPosition, BookmarkUpdateInfo, InvalidPlaceInfo, UpdatableBookmark, UpdatableFolder,
    UpdatableItem, UpdatableSeparator, UpdateTreeLocation,
};

use crate::error::Result;
use crate::types::BookmarkType;
use sync_guid::Guid as SyncGuid;
use url::Url;

impl BookmarkUpdateInfo {
    /// The functions exposed over the FFI use the same type for all inserts.
    /// This function converts that into the type our update API uses.
    pub fn into_updatable(self, ty: BookmarkType) -> Result<(SyncGuid, UpdatableItem)> {
        // Check the things that otherwise would be enforced by the type system.

        if self.title.is_some() && ty == BookmarkType::Separator {
            return Err(InvalidPlaceInfo::IllegalChange("title", ty).into());
        }

        if self.url.is_some() && ty != BookmarkType::Bookmark {
            return Err(InvalidPlaceInfo::IllegalChange("url", ty).into());
        }

        let location = match (self.parent_guid, self.position) {
            (None, None) => UpdateTreeLocation::None,
            (None, Some(pos)) => UpdateTreeLocation::Position {
                pos: BookmarkPosition::Specific { pos },
            },
            (Some(parent_guid), pos) => UpdateTreeLocation::Parent {
                guid: parent_guid,
                pos: pos.map_or(BookmarkPosition::Append, |p| BookmarkPosition::Specific {
                    pos: p,
                }),
            },
        };

        let updatable = match ty {
            BookmarkType::Bookmark => UpdatableItem::Bookmark {
                b: UpdatableBookmark {
                    location,
                    title: self.title,
                    url: self.url.map(|u| Url::parse(&u)).transpose()?,
                },
            },
            BookmarkType::Separator => UpdatableItem::Separator {
                s: UpdatableSeparator { location },
            },
            BookmarkType::Folder => UpdatableItem::Folder {
                f: UpdatableFolder {
                    location,
                    title: self.title,
                },
            },
        };

        Ok((self.guid, updatable))
    }
}