Browse Source

Update slotmap

index-subcmd
Vincent Prouillet 5 years ago
parent
commit
5844047435
8 changed files with 275 additions and 287 deletions
  1. +234
    -246
      Cargo.lock
  2. +1
    -1
      components/library/Cargo.toml
  3. +7
    -7
      components/library/src/content/page.rs
  4. +6
    -6
      components/library/src/content/section.rs
  5. +17
    -17
      components/library/src/library.rs
  6. +2
    -2
      components/library/src/pagination/mod.rs
  7. +4
    -4
      components/library/src/sorting.rs
  8. +4
    -4
      components/library/src/taxonomies/mod.rs

+ 234
- 246
Cargo.lock
File diff suppressed because it is too large
View File


+ 1
- 1
components/library/Cargo.toml View File

@@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Vincent Prouillet <prouillet.vincent@gmail.com>"] authors = ["Vincent Prouillet <prouillet.vincent@gmail.com>"]


[dependencies] [dependencies]
slotmap = "0.2"
slotmap = "0.4"
rayon = "1" rayon = "1"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
tera = "1.0.0-beta.10" tera = "1.0.0-beta.10"


+ 7
- 7
components/library/src/content/page.rs View File

@@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};


use regex::Regex; use regex::Regex;
use slotmap::Key;
use slotmap::DefaultKey;
use slug::slugify; use slug::slugify;
use tera::{Context as TeraContext, Tera}; use tera::{Context as TeraContext, Tera};


@@ -35,7 +35,7 @@ pub struct Page {
/// The front matter meta-data /// The front matter meta-data
pub meta: PageFrontMatter, pub meta: PageFrontMatter,
/// The list of parent sections /// The list of parent sections
pub ancestors: Vec<Key>,
pub ancestors: Vec<DefaultKey>,
/// The actual content of the page, in markdown /// The actual content of the page, in markdown
pub raw_content: String, pub raw_content: String,
/// All the non-md files we found next to the .md file /// All the non-md files we found next to the .md file
@@ -58,13 +58,13 @@ pub struct Page {
/// as summary /// as summary
pub summary: Option<String>, pub summary: Option<String>,
/// The earlier page, for pages sorted by date /// The earlier page, for pages sorted by date
pub earlier: Option<Key>,
pub earlier: Option<DefaultKey>,
/// The later page, for pages sorted by date /// The later page, for pages sorted by date
pub later: Option<Key>,
pub later: Option<DefaultKey>,
/// The lighter page, for pages sorted by weight /// The lighter page, for pages sorted by weight
pub lighter: Option<Key>,
pub lighter: Option<DefaultKey>,
/// The heavier page, for pages sorted by weight /// The heavier page, for pages sorted by weight
pub heavier: Option<Key>,
pub heavier: Option<DefaultKey>,
/// Toc made from the headings of the markdown file /// Toc made from the headings of the markdown file
pub toc: Vec<Heading>, pub toc: Vec<Heading>,
/// How many words in the raw content /// How many words in the raw content
@@ -76,7 +76,7 @@ pub struct Page {
/// Corresponds to the lang in the {slug}.{lang}.md file scheme /// Corresponds to the lang in the {slug}.{lang}.md file scheme
pub lang: String, pub lang: String,
/// Contains all the translated version of that page /// Contains all the translated version of that page
pub translations: Vec<Key>,
pub translations: Vec<DefaultKey>,
/// Contains the internal links that have an anchor: we can only check the anchor /// Contains the internal links that have an anchor: we can only check the anchor
/// after all pages have been built and their ToC compiled. The page itself should exist otherwise /// after all pages have been built and their ToC compiled. The page itself should exist otherwise
/// it would have errored before getting there /// it would have errored before getting there


+ 6
- 6
components/library/src/content/section.rs View File

@@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};


use slotmap::Key;
use slotmap::DefaultKey;
use tera::{Context as TeraContext, Tera}; use tera::{Context as TeraContext, Tera};


use config::Config; use config::Config;
@@ -38,13 +38,13 @@ pub struct Section {
/// All the non-md files we found next to the .md file as string for use in templates /// All the non-md files we found next to the .md file as string for use in templates
pub serialized_assets: Vec<String>, pub serialized_assets: Vec<String>,
/// All direct pages of that section /// All direct pages of that section
pub pages: Vec<Key>,
pub pages: Vec<DefaultKey>,
/// All pages that cannot be sorted in this section /// All pages that cannot be sorted in this section
pub ignored_pages: Vec<Key>,
pub ignored_pages: Vec<DefaultKey>,
/// The list of parent sections /// The list of parent sections
pub ancestors: Vec<Key>,
pub ancestors: Vec<DefaultKey>,
/// All direct subsections /// All direct subsections
pub subsections: Vec<Key>,
pub subsections: Vec<DefaultKey>,
/// Toc made from the headings of the markdown file /// Toc made from the headings of the markdown file
pub toc: Vec<Heading>, pub toc: Vec<Heading>,
/// How many words in the raw content /// How many words in the raw content
@@ -56,7 +56,7 @@ pub struct Section {
/// Corresponds to the lang in the _index.{lang}.md file scheme /// Corresponds to the lang in the _index.{lang}.md file scheme
pub lang: String, pub lang: String,
/// Contains all the translated version of that section /// Contains all the translated version of that section
pub translations: Vec<Key>,
pub translations: Vec<DefaultKey>,
/// Contains the internal links that have an anchor: we can only check the anchor /// Contains the internal links that have an anchor: we can only check the anchor
/// after all pages have been built and their ToC compiled. The page itself should exist otherwise /// after all pages have been built and their ToC compiled. The page itself should exist otherwise
/// it would have errored before getting there /// it would have errored before getting there


+ 17
- 17
components/library/src/library.rs View File

@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};


use slotmap::{DenseSlotMap, Key};
use slotmap::{DenseSlotMap, DefaultKey};


use front_matter::SortBy; use front_matter::SortBy;


@@ -19,13 +19,13 @@ use sorting::{find_siblings, sort_pages_by_date, sort_pages_by_weight};
#[derive(Debug)] #[derive(Debug)]
pub struct Library { pub struct Library {
/// All the pages of the site /// All the pages of the site
pages: DenseSlotMap<Page>,
pages: DenseSlotMap<DefaultKey, Page>,
/// All the sections of the site /// All the sections of the site
sections: DenseSlotMap<Section>,
sections: DenseSlotMap<DefaultKey, Section>,
/// A mapping path -> key for pages so we can easily get their key /// A mapping path -> key for pages so we can easily get their key
pub paths_to_pages: HashMap<PathBuf, Key>,
pub paths_to_pages: HashMap<PathBuf, DefaultKey>,
/// A mapping path -> key for sections so we can easily get their key /// A mapping path -> key for sections so we can easily get their key
pub paths_to_sections: HashMap<PathBuf, Key>,
pub paths_to_sections: HashMap<PathBuf, DefaultKey>,
/// Whether we need to look for translations /// Whether we need to look for translations
is_multilingual: bool, is_multilingual: bool,
} }
@@ -42,7 +42,7 @@ impl Library {
} }


/// Add a section and return its Key /// Add a section and return its Key
pub fn insert_section(&mut self, section: Section) -> Key {
pub fn insert_section(&mut self, section: Section) -> DefaultKey {
let path = section.file.path.clone(); let path = section.file.path.clone();
let key = self.sections.insert(section); let key = self.sections.insert(section);
self.paths_to_sections.insert(path, key); self.paths_to_sections.insert(path, key);
@@ -50,18 +50,18 @@ impl Library {
} }


/// Add a page and return its Key /// Add a page and return its Key
pub fn insert_page(&mut self, page: Page) -> Key {
pub fn insert_page(&mut self, page: Page) -> DefaultKey {
let path = page.file.path.clone(); let path = page.file.path.clone();
let key = self.pages.insert(page); let key = self.pages.insert(page);
self.paths_to_pages.insert(path, key); self.paths_to_pages.insert(path, key);
key key
} }


pub fn pages(&self) -> &DenseSlotMap<Page> {
pub fn pages(&self) -> &DenseSlotMap<DefaultKey, Page> {
&self.pages &self.pages
} }


pub fn pages_mut(&mut self) -> &mut DenseSlotMap<Page> {
pub fn pages_mut(&mut self) -> &mut DenseSlotMap<DefaultKey, Page> {
&mut self.pages &mut self.pages
} }


@@ -69,11 +69,11 @@ impl Library {
self.pages.values().collect::<Vec<_>>() self.pages.values().collect::<Vec<_>>()
} }


pub fn sections(&self) -> &DenseSlotMap<Section> {
pub fn sections(&self) -> &DenseSlotMap<DefaultKey, Section> {
&self.sections &self.sections
} }


pub fn sections_mut(&mut self) -> &mut DenseSlotMap<Section> {
pub fn sections_mut(&mut self) -> &mut DenseSlotMap<DefaultKey, Section> {
&mut self.sections &mut self.sections
} }


@@ -336,7 +336,7 @@ impl Library {
} }


/// Only used in tests /// Only used in tests
pub fn get_section_key<P: AsRef<Path>>(&self, path: P) -> Option<&Key> {
pub fn get_section_key<P: AsRef<Path>>(&self, path: P) -> Option<&DefaultKey> {
self.paths_to_sections.get(path.as_ref()) self.paths_to_sections.get(path.as_ref())
} }


@@ -349,15 +349,15 @@ impl Library {
.get_mut(self.paths_to_sections.get(path.as_ref()).cloned().unwrap_or_default()) .get_mut(self.paths_to_sections.get(path.as_ref()).cloned().unwrap_or_default())
} }


pub fn get_section_by_key(&self, key: Key) -> &Section {
pub fn get_section_by_key(&self, key: DefaultKey) -> &Section {
self.sections.get(key).unwrap() self.sections.get(key).unwrap()
} }


pub fn get_section_mut_by_key(&mut self, key: Key) -> &mut Section {
pub fn get_section_mut_by_key(&mut self, key: DefaultKey) -> &mut Section {
self.sections.get_mut(key).unwrap() self.sections.get_mut(key).unwrap()
} }


pub fn get_section_path_by_key(&self, key: Key) -> &str {
pub fn get_section_path_by_key(&self, key: DefaultKey) -> &str {
&self.get_section_by_key(key).file.relative &self.get_section_by_key(key).file.relative
} }


@@ -365,11 +365,11 @@ impl Library {
self.pages.get(self.paths_to_pages.get(path.as_ref()).cloned().unwrap_or_default()) self.pages.get(self.paths_to_pages.get(path.as_ref()).cloned().unwrap_or_default())
} }


pub fn get_page_by_key(&self, key: Key) -> &Page {
pub fn get_page_by_key(&self, key: DefaultKey) -> &Page {
self.pages.get(key).unwrap() self.pages.get(key).unwrap()
} }


pub fn get_page_mut_by_key(&mut self, key: Key) -> &mut Page {
pub fn get_page_mut_by_key(&mut self, key: DefaultKey) -> &mut Page {
self.pages.get_mut(key).unwrap() self.pages.get_mut(key).unwrap()
} }




+ 2
- 2
components/library/src/pagination/mod.rs View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;


use slotmap::Key;
use slotmap::DefaultKey;
use tera::{to_value, Context, Tera, Value}; use tera::{to_value, Context, Tera, Value};


use config::Config; use config::Config;
@@ -44,7 +44,7 @@ impl<'a> Pager<'a> {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Paginator<'a> { pub struct Paginator<'a> {
/// All pages in the section/taxonomy /// All pages in the section/taxonomy
all_pages: &'a [Key],
all_pages: &'a [DefaultKey],
/// Pages split in chunks of `paginate_by` /// Pages split in chunks of `paginate_by`
pub pagers: Vec<Pager<'a>>, pub pagers: Vec<Pager<'a>>,
/// How many content pages on a paginated page at max /// How many content pages on a paginated page at max


+ 4
- 4
components/library/src/sorting.rs View File

@@ -2,7 +2,7 @@ use std::cmp::Ordering;


use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use rayon::prelude::*; use rayon::prelude::*;
use slotmap::Key;
use slotmap::DefaultKey;


use content::Page; use content::Page;


@@ -21,7 +21,7 @@ pub fn sort_actual_pages_by_date(a: &&Page, b: &&Page) -> Ordering {
/// Takes a list of (page key, date, permalink) and sort them by dates if possible /// Takes a list of (page key, date, permalink) and sort them by dates if possible
/// Pages without date will be put in the unsortable bucket /// Pages without date will be put in the unsortable bucket
/// The permalink is used to break ties /// The permalink is used to break ties
pub fn sort_pages_by_date(pages: Vec<(&Key, Option<NaiveDateTime>, &str)>) -> (Vec<Key>, Vec<Key>) {
pub fn sort_pages_by_date(pages: Vec<(&DefaultKey, Option<NaiveDateTime>, &str)>) -> (Vec<DefaultKey>, Vec<DefaultKey>) {
let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) = let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) =
pages.into_par_iter().partition(|page| page.1.is_some()); pages.into_par_iter().partition(|page| page.1.is_some());


@@ -40,7 +40,7 @@ pub fn sort_pages_by_date(pages: Vec<(&Key, Option<NaiveDateTime>, &str)>) -> (V
/// Takes a list of (page key, weight, permalink) and sort them by weight if possible /// Takes a list of (page key, weight, permalink) and sort them by weight if possible
/// Pages without weight will be put in the unsortable bucket /// Pages without weight will be put in the unsortable bucket
/// The permalink is used to break ties /// The permalink is used to break ties
pub fn sort_pages_by_weight(pages: Vec<(&Key, Option<usize>, &str)>) -> (Vec<Key>, Vec<Key>) {
pub fn sort_pages_by_weight(pages: Vec<(&DefaultKey, Option<usize>, &str)>) -> (Vec<DefaultKey>, Vec<DefaultKey>) {
let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) = let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) =
pages.into_par_iter().partition(|page| page.1.is_some()); pages.into_par_iter().partition(|page| page.1.is_some());


@@ -57,7 +57,7 @@ pub fn sort_pages_by_weight(pages: Vec<(&Key, Option<usize>, &str)>) -> (Vec<Key
} }


/// Find the lighter/heavier and earlier/later pages for all pages having a date/weight /// Find the lighter/heavier and earlier/later pages for all pages having a date/weight
pub fn find_siblings(sorted: &[Key]) -> Vec<(Key, Option<Key>, Option<Key>)> {
pub fn find_siblings(sorted: &[DefaultKey]) -> Vec<(DefaultKey, Option<DefaultKey>, Option<DefaultKey>)> {
let mut res = Vec::with_capacity(sorted.len()); let mut res = Vec::with_capacity(sorted.len());
let length = sorted.len(); let length = sorted.len();




+ 4
- 4
components/library/src/taxonomies/mod.rs View File

@@ -1,6 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;


use slotmap::Key;
use slotmap::DefaultKey;
use slug::slugify; use slug::slugify;
use tera::{Context, Tera}; use tera::{Context, Tera};


@@ -44,7 +44,7 @@ pub struct TaxonomyItem {
pub name: String, pub name: String,
pub slug: String, pub slug: String,
pub permalink: String, pub permalink: String,
pub pages: Vec<Key>,
pub pages: Vec<DefaultKey>,
} }


impl TaxonomyItem { impl TaxonomyItem {
@@ -52,7 +52,7 @@ impl TaxonomyItem {
name: &str, name: &str,
taxonomy: &TaxonomyConfig, taxonomy: &TaxonomyConfig,
config: &Config, config: &Config,
keys: Vec<Key>,
keys: Vec<DefaultKey>,
library: &Library, library: &Library,
) -> Self { ) -> Self {
// Taxonomy are almost always used for blogs so we filter by dates // Taxonomy are almost always used for blogs so we filter by dates
@@ -113,7 +113,7 @@ impl Taxonomy {
fn new( fn new(
kind: TaxonomyConfig, kind: TaxonomyConfig,
config: &Config, config: &Config,
items: HashMap<String, Vec<Key>>,
items: HashMap<String, Vec<DefaultKey>>,
library: &Library, library: &Library,
) -> Taxonomy { ) -> Taxonomy {
let mut sorted_items = vec![]; let mut sorted_items = vec![];


Loading…
Cancel
Save