places/storage/bookmarks/
conversions.rsuse 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 {
pub fn into_updatable(self, ty: BookmarkType) -> Result<(SyncGuid, UpdatableItem)> {
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))
}
}