Skip to content
177 changes: 92 additions & 85 deletions src/guides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ use std::collections::HashMap;
use cot_site_common::md_pages::{MdPage, MdPageLink};
use cot_site_macros::md_page;

use crate::GuideLinkCategory;
use crate::{GuideCategoryItem, GuideItem, GuideLinkCategory};

pub fn parse_guides(categories: Vec<(&'static str, Vec<MdPage>)>) -> ParsedPagesForVersion {
pub fn parse_guides(categories: Vec<(&'static str, Vec<GuideItem>)>) -> ParsedPagesForVersion {
let categories_links = categories
.iter()
.map(|(title, guides)| GuideLinkCategory {
.map(|(title, items)| GuideLinkCategory {
title,
guides: guides.iter().map(MdPageLink::from).collect(),
guides: items
.iter()
.map(|item| match item {
GuideItem::Page(page) => GuideCategoryItem::Page(MdPageLink::from(page)),
GuideItem::SubCategory { title, pages } => GuideCategoryItem::SubCategory {
title,
pages: pages.iter().map(MdPageLink::from).collect(),
},
})
.collect(),
})
.collect();

let guide_map = categories
.into_iter()
.flat_map(|(_title, guides)| guides)
.map(|guide| (guide.link.clone(), guide))
.flat_map(|(_title, items)| items)
.flat_map(|item| match item {
GuideItem::Page(page) => vec![page],
GuideItem::SubCategory { pages, .. } => pages,
})
.map(|page| (page.link.clone(), page))
.collect();

ParsedPagesForVersion {
Expand All @@ -40,39 +54,46 @@ pub fn get_prev_next_link<'a>(
guides: &'a [GuideLinkCategory],
current_id: &str,
) -> (Option<&'a MdPageLink>, Option<&'a MdPageLink>) {
let all_links: Vec<&MdPageLink> = guides
.iter()
.flat_map(|category| category.guides.iter())
.flat_map(|item| match item {
GuideCategoryItem::Page(link) => vec![link],
GuideCategoryItem::SubCategory { pages, .. } => pages.iter().collect(),
})
.collect();

let mut prev = None;
let mut has_found = false;

for category in guides {
for guide in &category.guides {
if has_found {
return (prev, Some(guide));
} else if guide.link == current_id {
has_found = true;
} else {
prev = Some(guide);
}
for link in all_links {
if has_found {
return (prev, Some(link));
} else if link.link == current_id {
has_found = true;
} else {
prev = Some(link);
}
}

(prev, None)
}

pub(crate) fn get_categories(master_version: Vec<(&'static str, Vec<MdPage>)>) -> ParsedPages {
pub fn get_categories(master_version: Vec<(&'static str, Vec<GuideItem>)>) -> ParsedPages {
let version_map = HashMap::from([
(
"v0.1",
vec![(
"Getting started",
vec![
md_page!("v0.1", "introduction"),
md_page!("v0.1", "templates"),
md_page!("v0.1", "forms"),
md_page!("v0.1", "db-models"),
md_page!("v0.1", "admin-panel"),
md_page!("v0.1", "static-files"),
md_page!("v0.1", "error-pages"),
md_page!("v0.1", "testing"),
GuideItem::Page(md_page!("v0.1", "introduction")),
GuideItem::Page(md_page!("v0.1", "templates")),
GuideItem::Page(md_page!("v0.1", "forms")),
GuideItem::Page(md_page!("v0.1", "db-models")),
GuideItem::Page(md_page!("v0.1", "admin-panel")),
GuideItem::Page(md_page!("v0.1", "static-files")),
GuideItem::Page(md_page!("v0.1", "error-pages")),
GuideItem::Page(md_page!("v0.1", "testing")),
],
)],
),
Expand All @@ -81,14 +102,14 @@ pub(crate) fn get_categories(master_version: Vec<(&'static str, Vec<MdPage>)>) -
vec![(
"Getting started",
vec![
md_page!("v0.2", "introduction"),
md_page!("v0.2", "templates"),
md_page!("v0.2", "forms"),
md_page!("v0.2", "db-models"),
md_page!("v0.2", "admin-panel"),
md_page!("v0.2", "static-files"),
md_page!("v0.2", "error-pages"),
md_page!("v0.2", "testing"),
GuideItem::Page(md_page!("v0.2", "introduction")),
GuideItem::Page(md_page!("v0.2", "templates")),
GuideItem::Page(md_page!("v0.2", "forms")),
GuideItem::Page(md_page!("v0.2", "db-models")),
GuideItem::Page(md_page!("v0.2", "admin-panel")),
GuideItem::Page(md_page!("v0.2", "static-files")),
GuideItem::Page(md_page!("v0.2", "error-pages")),
GuideItem::Page(md_page!("v0.2", "testing")),
],
)],
),
Expand All @@ -97,15 +118,15 @@ pub(crate) fn get_categories(master_version: Vec<(&'static str, Vec<MdPage>)>) -
vec![(
"Getting started",
vec![
md_page!("v0.3", "introduction"),
md_page!("v0.3", "templates"),
md_page!("v0.3", "forms"),
md_page!("v0.3", "db-models"),
md_page!("v0.3", "admin-panel"),
md_page!("v0.3", "static-files"),
md_page!("v0.3", "error-pages"),
md_page!("v0.3", "openapi"),
md_page!("v0.3", "testing"),
GuideItem::Page(md_page!("v0.3", "introduction")),
GuideItem::Page(md_page!("v0.3", "templates")),
GuideItem::Page(md_page!("v0.3", "forms")),
GuideItem::Page(md_page!("v0.3", "db-models")),
GuideItem::Page(md_page!("v0.3", "admin-panel")),
GuideItem::Page(md_page!("v0.3", "static-files")),
GuideItem::Page(md_page!("v0.3", "error-pages")),
GuideItem::Page(md_page!("v0.3", "openapi")),
GuideItem::Page(md_page!("v0.3", "testing")),
],
)],
),
Expand All @@ -115,18 +136,21 @@ pub(crate) fn get_categories(master_version: Vec<(&'static str, Vec<MdPage>)>) -
(
"Getting started",
vec![
md_page!("v0.4", "introduction"),
md_page!("v0.4", "templates"),
md_page!("v0.4", "forms"),
md_page!("v0.4", "db-models"),
md_page!("v0.4", "admin-panel"),
md_page!("v0.4", "static-files"),
md_page!("v0.4", "error-pages"),
md_page!("v0.4", "openapi"),
md_page!("v0.4", "testing"),
GuideItem::Page(md_page!("v0.4", "introduction")),
GuideItem::Page(md_page!("v0.4", "templates")),
GuideItem::Page(md_page!("v0.4", "forms")),
GuideItem::Page(md_page!("v0.4", "db-models")),
GuideItem::Page(md_page!("v0.4", "admin-panel")),
GuideItem::Page(md_page!("v0.4", "static-files")),
GuideItem::Page(md_page!("v0.4", "error-pages")),
GuideItem::Page(md_page!("v0.4", "openapi")),
GuideItem::Page(md_page!("v0.4", "testing")),
],
),
("Upgrading", vec![md_page!("v0.4", "upgrade-guide")]),
(
"Upgrading",
vec![GuideItem::Page(md_page!("v0.4", "upgrade-guide"))],
),
],
),
(
Expand All @@ -135,44 +159,27 @@ pub(crate) fn get_categories(master_version: Vec<(&'static str, Vec<MdPage>)>) -
(
"Getting started",
vec![
md_page!("v0.5", "introduction"),
md_page!("v0.5", "templates"),
md_page!("v0.5", "forms"),
md_page!("v0.5", "db-models"),
md_page!("v0.5", "admin-panel"),
md_page!("v0.5", "static-files"),
md_page!("v0.5", "sending-emails"),
md_page!("v0.5", "caching"),
md_page!("v0.5", "error-pages"),
md_page!("v0.5", "openapi"),
md_page!("v0.5", "testing"),
GuideItem::Page(md_page!("v0.5", "introduction")),
GuideItem::Page(md_page!("v0.5", "templates")),
GuideItem::Page(md_page!("v0.5", "forms")),
GuideItem::Page(md_page!("v0.5", "db-models")),
GuideItem::Page(md_page!("v0.5", "admin-panel")),
GuideItem::Page(md_page!("v0.5", "static-files")),
GuideItem::Page(md_page!("v0.5", "sending-emails")),
GuideItem::Page(md_page!("v0.5", "caching")),
GuideItem::Page(md_page!("v0.5", "error-pages")),
GuideItem::Page(md_page!("v0.5", "openapi")),
GuideItem::Page(md_page!("v0.5", "testing")),
],
),
("Upgrading", vec![md_page!("v0.5", "upgrade-guide")]),
("About", vec![md_page!("v0.5", "framework-comparison")]),
],
),
(
"v0.6",
vec![
(
"Getting started",
vec![
md_page!("v0.6", "introduction"),
md_page!("v0.6", "templates"),
md_page!("v0.6", "forms"),
md_page!("v0.6", "db-models"),
md_page!("v0.6", "admin-panel"),
md_page!("v0.6", "static-files"),
md_page!("v0.6", "sending-emails"),
md_page!("v0.6", "caching"),
md_page!("v0.6", "error-pages"),
md_page!("v0.6", "openapi"),
md_page!("v0.6", "testing"),
],
"Upgrading",
vec![GuideItem::Page(md_page!("v0.5", "upgrade-guide"))],
),
(
"About",
vec![GuideItem::Page(md_page!("v0.5", "framework-comparison"))],
),
("Upgrading", vec![md_page!("v0.6", "upgrade-guide")]),
("About", vec![md_page!("v0.6", "framework-comparison")]),
],
),
("master", master_version),
Expand Down
Loading
Loading