cobalt_config/
pagination.rs

1use std::vec::Vec;
2
3use super::*;
4
5const DEFAULT_PER_PAGE: i32 = 10;
6const DEFAULT_PERMALINK: &str = "{{num}}/";
7const DEFAULT_SORT: &str = "published_date";
8
9#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Default)]
10#[serde(default)]
11#[serde(rename_all = "snake_case")]
12#[cfg_attr(feature = "unstable", serde(deny_unknown_fields))]
13#[cfg_attr(not(feature = "unstable"), non_exhaustive)]
14pub struct Pagination {
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub include: Option<Include>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub per_page: Option<i32>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub permalink_suffix: Option<liquid_core::model::KString>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub order: Option<SortOrder>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub sort_by: Option<Vec<liquid_core::model::KString>>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub date_index: Option<Vec<DateIndex>>,
27}
28
29impl Pagination {
30    pub fn empty() -> Self {
31        Self::default()
32    }
33
34    pub fn with_defaults() -> Self {
35        Self {
36            include: Some(Include::None),
37            per_page: Some(DEFAULT_PER_PAGE),
38            permalink_suffix: Some(DEFAULT_PERMALINK.into()),
39            order: Some(SortOrder::Desc),
40            sort_by: Some(vec![DEFAULT_SORT.into()]),
41            date_index: Some(vec![DateIndex::Year, DateIndex::Month]),
42        }
43    }
44
45    pub fn merge(self, other: &Self) -> Self {
46        let Pagination {
47            include,
48            per_page,
49            permalink_suffix,
50            order,
51            sort_by,
52            date_index,
53        } = self;
54        Self {
55            include: include.or(other.include),
56            per_page: per_page.or(other.per_page),
57            permalink_suffix: permalink_suffix.or_else(|| other.permalink_suffix.clone()),
58            order: order.or(other.order),
59            sort_by: sort_by.or_else(|| other.sort_by.clone()),
60            date_index: date_index.or_else(|| other.date_index.clone()),
61        }
62    }
63}
64
65#[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
66#[cfg_attr(feature = "preview_unstable", serde(rename_all = "snake_case"))]
67#[cfg_attr(feature = "unstable", serde(deny_unknown_fields))]
68#[cfg_attr(not(feature = "unstable"), non_exhaustive)]
69#[derive(Default)]
70pub enum Include {
71    #[default]
72    None,
73    All,
74    Tags,
75    Categories,
76    Dates,
77    #[cfg(not(feature = "unstable"))]
78    #[doc(hidden)]
79    #[serde(other)]
80    Unknown,
81}
82
83#[derive(
84    Copy, Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, PartialOrd, Ord,
85)]
86#[cfg_attr(feature = "preview_unstable", serde(rename_all = "snake_case"))]
87#[cfg_attr(feature = "unstable", serde(deny_unknown_fields))]
88#[cfg_attr(not(feature = "unstable"), non_exhaustive)]
89pub enum DateIndex {
90    Year,
91    Month,
92    Day,
93    Hour,
94    Minute,
95    #[cfg(not(feature = "unstable"))]
96    #[doc(hidden)]
97    #[serde(other)]
98    Unknown,
99}