ghost-io-api 0.2.0

Strongly-typed, async Rust client for the Ghost CMS Content and Admin APIs
Documentation
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
//! Pagination types for Ghost API browse endpoints.
//!
//! Ghost API browse endpoints return paginated results with metadata about
//! the current page, total pages, and navigation links.
//!
//! # Example
//!
//! ```
//! use ghost_io_api::models::pagination::{Meta, Pagination};
//!
//! // Typically you won't construct these manually, they come from API responses
//! let pagination = Pagination {
//!     page: 1,
//!     limit: 15,
//!     pages: 5,
//!     total: 73,
//!     next: Some(2),
//!     prev: None,
//! };
//!
//! // Check if there are more pages
//! assert!(pagination.has_next());
//! assert!(!pagination.has_prev());
//! ```

use serde::{Deserialize, Serialize};

/// Metadata wrapper returned by Ghost API browse endpoints.
///
/// Browse endpoints return paginated results with a `meta` object containing
/// pagination information.
///
/// # Example
///
/// ```
/// use ghost_io_api::models::pagination::Meta;
/// use serde_json::json;
///
/// let json = json!({
///     "pagination": {
///         "page": 1,
///         "limit": 15,
///         "pages": 5,
///         "total": 73,
///         "next": 2,
///         "prev": null
///     }
/// });
///
/// let meta: Meta = serde_json::from_value(json).unwrap();
/// assert_eq!(meta.pagination.page, 1);
/// assert_eq!(meta.pagination.total, 73);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Meta {
    /// Pagination information for the current request.
    pub pagination: Pagination,
}

/// Pagination information for browse endpoints.
///
/// Contains information about the current page, total number of pages,
/// and links to the next/previous pages.
///
/// # Fields
///
/// * `page` - Current page number (1-indexed)
/// * `limit` - Number of items per page
/// * `pages` - Total number of pages available
/// * `total` - Total number of items across all pages
/// * `next` - Page number of the next page, if available
/// * `prev` - Page number of the previous page, if available
///
/// # Example
///
/// ```
/// use ghost_io_api::models::pagination::Pagination;
/// use serde_json::json;
///
/// let json = json!({
///     "page": 2,
///     "limit": 10,
///     "pages": 5,
///     "total": 47,
///     "next": 3,
///     "prev": 1
/// });
///
/// let pagination: Pagination = serde_json::from_value(json).unwrap();
/// assert_eq!(pagination.page, 2);
/// assert!(pagination.has_next());
/// assert!(pagination.has_prev());
/// assert!(!pagination.is_first_page());
/// assert!(!pagination.is_last_page());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Pagination {
    /// Current page number (1-indexed).
    pub page: u32,

    /// Number of items per page.
    pub limit: u32,

    /// Total number of pages available.
    pub pages: u32,

    /// Total number of items across all pages.
    pub total: u32,

    /// Page number of the next page, if available.
    ///
    /// `None` if this is the last page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next: Option<u32>,

    /// Page number of the previous page, if available.
    ///
    /// `None` if this is the first page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prev: Option<u32>,
}

impl Pagination {
    /// Returns `true` if there is a next page available.
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 1,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: Some(2),
    ///     prev: None,
    /// };
    ///
    /// assert!(pagination.has_next());
    /// ```
    pub fn has_next(&self) -> bool {
        self.next.is_some()
    }

    /// Returns `true` if there is a previous page available.
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 2,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: Some(3),
    ///     prev: Some(1),
    /// };
    ///
    /// assert!(pagination.has_prev());
    /// ```
    pub fn has_prev(&self) -> bool {
        self.prev.is_some()
    }

    /// Returns `true` if this is the first page.
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 1,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: Some(2),
    ///     prev: None,
    /// };
    ///
    /// assert!(pagination.is_first_page());
    /// ```
    pub fn is_first_page(&self) -> bool {
        self.page == 1
    }

    /// Returns `true` if this is the last page.
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 5,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: None,
    ///     prev: Some(4),
    /// };
    ///
    /// assert!(pagination.is_last_page());
    /// ```
    pub fn is_last_page(&self) -> bool {
        self.page == self.pages
    }

    /// Returns the number of items on the current page.
    ///
    /// For all pages except potentially the last one, this will equal `limit`.
    /// On the last page, it may be less than `limit`.
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 5,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: None,
    ///     prev: Some(4),
    /// };
    ///
    /// // Last page: 73 total - (4 * 15) = 13 items on this page
    /// assert_eq!(pagination.items_on_page(), 13);
    /// ```
    pub fn items_on_page(&self) -> u32 {
        if self.is_last_page() {
            // Calculate remaining items on last page
            let items_before_last_page = (self.pages - 1) * self.limit;
            self.total.saturating_sub(items_before_last_page)
        } else {
            self.limit
        }
    }

    /// Returns the starting item index for the current page (0-indexed).
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 2,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: Some(3),
    ///     prev: Some(1),
    /// };
    ///
    /// // Page 2, limit 15: starts at index 15
    /// assert_eq!(pagination.start_index(), 15);
    /// ```
    pub fn start_index(&self) -> u32 {
        (self.page - 1) * self.limit
    }

    /// Returns the ending item index for the current page (0-indexed, exclusive).
    ///
    /// # Example
    ///
    /// ```
    /// use ghost_io_api::models::pagination::Pagination;
    ///
    /// let pagination = Pagination {
    ///     page: 2,
    ///     limit: 15,
    ///     pages: 5,
    ///     total: 73,
    ///     next: Some(3),
    ///     prev: Some(1),
    /// };
    ///
    /// // Page 2, limit 15: ends at index 30 (exclusive)
    /// assert_eq!(pagination.end_index(), 30);
    /// ```
    pub fn end_index(&self) -> u32 {
        self.start_index() + self.items_on_page()
    }
}

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

    #[test]
    fn test_pagination_deserialization() {
        let json = json!({
            "page": 1,
            "limit": 15,
            "pages": 5,
            "total": 73,
            "next": 2,
            "prev": null
        });

        let pagination: Pagination = serde_json::from_value(json).unwrap();
        assert_eq!(pagination.page, 1);
        assert_eq!(pagination.limit, 15);
        assert_eq!(pagination.pages, 5);
        assert_eq!(pagination.total, 73);
        assert_eq!(pagination.next, Some(2));
        assert_eq!(pagination.prev, None);
    }

    #[test]
    fn test_pagination_middle_page() {
        let json = json!({
            "page": 3,
            "limit": 10,
            "pages": 5,
            "total": 47,
            "next": 4,
            "prev": 2
        });

        let pagination: Pagination = serde_json::from_value(json).unwrap();
        assert_eq!(pagination.page, 3);
        assert_eq!(pagination.next, Some(4));
        assert_eq!(pagination.prev, Some(2));
    }

    #[test]
    fn test_pagination_last_page() {
        let json = json!({
            "page": 5,
            "limit": 15,
            "pages": 5,
            "total": 73,
            "next": null,
            "prev": 4
        });

        let pagination: Pagination = serde_json::from_value(json).unwrap();
        assert_eq!(pagination.page, 5);
        assert_eq!(pagination.next, None);
        assert_eq!(pagination.prev, Some(4));
    }

    #[test]
    fn test_meta_deserialization() {
        let json = json!({
            "pagination": {
                "page": 1,
                "limit": 15,
                "pages": 5,
                "total": 73,
                "next": 2,
                "prev": null
            }
        });

        let meta: Meta = serde_json::from_value(json).unwrap();
        assert_eq!(meta.pagination.page, 1);
        assert_eq!(meta.pagination.total, 73);
    }

    #[test]
    fn test_has_next() {
        let pagination = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert!(pagination.has_next());

        let last_page = Pagination {
            page: 5,
            limit: 15,
            pages: 5,
            total: 73,
            next: None,
            prev: Some(4),
        };
        assert!(!last_page.has_next());
    }

    #[test]
    fn test_has_prev() {
        let pagination = Pagination {
            page: 2,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(3),
            prev: Some(1),
        };
        assert!(pagination.has_prev());

        let first_page = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert!(!first_page.has_prev());
    }

    #[test]
    fn test_is_first_page() {
        let pagination = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert!(pagination.is_first_page());

        let not_first = Pagination {
            page: 2,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(3),
            prev: Some(1),
        };
        assert!(!not_first.is_first_page());
    }

    #[test]
    fn test_is_last_page() {
        let pagination = Pagination {
            page: 5,
            limit: 15,
            pages: 5,
            total: 73,
            next: None,
            prev: Some(4),
        };
        assert!(pagination.is_last_page());

        let not_last = Pagination {
            page: 4,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(5),
            prev: Some(3),
        };
        assert!(!not_last.is_last_page());
    }

    #[test]
    fn test_items_on_page() {
        // Full page
        let full_page = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert_eq!(full_page.items_on_page(), 15);

        // Last page with partial items: 73 - (4 * 15) = 13
        let last_page = Pagination {
            page: 5,
            limit: 15,
            pages: 5,
            total: 73,
            next: None,
            prev: Some(4),
        };
        assert_eq!(last_page.items_on_page(), 13);
    }

    #[test]
    fn test_start_index() {
        let page_1 = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert_eq!(page_1.start_index(), 0);

        let page_2 = Pagination {
            page: 2,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(3),
            prev: Some(1),
        };
        assert_eq!(page_2.start_index(), 15);

        let page_5 = Pagination {
            page: 5,
            limit: 15,
            pages: 5,
            total: 73,
            next: None,
            prev: Some(4),
        };
        assert_eq!(page_5.start_index(), 60);
    }

    #[test]
    fn test_end_index() {
        let page_1 = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };
        assert_eq!(page_1.end_index(), 15);

        let page_2 = Pagination {
            page: 2,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(3),
            prev: Some(1),
        };
        assert_eq!(page_2.end_index(), 30);

        // Last page: starts at 60, has 13 items
        let page_5 = Pagination {
            page: 5,
            limit: 15,
            pages: 5,
            total: 73,
            next: None,
            prev: Some(4),
        };
        assert_eq!(page_5.end_index(), 73);
    }

    #[test]
    fn test_pagination_serialization() {
        let pagination = Pagination {
            page: 2,
            limit: 10,
            pages: 5,
            total: 47,
            next: Some(3),
            prev: Some(1),
        };

        let json = serde_json::to_value(&pagination).unwrap();
        assert_eq!(json["page"], 2);
        assert_eq!(json["limit"], 10);
        assert_eq!(json["pages"], 5);
        assert_eq!(json["total"], 47);
        assert_eq!(json["next"], 3);
        assert_eq!(json["prev"], 1);
    }

    #[test]
    fn test_pagination_serialization_with_nulls() {
        let pagination = Pagination {
            page: 1,
            limit: 15,
            pages: 1,
            total: 10,
            next: None,
            prev: None,
        };

        let json = serde_json::to_value(&pagination).unwrap();
        // Fields with None should be omitted due to skip_serializing_if
        assert!(!json.as_object().unwrap().contains_key("next"));
        assert!(!json.as_object().unwrap().contains_key("prev"));
    }

    #[test]
    fn test_single_page() {
        let pagination = Pagination {
            page: 1,
            limit: 15,
            pages: 1,
            total: 10,
            next: None,
            prev: None,
        };

        assert!(pagination.is_first_page());
        assert!(pagination.is_last_page());
        assert!(!pagination.has_next());
        assert!(!pagination.has_prev());
        assert_eq!(pagination.items_on_page(), 10);
        assert_eq!(pagination.start_index(), 0);
        assert_eq!(pagination.end_index(), 10);
    }

    #[test]
    fn test_meta_serialization() {
        let meta = Meta {
            pagination: Pagination {
                page: 1,
                limit: 15,
                pages: 5,
                total: 73,
                next: Some(2),
                prev: None,
            },
        };

        let json = serde_json::to_value(&meta).unwrap();
        assert_eq!(json["pagination"]["page"], 1);
        assert_eq!(json["pagination"]["total"], 73);
    }

    #[test]
    fn test_pagination_clone_and_eq() {
        let pagination = Pagination {
            page: 1,
            limit: 15,
            pages: 5,
            total: 73,
            next: Some(2),
            prev: None,
        };

        let cloned = pagination.clone();
        assert_eq!(pagination, cloned);
    }

    #[test]
    fn test_meta_clone_and_eq() {
        let meta = Meta {
            pagination: Pagination {
                page: 1,
                limit: 15,
                pages: 5,
                total: 73,
                next: Some(2),
                prev: None,
            },
        };

        let cloned = meta.clone();
        assert_eq!(meta, cloned);
    }
}