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 std::cmp::Ordering;
use rusqlite::Row;
use serde::Serialize;
use crate::applebooks::database::{ABDatabaseName, ABQuery};
use super::datetime::DateTimeUtc;
use super::epubcfi;
#[derive(Debug, Default, Clone, Eq, Serialize)]
pub struct Annotation {
pub body: String,
pub style: String,
pub notes: String,
pub tags: Vec<String>,
pub metadata: AnnotationMetadata,
}
impl Annotation {
fn int_to_style(int: u8) -> String {
let style = match int {
0 => "underline",
1 => "green",
2 => "blue",
3 => "yellow",
4 => "pink",
5 => "purple",
_ => "",
};
style.to_owned()
}
}
impl ABQuery for Annotation {
const DATABASE_NAME: ABDatabaseName = ABDatabaseName::Annotations;
const QUERY: &'static str = {
"SELECT
ZANNOTATIONSELECTEDTEXT, -- 0 body
ZANNOTATIONNOTE, -- 1 notes
ZANNOTATIONSTYLE, -- 2 style
ZANNOTATIONUUID, -- 3 id
ZAEANNOTATION.ZANNOTATIONASSETID, -- 4 book_id
ZANNOTATIONCREATIONDATE, -- 5 created
ZANNOTATIONMODIFICATIONDATE, -- 6 modified
ZANNOTATIONLOCATION -- 7 location
FROM ZAEANNOTATION
WHERE ZANNOTATIONSELECTEDTEXT IS NOT NULL
AND ZANNOTATIONDELETED = 0
ORDER BY ZANNOTATIONASSETID;"
};
fn from_row(row: &Row<'_>) -> Self {
let notes: Option<String> = row.get_unwrap(1);
let style: u8 = row.get_unwrap(2);
let created: f64 = row.get_unwrap(5);
let modified: f64 = row.get_unwrap(6);
let epubcfi: String = row.get_unwrap(7);
Self {
body: row.get_unwrap(0),
style: Self::int_to_style(style),
notes: notes.unwrap_or_default(),
tags: Vec::new(),
metadata: AnnotationMetadata {
id: row.get_unwrap(3),
book_id: row.get_unwrap(4),
created: created.into(),
modified: modified.into(),
location: epubcfi::parse(&epubcfi),
epubcfi,
},
}
}
}
impl Ord for Annotation {
fn cmp(&self, other: &Self) -> Ordering {
self.metadata.cmp(&other.metadata)
}
}
impl PartialOrd for Annotation {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.metadata.partial_cmp(&other.metadata)
}
}
impl PartialEq for Annotation {
fn eq(&self, other: &Self) -> bool {
self.metadata == other.metadata
}
}
#[derive(Debug, Default, Clone, Eq)]
pub struct AnnotationMetadata {
pub id: String,
pub book_id: String,
pub created: DateTimeUtc,
pub modified: DateTimeUtc,
pub location: String,
pub epubcfi: String,
}
impl AnnotationMetadata {
#[must_use]
pub fn slug_created(&self) -> String {
self.created
.format(crate::defaults::DATE_FORMAT)
.to_string()
}
#[must_use]
pub fn slug_modified(&self) -> String {
self.created
.format(crate::defaults::DATE_FORMAT)
.to_string()
}
}
impl serde::Serialize for AnnotationMetadata {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct _Slugs {
created: String,
modified: String,
}
#[derive(Serialize)]
struct _AnnotationMetadata<'a> {
id: &'a str,
book_id: &'a str,
created: &'a DateTimeUtc,
modified: &'a DateTimeUtc,
location: &'a str,
epubcfi: &'a str,
slugs: _Slugs,
}
let metadata = _AnnotationMetadata {
id: &self.id,
book_id: &self.book_id,
created: &self.created,
modified: &self.modified,
location: &self.location,
epubcfi: &self.epubcfi,
slugs: _Slugs {
created: self.slug_created(),
modified: self.slug_modified(),
},
};
metadata.serialize(serializer)
}
}
impl Ord for AnnotationMetadata {
fn cmp(&self, other: &Self) -> Ordering {
self.location.cmp(&other.location)
}
}
impl PartialOrd for AnnotationMetadata {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.location.partial_cmp(&other.location)
}
}
impl PartialEq for AnnotationMetadata {
fn eq(&self, other: &Self) -> bool {
self.location == other.location
}
}
#[cfg(test)]
mod test_annotations {
use super::*;
#[test]
fn test_cmp_annotations() {
let mut a1 = Annotation::default();
a1.metadata.location = epubcfi::parse("epubcfi(/6/10[c01]!/4/10/3,:335,:749)");
let mut a2 = Annotation::default();
a2.metadata.location = epubcfi::parse("epubcfi(/6/12[c02]!/4/26/3,:68,:493)");
assert!(a1 < a2);
}
}