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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use rusqlite::Result as RusqliteResult;
use serde::ser::{Serialize, Serializer};
use std::convert::TryFrom;
use std::fmt;
mod visit_transition_set;
pub use visit_transition_set::VisitTransitionSet;
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[error("Invalid visit type")]
pub struct InvalidVisitType;
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VisitTransition {
Link = 1,
Typed = 2,
Bookmark = 3,
Embed = 4,
RedirectPermanent = 5,
RedirectTemporary = 6,
Download = 7,
FramedLink = 8,
Reload = 9,
}
impl ToSql for VisitTransition {
fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(*self as u8))
}
}
impl VisitTransition {
pub fn from_primitive(p: u8) -> Option<Self> {
match p {
1 => Some(VisitTransition::Link),
2 => Some(VisitTransition::Typed),
3 => Some(VisitTransition::Bookmark),
4 => Some(VisitTransition::Embed),
5 => Some(VisitTransition::RedirectPermanent),
6 => Some(VisitTransition::RedirectTemporary),
7 => Some(VisitTransition::Download),
8 => Some(VisitTransition::FramedLink),
9 => Some(VisitTransition::Reload),
_ => None,
}
}
}
impl TryFrom<u8> for VisitTransition {
type Error = InvalidVisitType;
fn try_from(p: u8) -> Result<Self, Self::Error> {
VisitTransition::from_primitive(p).ok_or(InvalidVisitType)
}
}
struct VisitTransitionSerdeVisitor;
impl<'de> serde::de::Visitor<'de> for VisitTransitionSerdeVisitor {
type Value = VisitTransition;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("positive integer representing VisitTransition")
}
fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<VisitTransition, E> {
use std::u8::MAX as U8_MAX;
if value > u64::from(U8_MAX) {
return Err(E::custom(format!("value out of u8 range: {}", value)));
}
VisitTransition::from_primitive(value as u8)
.ok_or_else(|| E::custom(format!("unknown VisitTransition value: {}", value)))
}
}
impl serde::Serialize for VisitTransition {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u64(*self as u64)
}
}
impl<'de> serde::Deserialize<'de> for VisitTransition {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_u64(VisitTransitionSerdeVisitor)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(u8)]
pub enum BookmarkType {
Bookmark = 1,
Folder = 2,
Separator = 3,
}
impl FromSql for BookmarkType {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let v = value.as_i64()?;
if v < 0 || v > i64::from(u8::max_value()) {
return Err(FromSqlError::OutOfRange(v));
}
BookmarkType::from_u8(v as u8).ok_or_else(|| FromSqlError::OutOfRange(v))
}
}
impl BookmarkType {
#[inline]
pub fn from_u8(v: u8) -> Option<Self> {
match v {
1 => Some(BookmarkType::Bookmark),
2 => Some(BookmarkType::Folder),
3 => Some(BookmarkType::Separator),
_ => None,
}
}
pub fn from_u8_with_valid_url<F: Fn() -> bool>(v: u8, has_valid_url: F) -> Self {
match BookmarkType::from_u8(v) {
Some(BookmarkType::Bookmark) | None => {
if has_valid_url() {
BookmarkType::Bookmark
} else {
BookmarkType::Folder
}
}
Some(t) => t,
}
}
}
impl ToSql for BookmarkType {
fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(*self as u8))
}
}
impl Serialize for BookmarkType {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u8(*self as u8)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(u8)]
pub enum SyncStatus {
Unknown = 0,
New = 1,
Normal = 2,
}
impl SyncStatus {
#[inline]
pub fn from_u8(v: u8) -> Self {
match v {
1 => SyncStatus::New,
2 => SyncStatus::Normal,
_ => SyncStatus::Unknown,
}
}
}
impl FromSql for SyncStatus {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
let v = value.as_i64()?;
if v < 0 || v > i64::from(u8::max_value()) {
return Err(FromSqlError::OutOfRange(v));
}
Ok(SyncStatus::from_u8(v as u8))
}
}
impl ToSql for SyncStatus {
fn to_sql(&self) -> RusqliteResult<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(*self as u8))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_primitive() {
assert_eq!(
Some(VisitTransition::Link),
VisitTransition::from_primitive(1)
);
assert_eq!(None, VisitTransition::from_primitive(99));
}
}