-
Notifications
You must be signed in to change notification settings - Fork 97
/
categories.rs
187 lines (168 loc) · 5.63 KB
/
categories.rs
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
use std::cmp::Ordering;
use crate::document::Document;
use super::*;
use helpers::extract_categories;
use paginator::Paginator;
#[derive(Debug)]
struct Category<'a> {
cat_path: liquid::model::Array,
posts: Vec<&'a liquid::model::Value>,
sub_cats: Vec<Category<'a>>,
}
impl<'a> Category<'a> {
fn new() -> Self {
Category {
cat_path: vec![],
posts: vec![],
sub_cats: vec![],
}
}
fn with_path<'v>(path: impl Iterator<Item = &'v dyn liquid::ValueView>) -> Self {
Category {
cat_path: path.map(|v| v.to_value()).collect(),
posts: vec![],
sub_cats: vec![],
}
}
fn add_post(&mut self, post: &'a liquid::model::Value) {
self.posts.push(post)
}
}
fn compare_category_path<'a, C, S>(cur_path: C, seek: S) -> Ordering
where
C: Iterator<Item = &'a dyn liquid::ValueView>,
S: Iterator<Item = &'a dyn liquid::ValueView>,
{
cur_path
.map(liquid::model::ValueViewCmp::new)
.partial_cmp(seek.map(liquid::model::ValueViewCmp::new))
.expect("Arrays of same hierarchy level should be fully comparable")
}
fn is_leaf_category(cur_idx: usize, categories: &[&dyn liquid::ValueView]) -> bool {
cur_idx == categories.len()
}
fn construct_cat_full_path<'v>(
cur_idx: usize,
categories: &[&'v dyn liquid::model::ValueView],
) -> Vec<&'v dyn liquid::model::ValueView> {
categories[..cur_idx].to_vec()
}
fn next_category(cur_idx: usize) -> usize {
cur_idx + 1
}
// construct a hierarchy of Categories with their posts from a list of categories
fn parse_categories_list<'a>(
parent: &mut Category<'a>,
cur_idx: usize,
cur_post_categories: &[&dyn liquid::ValueView],
post: &'a liquid::model::Value,
) -> Result<()> {
if cur_idx <= cur_post_categories.len() {
let cat_full_path = construct_cat_full_path(cur_idx, cur_post_categories);
let cur_cat = if let Ok(idx) = parent.sub_cats.binary_search_by(|c| {
compare_category_path(
c.cat_path.iter().map(|v| v.as_view()),
cat_full_path.iter().copied(),
)
}) {
&mut parent.sub_cats[idx]
} else {
let last_idx = parent.sub_cats.len();
parent
.sub_cats
.push(Category::with_path(cat_full_path.into_iter()));
// need to sort for binary_search_by
parent.sub_cats.sort_by(|c1, c2| {
compare_category_path(
c1.cat_path.iter().map(|v| v.as_view()),
c2.cat_path.iter().map(|v| v.as_view()),
)
});
&mut parent.sub_cats[last_idx]
};
if is_leaf_category(cur_idx, cur_post_categories) {
cur_cat.add_post(post);
} else {
parse_categories_list(cur_cat, next_category(cur_idx), cur_post_categories, post)?;
}
}
Ok(())
}
fn distribute_posts_by_categories<'a>(
all_posts: &[&'a liquid::model::Value],
) -> Result<Category<'a>> {
let mut root = Category::new();
for post in all_posts {
if let Some(categories) = extract_categories(post.as_view()) {
let categories: Vec<_> = categories.values().collect();
parse_categories_list(&mut root, 1, categories.as_slice(), post)?;
}
}
Ok(root)
}
// walk the categories tree and construct Paginator for each node,
// filling `pages` and `indexes` accordingly
fn walk_categories(
category: &mut Category<'_>,
config: &PaginationConfig,
doc: &Document,
) -> Result<Vec<Paginator>> {
let mut cur_cat_paginators_holder: Vec<Paginator> = vec![];
if !category.cat_path.is_empty() {
sort_posts(&mut category.posts, config);
let cur_cat_paginators = create_all_paginators(
&category.posts,
doc,
config,
Some(&liquid::model::Value::array(category.cat_path.clone())),
)?;
if !cur_cat_paginators.is_empty() {
cur_cat_paginators_holder.extend(cur_cat_paginators);
} else {
let p = Paginator {
index_title: Some(liquid::model::Value::array(category.cat_path.clone())),
..Default::default()
};
cur_cat_paginators_holder.push(p);
}
} else {
cur_cat_paginators_holder.push(Paginator::default());
}
for c in &mut category.sub_cats {
let mut sub_paginators_holder = walk_categories(c, config, doc)?;
if let Some(indexes) = cur_cat_paginators_holder[0].indexes.as_mut() {
indexes.push(sub_paginators_holder[0].clone());
} else {
cur_cat_paginators_holder[0].indexes = Some(vec![sub_paginators_holder[0].clone()]);
}
cur_cat_paginators_holder.append(&mut sub_paginators_holder);
}
Ok(cur_cat_paginators_holder)
}
pub fn create_categories_paginators(
all_posts: &[&liquid::model::Value],
doc: &Document,
pagination_cfg: &PaginationConfig,
) -> Result<Vec<Paginator>> {
let mut root_cat = distribute_posts_by_categories(all_posts)?;
let paginators_holder = walk_categories(&mut root_cat, pagination_cfg, doc)?;
Ok(paginators_holder)
}
#[cfg(test)]
mod test {
use super::*;
use liquid::model::ArrayView;
#[test]
fn compare_category_path_test() {
let a = liquid::model::array!(["A"]);
let b = liquid::model::array!(["B"]);
assert_eq!(
Ordering::Less,
compare_category_path(a.values(), b.values())
);
assert_eq!(
Ordering::Greater,
compare_category_path(b.values(), a.values())
);
}
}