places/bookmark_sync/
mod.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 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/. */

pub mod engine;
mod incoming;
pub mod record;

#[cfg(test)]
mod tests;

use crate::error::*;
pub use engine::BookmarksSyncEngine;
use rusqlite::types::{ToSql, ToSqlOutput};
use rusqlite::Result as RusqliteResult;

/// Synced item kinds. These are stored in `moz_bookmarks_synced.kind` and match
/// the definitions in `mozISyncedBookmarksMerger`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(u8)]
pub enum SyncedBookmarkKind {
    Bookmark = 1,  // KIND_BOOKMARK
    Query = 2,     // KIND_QUERY
    Folder = 3,    // KIND_FOLDER
    Livemark = 4,  // KIND_LIVEMARK
    Separator = 5, // KIND_SEPARATOR
}

impl SyncedBookmarkKind {
    #[inline]
    pub fn from_u8(v: u8) -> Result<Self> {
        match v {
            1 => Ok(SyncedBookmarkKind::Bookmark),
            2 => Ok(SyncedBookmarkKind::Query),
            3 => Ok(SyncedBookmarkKind::Folder),
            4 => Ok(SyncedBookmarkKind::Livemark),
            5 => Ok(SyncedBookmarkKind::Separator),
            _ => Err(Error::UnsupportedSyncedBookmarkKind(v)),
        }
    }
}

impl From<SyncedBookmarkKind> for dogear::Kind {
    #[inline]
    fn from(kind: SyncedBookmarkKind) -> dogear::Kind {
        match kind {
            SyncedBookmarkKind::Bookmark => dogear::Kind::Bookmark,
            SyncedBookmarkKind::Query => dogear::Kind::Query,
            SyncedBookmarkKind::Folder => dogear::Kind::Folder,
            SyncedBookmarkKind::Livemark => dogear::Kind::Livemark,
            SyncedBookmarkKind::Separator => dogear::Kind::Separator,
        }
    }
}

impl From<dogear::Kind> for SyncedBookmarkKind {
    #[inline]
    fn from(kind: dogear::Kind) -> SyncedBookmarkKind {
        match kind {
            dogear::Kind::Bookmark => SyncedBookmarkKind::Bookmark,
            dogear::Kind::Query => SyncedBookmarkKind::Query,
            dogear::Kind::Folder => SyncedBookmarkKind::Folder,
            dogear::Kind::Livemark => SyncedBookmarkKind::Livemark,
            dogear::Kind::Separator => SyncedBookmarkKind::Separator,
        }
    }
}

impl ToSql for SyncedBookmarkKind {
    #[inline]
    fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
        Ok(ToSqlOutput::from(*self as u8))
    }
}

/// Synced item validity states. These are stored in
/// `moz_bookmarks_synced.validity`, and match the definitions in
/// `mozISyncedBookmarksMerger`. In short:
/// * `Valid` means the record is valid and should be merged as usual.
/// * `Reupload` means a remote item can be fixed up and applied,
///    and should be reuploaded.
/// * `Replace` means a remote item isn't valid at all, and should either be
///    replaced with a valid local copy, or deleted if a valid local copy
///    doesn't exist.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(u8)]
pub enum SyncedBookmarkValidity {
    Valid = 1,    // VALIDITY_VALID
    Reupload = 2, // VALIDITY_REUPLOAD
    Replace = 3,  // VALIDITY_REPLACE
}

impl SyncedBookmarkValidity {
    #[inline]
    pub fn from_u8(v: u8) -> Result<Self> {
        match v {
            1 => Ok(SyncedBookmarkValidity::Valid),
            2 => Ok(SyncedBookmarkValidity::Reupload),
            3 => Ok(SyncedBookmarkValidity::Replace),
            _ => Err(Error::UnsupportedSyncedBookmarkValidity(v)),
        }
    }
}

impl From<SyncedBookmarkValidity> for dogear::Validity {
    fn from(validity: SyncedBookmarkValidity) -> dogear::Validity {
        match validity {
            SyncedBookmarkValidity::Valid => dogear::Validity::Valid,
            SyncedBookmarkValidity::Reupload => dogear::Validity::Reupload,
            SyncedBookmarkValidity::Replace => dogear::Validity::Replace,
        }
    }
}

impl ToSql for SyncedBookmarkValidity {
    fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
        Ok(ToSqlOutput::from(*self as u8))
    }
}