From 66220780383bb0f1bc07d8db3466dd937d146a6f Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 5 Jan 2021 21:48:35 +0100 Subject: [PATCH] DRY things a bit --- components/library/src/library.rs | 45 ++++++++++++------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/components/library/src/library.rs b/components/library/src/library.rs index dbca7194..d75b0aa3 100644 --- a/components/library/src/library.rs +++ b/components/library/src/library.rs @@ -62,27 +62,29 @@ impl Library { } } + fn insert_reverse_aliases(&mut self, entries: Vec, file_rel_path: &str) { + for entry in entries { + self.reverse_aliases + .entry(entry) + .and_modify(|s| { + s.insert(file_rel_path.to_owned()); + }) + .or_insert_with(|| { + let mut s = HashSet::new(); + s.insert(file_rel_path.to_owned()); + s + }); + } + } + /// Add a section and return its Key pub fn insert_section(&mut self, section: Section) -> DefaultKey { let file_path = section.file.path.clone(); - let file_rel_path = section.file.relative.clone(); let rel_path = section.path.clone(); let mut entries = vec![rel_path.clone()]; entries.extend(section.meta.aliases.iter().map(|a| a.clone()).collect::>()); - - for entry in &entries { - self.reverse_aliases - .entry(entry.to_string()) - .and_modify(|s| { - s.insert(file_rel_path.clone()); - }) - .or_insert_with(|| { - let mut s = HashSet::new(); - s.insert(file_rel_path.clone()); - s - }); - } + self.insert_reverse_aliases(entries, §ion.file.relative); let key = self.sections.insert(section); self.paths_to_sections.insert(file_path, key); @@ -92,24 +94,11 @@ impl Library { /// Add a page and return its Key pub fn insert_page(&mut self, page: Page) -> DefaultKey { let file_path = page.file.path.clone(); - let file_rel_path = page.file.relative.clone(); let rel_path = page.path.clone(); let mut entries = vec![rel_path.clone()]; entries.extend(page.meta.aliases.iter().map(|a| a.clone()).collect::>()); - - for entry in &entries { - self.reverse_aliases - .entry(entry.to_string()) - .and_modify(|s| { - s.insert(file_rel_path.clone()); - }) - .or_insert_with(|| { - let mut s = HashSet::new(); - s.insert(file_rel_path.clone()); - s - }); - } + self.insert_reverse_aliases(entries, &page.file.relative); let key = self.pages.insert(page);