You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.0KB

  1. use std::cmp::Ordering;
  2. use chrono::NaiveDateTime;
  3. use rayon::prelude::*;
  4. use slotmap::Key;
  5. use content::Page;
  6. /// Used by the RSS feed
  7. /// There to not have to import sorting stuff in the site crate
  8. pub fn sort_actual_pages_by_date(a: &&Page, b: &&Page) -> Ordering {
  9. let ord = b.meta.datetime.unwrap().cmp(&a.meta.datetime.unwrap());
  10. if ord == Ordering::Equal {
  11. a.permalink.cmp(&b.permalink)
  12. } else {
  13. ord
  14. }
  15. }
  16. /// Takes a list of (page key, date, permalink) and sort them by dates if possible
  17. /// Pages without date will be put in the unsortable bucket
  18. /// The permalink is used to break ties
  19. pub fn sort_pages_by_date(pages: Vec<(&Key, Option<NaiveDateTime>, &str)>) -> (Vec<Key>, Vec<Key>) {
  20. let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) =
  21. pages.into_par_iter().partition(|page| page.1.is_some());
  22. can_be_sorted.par_sort_unstable_by(|a, b| {
  23. let ord = b.1.unwrap().cmp(&a.1.unwrap());
  24. if ord == Ordering::Equal {
  25. a.2.cmp(&b.2)
  26. } else {
  27. ord
  28. }
  29. });
  30. (can_be_sorted.iter().map(|p| *p.0).collect(), cannot_be_sorted.iter().map(|p| *p.0).collect())
  31. }
  32. /// Takes a list of (page key, weight, permalink) and sort them by weight if possible
  33. /// Pages without weight will be put in the unsortable bucket
  34. /// The permalink is used to break ties
  35. pub fn sort_pages_by_weight(pages: Vec<(&Key, Option<usize>, &str)>) -> (Vec<Key>, Vec<Key>) {
  36. let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) =
  37. pages.into_par_iter().partition(|page| page.1.is_some());
  38. can_be_sorted.par_sort_unstable_by(|a, b| {
  39. let ord = a.1.unwrap().cmp(&b.1.unwrap());
  40. if ord == Ordering::Equal {
  41. a.2.cmp(&b.2)
  42. } else {
  43. ord
  44. }
  45. });
  46. (can_be_sorted.iter().map(|p| *p.0).collect(), cannot_be_sorted.iter().map(|p| *p.0).collect())
  47. }
  48. /// Find the lighter/heavier and earlier/later pages for all pages having a date/weight
  49. /// and that are not drafts.
  50. pub fn find_siblings(sorted: Vec<(&Key, bool)>) -> Vec<(Key, Option<Key>, Option<Key>)> {
  51. let mut res = Vec::with_capacity(sorted.len());
  52. let length = sorted.len();
  53. for (i, (key, is_draft)) in sorted.iter().enumerate() {
  54. if *is_draft {
  55. res.push((**key, None, None));
  56. continue;
  57. }
  58. let mut with_siblings = (**key, None, None);
  59. if i > 0 {
  60. let mut j = i;
  61. loop {
  62. if j == 0 {
  63. break;
  64. }
  65. j -= 1;
  66. if sorted[j].1 {
  67. continue;
  68. }
  69. // lighter / later
  70. with_siblings.1 = Some(*sorted[j].0);
  71. break;
  72. }
  73. }
  74. if i < length - 1 {
  75. let mut j = i;
  76. loop {
  77. if j == length - 1 {
  78. break;
  79. }
  80. j += 1;
  81. if sorted[j].1 {
  82. continue;
  83. }
  84. // heavier/earlier
  85. with_siblings.2 = Some(*sorted[j].0);
  86. break;
  87. }
  88. }
  89. res.push(with_siblings);
  90. }
  91. res
  92. }
  93. #[cfg(test)]
  94. mod tests {
  95. use slotmap::DenseSlotMap;
  96. use super::{find_siblings, sort_pages_by_date, sort_pages_by_weight};
  97. use content::Page;
  98. use front_matter::PageFrontMatter;
  99. fn create_page_with_date(date: &str) -> Page {
  100. let mut front_matter = PageFrontMatter::default();
  101. front_matter.date = Some(date.to_string());
  102. front_matter.date_to_datetime();
  103. Page::new("content/hello.md", front_matter)
  104. }
  105. fn create_page_with_weight(weight: usize) -> Page {
  106. let mut front_matter = PageFrontMatter::default();
  107. front_matter.weight = Some(weight);
  108. Page::new("content/hello.md", front_matter)
  109. }
  110. #[test]
  111. fn can_sort_by_dates() {
  112. let mut dense = DenseSlotMap::new();
  113. let page1 = create_page_with_date("2018-01-01");
  114. let key1 = dense.insert(page1.clone());
  115. let page2 = create_page_with_date("2017-01-01");
  116. let key2 = dense.insert(page2.clone());
  117. let page3 = create_page_with_date("2019-01-01");
  118. let key3 = dense.insert(page3.clone());
  119. let input = vec![
  120. (&key1, page1.meta.datetime, page1.permalink.as_ref()),
  121. (&key2, page2.meta.datetime, page2.permalink.as_ref()),
  122. (&key3, page3.meta.datetime, page3.permalink.as_ref()),
  123. ];
  124. let (pages, _) = sort_pages_by_date(input);
  125. // Should be sorted by date
  126. assert_eq!(pages[0], key3);
  127. assert_eq!(pages[1], key1);
  128. assert_eq!(pages[2], key2);
  129. }
  130. #[test]
  131. fn can_sort_by_weight() {
  132. let mut dense = DenseSlotMap::new();
  133. let page1 = create_page_with_weight(2);
  134. let key1 = dense.insert(page1.clone());
  135. let page2 = create_page_with_weight(3);
  136. let key2 = dense.insert(page2.clone());
  137. let page3 = create_page_with_weight(1);
  138. let key3 = dense.insert(page3.clone());
  139. let input = vec![
  140. (&key1, page1.meta.weight, page1.permalink.as_ref()),
  141. (&key2, page2.meta.weight, page2.permalink.as_ref()),
  142. (&key3, page3.meta.weight, page3.permalink.as_ref()),
  143. ];
  144. let (pages, _) = sort_pages_by_weight(input);
  145. // Should be sorted by weight
  146. assert_eq!(pages[0], key3);
  147. assert_eq!(pages[1], key1);
  148. assert_eq!(pages[2], key2);
  149. }
  150. #[test]
  151. fn ignore_page_with_missing_field() {
  152. let mut dense = DenseSlotMap::new();
  153. let page1 = create_page_with_weight(2);
  154. let key1 = dense.insert(page1.clone());
  155. let page2 = create_page_with_weight(3);
  156. let key2 = dense.insert(page2.clone());
  157. let page3 = create_page_with_date("2019-01-01");
  158. let key3 = dense.insert(page3.clone());
  159. let input = vec![
  160. (&key1, page1.meta.weight, page1.permalink.as_ref()),
  161. (&key2, page2.meta.weight, page2.permalink.as_ref()),
  162. (&key3, page3.meta.weight, page3.permalink.as_ref()),
  163. ];
  164. let (pages, unsorted) = sort_pages_by_weight(input);
  165. assert_eq!(pages.len(), 2);
  166. assert_eq!(unsorted.len(), 1);
  167. }
  168. #[test]
  169. fn can_find_siblings() {
  170. let mut dense = DenseSlotMap::new();
  171. let page1 = create_page_with_weight(1);
  172. let key1 = dense.insert(page1.clone());
  173. let page2 = create_page_with_weight(2);
  174. let key2 = dense.insert(page2.clone());
  175. let page3 = create_page_with_weight(3);
  176. let key3 = dense.insert(page3.clone());
  177. let input =
  178. vec![(&key1, page1.is_draft()), (&key2, page2.is_draft()), (&key3, page3.is_draft())];
  179. let pages = find_siblings(input);
  180. assert_eq!(pages[0].1, None);
  181. assert_eq!(pages[0].2, Some(key2));
  182. assert_eq!(pages[1].1, Some(key1));
  183. assert_eq!(pages[1].2, Some(key3));
  184. assert_eq!(pages[2].1, Some(key2));
  185. assert_eq!(pages[2].2, None);
  186. }
  187. }