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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* 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/. */

use std::fmt;

use crate::storage::bookmarks::BookmarkRootGuid;
use crate::types::UnknownFields;
use serde::{
    de::{Deserialize, Deserializer, Visitor},
    ser::{Serialize, Serializer},
};
use serde_derive::*;
use sync_guid::Guid as SyncGuid;

/// A bookmark record ID. Bookmark record IDs are the same as Places GUIDs,
/// except for:
///
/// 1. The Places root, which is "places". Note that the Places root is not
///    synced, but is still referenced in the user content roots' `parentid`s.
/// 2. The four user content roots, which omit trailing underscores.
///
/// This wrapper helps avoid mix-ups like storing a record ID instead of a GUID,
/// or uploading a GUID instead of a record ID.
///
/// Internally, we convert record IDs to GUIDs when applying incoming records,
/// and only convert back to GUIDs during upload.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct BookmarkRecordId(SyncGuid);

impl BookmarkRecordId {
    /// Creates a bookmark record ID from a Sync record payload ID.
    pub fn from_payload_id(payload_id: SyncGuid) -> BookmarkRecordId {
        BookmarkRecordId(match payload_id.as_str() {
            "places" => BookmarkRootGuid::Root.as_guid(),
            "menu" => BookmarkRootGuid::Menu.as_guid(),
            "toolbar" => BookmarkRootGuid::Toolbar.as_guid(),
            "unfiled" => BookmarkRootGuid::Unfiled.as_guid(),
            "mobile" => BookmarkRootGuid::Mobile.as_guid(),
            _ => payload_id,
        })
    }

    /// Returns a reference to the record payload ID. This is the borrowed
    /// version of `into_payload_id`, and used for serialization.
    #[inline]
    pub fn as_payload_id(&self) -> &str {
        self.root_payload_id().unwrap_or_else(|| self.0.as_ref())
    }

    /// Returns the record payload ID. This is the owned version of
    /// `as_payload_id`, and exists to avoid copying strings when uploading
    /// tombstones.
    #[inline]
    pub fn into_payload_id(self) -> SyncGuid {
        self.root_payload_id().map(Into::into).unwrap_or(self.0)
    }

    /// Returns a reference to the GUID for this record ID.
    #[inline]
    pub fn as_guid(&self) -> &SyncGuid {
        &self.0
    }

    fn root_payload_id(&self) -> Option<&str> {
        Some(match BookmarkRootGuid::from_guid(self.as_guid()) {
            Some(BookmarkRootGuid::Root) => "places",
            Some(BookmarkRootGuid::Menu) => "menu",
            Some(BookmarkRootGuid::Toolbar) => "toolbar",
            Some(BookmarkRootGuid::Unfiled) => "unfiled",
            Some(BookmarkRootGuid::Mobile) => "mobile",
            None => return None,
        })
    }
}

/// Converts a Places GUID into a bookmark record ID.
impl From<SyncGuid> for BookmarkRecordId {
    #[inline]
    fn from(guid: SyncGuid) -> BookmarkRecordId {
        BookmarkRecordId(guid)
    }
}

/// Converts a bookmark record ID into a Places GUID.
impl From<BookmarkRecordId> for SyncGuid {
    #[inline]
    fn from(record_id: BookmarkRecordId) -> SyncGuid {
        record_id.0
    }
}

impl Serialize for BookmarkRecordId {
    #[inline]
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_payload_id())
    }
}

impl<'de> Deserialize<'de> for BookmarkRecordId {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        struct V;

        impl<'de> Visitor<'de> for V {
            type Value = BookmarkRecordId;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a bookmark record ID")
            }

            #[inline]
            fn visit_string<E: serde::de::Error>(
                self,
                payload_id: String,
            ) -> std::result::Result<BookmarkRecordId, E> {
                // The JSON deserializer passes owned strings, so we can avoid
                // cloning the payload ID in the common case...
                Ok(BookmarkRecordId::from_payload_id(payload_id.into()))
            }

            #[inline]
            fn visit_str<E: serde::de::Error>(
                self,
                payload_id: &str,
            ) -> std::result::Result<BookmarkRecordId, E> {
                // ...However, the Serde docs say we must implement
                // `visit_str` if we implement `visit_string`, so we also
                // provide an implementation that clones the ID.
                Ok(BookmarkRecordId::from_payload_id(payload_id.into()))
            }
        }

        deserializer.deserialize_string(V)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BookmarkRecord {
    // Note that `SyncGuid` does not check for validity, which is what we
    // want. If the bookmark has an invalid GUID, we'll make a new one.
    #[serde(rename = "id")]
    pub record_id: BookmarkRecordId,

    #[serde(rename = "parentid")]
    pub parent_record_id: Option<BookmarkRecordId>,

    #[serde(rename = "parentName", skip_serializing_if = "Option::is_none")]
    pub parent_title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "de_maybe_stringified_timestamp")]
    pub date_added: Option<i64>,

    #[serde(default)]
    pub has_dupe: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(rename = "bmkUri", skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub keyword: Option<String>,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,

    #[serde(flatten)]
    pub unknown_fields: UnknownFields,
}

impl From<BookmarkRecord> for BookmarkItemRecord {
    #[inline]
    fn from(b: BookmarkRecord) -> BookmarkItemRecord {
        BookmarkItemRecord::Bookmark(b)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryRecord {
    #[serde(rename = "id")]
    pub record_id: BookmarkRecordId,

    #[serde(rename = "parentid")]
    pub parent_record_id: Option<BookmarkRecordId>,

    #[serde(rename = "parentName", skip_serializing_if = "Option::is_none")]
    pub parent_title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "de_maybe_stringified_timestamp")]
    pub date_added: Option<i64>,

    #[serde(default)]
    pub has_dupe: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(rename = "bmkUri", skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    #[serde(rename = "folderName", skip_serializing_if = "Option::is_none")]
    pub tag_folder_name: Option<String>,

    #[serde(flatten)]
    pub unknown_fields: UnknownFields,
}

impl From<QueryRecord> for BookmarkItemRecord {
    #[inline]
    fn from(q: QueryRecord) -> BookmarkItemRecord {
        BookmarkItemRecord::Query(q)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FolderRecord {
    #[serde(rename = "id")]
    pub record_id: BookmarkRecordId,

    #[serde(rename = "parentid")]
    pub parent_record_id: Option<BookmarkRecordId>,

    #[serde(rename = "parentName", skip_serializing_if = "Option::is_none")]
    pub parent_title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "de_maybe_stringified_timestamp")]
    pub date_added: Option<i64>,

    #[serde(default)]
    pub has_dupe: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(default)]
    pub children: Vec<BookmarkRecordId>,

    #[serde(flatten)]
    pub unknown_fields: UnknownFields,
}

impl From<FolderRecord> for BookmarkItemRecord {
    #[inline]
    fn from(f: FolderRecord) -> BookmarkItemRecord {
        BookmarkItemRecord::Folder(f)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LivemarkRecord {
    #[serde(rename = "id")]
    pub record_id: BookmarkRecordId,

    #[serde(rename = "parentid")]
    pub parent_record_id: Option<BookmarkRecordId>,

    #[serde(rename = "parentName", skip_serializing_if = "Option::is_none")]
    pub parent_title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "de_maybe_stringified_timestamp")]
    pub date_added: Option<i64>,

    #[serde(default)]
    pub has_dupe: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(rename = "feedUri", skip_serializing_if = "Option::is_none")]
    pub feed_url: Option<String>,

    #[serde(rename = "siteUri", skip_serializing_if = "Option::is_none")]
    pub site_url: Option<String>,

    #[serde(flatten)]
    pub unknown_fields: UnknownFields,
}

impl From<LivemarkRecord> for BookmarkItemRecord {
    #[inline]
    fn from(l: LivemarkRecord) -> BookmarkItemRecord {
        BookmarkItemRecord::Livemark(l)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SeparatorRecord {
    #[serde(rename = "id")]
    pub record_id: BookmarkRecordId,

    #[serde(rename = "parentid")]
    pub parent_record_id: Option<BookmarkRecordId>,

    #[serde(rename = "parentName", skip_serializing_if = "Option::is_none")]
    pub parent_title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "de_maybe_stringified_timestamp")]
    pub date_added: Option<i64>,

    #[serde(default)]
    pub has_dupe: bool,

    // Not used on newer clients, but can be used to detect parent-child
    // position disagreements. Older clients use this for deduping.
    #[serde(rename = "pos", skip_serializing_if = "Option::is_none")]
    pub position: Option<i64>,

    #[serde(flatten)]
    pub unknown_fields: UnknownFields,
}

impl From<SeparatorRecord> for BookmarkItemRecord {
    #[inline]
    fn from(s: SeparatorRecord) -> BookmarkItemRecord {
        BookmarkItemRecord::Separator(s)
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum BookmarkItemRecord {
    Bookmark(BookmarkRecord),
    Query(QueryRecord),
    Folder(FolderRecord),
    Livemark(LivemarkRecord),
    Separator(SeparatorRecord),
}

impl BookmarkItemRecord {
    pub fn record_id(&self) -> &BookmarkRecordId {
        match self {
            Self::Bookmark(b) => &b.record_id,
            Self::Query(q) => &q.record_id,
            Self::Folder(f) => &f.record_id,
            Self::Livemark(l) => &l.record_id,
            Self::Separator(s) => &s.record_id,
        }
    }

    pub fn unknown_fields(&self) -> &UnknownFields {
        match self {
            Self::Bookmark(b) => &b.unknown_fields,
            Self::Folder(f) => &f.unknown_fields,
            Self::Separator(s) => &s.unknown_fields,
            Self::Query(q) => &q.unknown_fields,
            Self::Livemark(l) => &l.unknown_fields,
        }
    }
}

// dateAdded on a bookmark might be a string! See #1148.
fn de_maybe_stringified_timestamp<'de, D>(
    deserializer: D,
) -> std::result::Result<Option<i64>, D::Error>
where
    D: serde::de::Deserializer<'de>,
{
    use std::marker::PhantomData;

    struct StringOrInt(PhantomData<Option<i64>>);

    impl<'de> Visitor<'de> for StringOrInt {
        type Value = Option<i64>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("string or int")
        }

        fn visit_str<E>(self, value: &str) -> Result<Option<i64>, E>
        where
            E: serde::de::Error,
        {
            match value.parse::<i64>() {
                Ok(v) => Ok(Some(v)),
                Err(_) => Err(E::custom("invalid string literal")),
            }
        }

        // all positive int literals
        fn visit_i64<E: serde::de::Error>(self, value: i64) -> Result<Option<i64>, E> {
            Ok(Some(value.max(0)))
        }

        // all negative int literals
        fn visit_u64<E: serde::de::Error>(self, value: u64) -> Result<Option<i64>, E> {
            Ok(Some((value as i64).max(0)))
        }
    }
    deserializer.deserialize_any(StringOrInt(PhantomData))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{json, Error};

    #[test]
    fn test_invalid_record_type() {
        let r: std::result::Result<BookmarkItemRecord, Error> =
            serde_json::from_value(json!({"id": "whatever", "type" : "unknown-type"}));
        let e = r.unwrap_err();
        assert!(e.is_data());
        // I guess is good enough to check we are hitting what we expect.
        assert!(e.to_string().contains("unknown-type"));
    }

    #[test]
    fn test_id_rewriting() {
        let j = json!({"id": "unfiled", "parentid": "menu", "type": "bookmark"});
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Bookmark(b) => {
                assert_eq!(b.record_id.as_guid(), BookmarkRootGuid::Unfiled);
                assert_eq!(
                    b.parent_record_id.as_ref().map(BookmarkRecordId::as_guid),
                    Some(&BookmarkRootGuid::Menu.as_guid())
                );
            }
            _ => panic!("unexpected record type"),
        };
        let v = serde_json::to_value(r).expect("should serialize");
        assert_eq!(
            v,
            json!({
                "id": "unfiled",
                "parentid": "menu",
                "type": "bookmark",
                "hasDupe": false,
            })
        );

        let j = json!({"id": "unfiled", "parentid": "menu", "type": "query"});
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Query(q) => {
                assert_eq!(q.record_id.as_guid(), BookmarkRootGuid::Unfiled);
                assert_eq!(
                    q.parent_record_id.as_ref().map(BookmarkRecordId::as_guid),
                    Some(&BookmarkRootGuid::Menu.as_guid())
                );
            }
            _ => panic!("unexpected record type"),
        };
        let v = serde_json::to_value(r).expect("should serialize");
        assert_eq!(
            v,
            json!({
                "id": "unfiled",
                "parentid": "menu",
                "type": "query",
                "hasDupe": false,
            })
        );

        let j = json!({"id": "unfiled", "parentid": "menu", "type": "folder"});
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Folder(f) => {
                assert_eq!(f.record_id.as_guid(), BookmarkRootGuid::Unfiled);
                assert_eq!(
                    f.parent_record_id.as_ref().map(BookmarkRecordId::as_guid),
                    Some(&BookmarkRootGuid::Menu.as_guid())
                );
            }
            _ => panic!("unexpected record type"),
        };
        let v = serde_json::to_value(r).expect("should serialize");
        assert_eq!(
            v,
            json!({
                "id": "unfiled",
                "parentid": "menu",
                "type": "folder",
                "hasDupe": false,
                "children": [],
            })
        );

        let j = json!({"id": "unfiled", "parentid": "menu", "type": "livemark"});
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Livemark(l) => {
                assert_eq!(l.record_id.as_guid(), BookmarkRootGuid::Unfiled);
                assert_eq!(
                    l.parent_record_id.as_ref().map(BookmarkRecordId::as_guid),
                    Some(&BookmarkRootGuid::Menu.as_guid())
                );
            }
            _ => panic!("unexpected record type"),
        };
        let v = serde_json::to_value(r).expect("should serialize");
        assert_eq!(
            v,
            json!({
                "id": "unfiled",
                "parentid": "menu",
                "type": "livemark",
                "hasDupe": false,
            })
        );

        let j = json!({"id": "unfiled", "parentid": "menu", "type": "separator"});
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Separator(s) => {
                assert_eq!(s.record_id.as_guid(), BookmarkRootGuid::Unfiled);
                assert_eq!(
                    s.parent_record_id.as_ref().map(BookmarkRecordId::as_guid),
                    Some(&BookmarkRootGuid::Menu.as_guid())
                );
            }
            _ => panic!("unexpected record type"),
        };
        let v = serde_json::to_value(r).expect("should serialize");
        assert_eq!(
            v,
            json!({
                "id": "unfiled",
                "parentid": "menu",
                "type": "separator",
                "hasDupe": false,
            })
        );
    }

    // It's unfortunate that all below 'dateadded' tests only check the
    // 'BookmarkItemRecord' variant, so it would be a problem if `date_added` on
    // other variants forgot to do the `deserialize_with` dance. We could
    // implement a new type to make that less likely, but that's not foolproof
    // either and causes this hysterical raisin to leak out from this module.
    fn check_date_added(j: serde_json::Value, expected: Option<i64>) {
        let r: BookmarkItemRecord = serde_json::from_value(j).expect("should deserialize");
        match &r {
            BookmarkItemRecord::Bookmark(b) => assert_eq!(b.date_added, expected),
            _ => panic!("unexpected record type"),
        };
    }

    #[test]
    fn test_dateadded_missing() {
        check_date_added(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark"}),
            None,
        )
    }

    #[test]
    fn test_dateadded_int() {
        check_date_added(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": 123}),
            Some(123),
        )
    }

    #[test]
    fn test_dateadded_negative() {
        check_date_added(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": -1}),
            Some(0),
        )
    }

    #[test]
    fn test_dateadded_str() {
        check_date_added(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": "123"}),
            Some(123),
        )
    }

    // A kinda "policy" decision - like serde, 'type errors' fail rather than default.
    #[test]
    fn test_dateadded_null() {
        // a literal `null` is insane (and note we already test it *missing* above)
        serde_json::from_value::<BookmarkItemRecord>(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": null}),
        )
        .expect_err("should fail, literal null");
    }

    #[test]
    fn test_dateadded_invalid_str() {
        serde_json::from_value::<BookmarkItemRecord>(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": "foo"}),
        )
        .expect_err("should fail, bad string value");
    }

    #[test]
    fn test_dateadded_invalid_type() {
        serde_json::from_value::<BookmarkItemRecord>(
            json!({"id": "unfiled", "parentid": "menu", "type": "bookmark", "dateAdded": []}),
        )
        .expect_err("should fail, invalid type");
    }
}