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/. */
45use super::{
6 BookmarkPosition, BookmarkUpdateInfo, InvalidPlaceInfo, UpdatableBookmark, UpdatableFolder,
7 UpdatableItem, UpdatableSeparator, UpdateTreeLocation,
8};
910use crate::error::Result;
11use crate::types::BookmarkType;
12use sync_guid::Guid as SyncGuid;
13use url::Url;
1415impl 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.
18pub fn into_updatable(self, ty: BookmarkType) -> Result<(SyncGuid, UpdatableItem)> {
19// Check the things that otherwise would be enforced by the type system.
2021if self.title.is_some() && ty == BookmarkType::Separator {
22return Err(InvalidPlaceInfo::IllegalChange("title", ty).into());
23 }
2425if self.url.is_some() && ty != BookmarkType::Bookmark {
26return Err(InvalidPlaceInfo::IllegalChange("url", ty).into());
27 }
2829let 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 };
4142let 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 };
6061Ok((self.guid, updatable))
62 }
63}