Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up diverging structure in bookmark trees #19

Merged
merged 15 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub enum ErrorKind {
MergeConflict,
UnmergedLocalItems,
UnmergedRemoteItems,
GenerateGuid(Guid),
}

impl fmt::Display for ErrorKind {
Expand Down Expand Up @@ -83,6 +84,9 @@ impl fmt::Display for ErrorKind {
},
ErrorKind::UnmergedRemoteItems => {
write!(f, "Merged tree doesn't mention all items from remote tree")
},
ErrorKind::GenerateGuid(invalid_guid) => {
write!(f, "Failed to generate new GUID for {}", invalid_guid)
}
}
}
Expand Down
34 changes: 20 additions & 14 deletions src/guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ enum Repr {
/// The Places root GUID, used to root all items in a bookmark tree.
pub const ROOT_GUID: Guid = Guid(Repr::Fast(*b"root________"));

/// The "Other Bookmarks" GUID, used to hold items without a parent.
pub const UNFILED_GUID: Guid = Guid(Repr::Fast(*b"unfiled_____"));

/// The syncable Places roots. All synced items should descend from these
/// roots.
pub const USER_CONTENT_ROOTS: [Guid; 4] = [
Guid(Repr::Fast(*b"toolbar_____")),
Guid(Repr::Fast(*b"menu________")),
Guid(Repr::Fast(*b"unfiled_____")),
UNFILED_GUID,
Guid(Repr::Fast(*b"mobile______")),
];

Expand All @@ -51,18 +54,6 @@ const VALID_GUID_BYTES: [u8; 255] =
0, 0, 0, 0, 0, 0, 0];

impl Guid {
pub fn new(s: &str) -> Guid {
let repr = if Guid::is_valid(s.as_bytes()) {
assert!(s.is_char_boundary(12));
let mut bytes = [0u8; 12];
bytes.copy_from_slice(s.as_bytes());
Repr::Fast(bytes)
} else {
Repr::Slow(s.into())
};
Guid(repr)
}

pub fn from_utf8(b: &[u8]) -> Option<Guid> {
let repr = if Guid::is_valid(b) {
let mut bytes = [0u8; 12];
Expand Down Expand Up @@ -117,6 +108,13 @@ impl Guid {
}
}

pub fn valid(&self) -> bool {
match self.0 {
Repr::Fast(_) => true,
Repr::Slow(_) => false,
}
}

/// Equivalent to `PlacesUtils.isValidGuid`.
#[inline]
fn is_valid<T: Copy + IntoByte>(bytes: &[T]) -> bool {
Expand All @@ -129,7 +127,15 @@ impl Guid {
impl<'a> From<&'a str> for Guid {
#[inline]
fn from(s: &'a str) -> Guid {
Guid::new(s)
let repr = if Guid::is_valid(s.as_bytes()) {
assert!(s.is_char_boundary(12));
let mut bytes = [0u8; 12];
bytes.copy_from_slice(s.as_bytes());
Repr::Fast(bytes)
} else {
Repr::Slow(s.into())
};
Guid(repr)
}
}

Expand Down
Loading