diff --git a/src/my_clippings.rs b/src/my_clippings.rs index 7212043..0296d66 100644 --- a/src/my_clippings.rs +++ b/src/my_clippings.rs @@ -2,7 +2,8 @@ use crate::note::*; use anyhow::{anyhow, Result}; use std::collections::HashMap; -pub fn parse(data: &str) -> Result<(HashMap>, Vec)> { + +pub fn parse(data: &str) -> Result<(BookNotes, Vec)> { let mut output: HashMap> = HashMap::new(); let mut titles_last_seen_indice: HashMap = HashMap::new(); for (i, note) in data.split("==========\r\n").enumerate() { @@ -14,13 +15,13 @@ pub fn parse(data: &str) -> Result<(HashMap>, Vec titles_last_seen_indice.insert(highlight.name.to_string(), to_insert); let entry = output .entry(highlight.name.to_string()) - .or_insert_with(Vec::new); + .or_default(); entry.push(highlight); } } let mut ordered_titles: Vec<(usize, String)> = titles_last_seen_indice .iter() - .map(|(k, v)| (v.clone(), k.clone())) + .map(|(k, v)| (*v, k.clone())) .collect(); ordered_titles.sort(); let only_ordered_titles: Vec = ordered_titles.iter().map(|(_, k)| k.clone()).collect(); diff --git a/src/note.rs b/src/note.rs index 50d9cc0..bb5672c 100644 --- a/src/note.rs +++ b/src/note.rs @@ -1,3 +1,5 @@ +pub type BookNotes<'a> = std::collections::HashMap>>; + #[derive(Debug)] pub struct Highlight<'a> { pub name: &'a str, @@ -19,13 +21,13 @@ impl<'a> std::fmt::Display for Highlight<'a> { HighlightType::Highlight => "", HighlightType::Comment => "NOTE: ", }; - write!(f, "{}{}", prefix, self.highlight.replace("\r", "")) + write!(f, "{}{}", prefix, self.highlight.replace('\r', "")) } } impl<'a> Highlight<'a> { pub fn filestem(&self) -> String { - let bad_chars = vec!['(', ')', ',', ':']; + let bad_chars = ['(', ')', ',', ':']; let letter_tidier = |letter| { if bad_chars.contains(&letter) { "".to_string() diff --git a/src/web_export.rs b/src/web_export.rs index 00b202d..583c96b 100644 --- a/src/web_export.rs +++ b/src/web_export.rs @@ -17,14 +17,14 @@ fn get_h3_title(data: &str) -> Result<&str> { Ok(title) } -pub fn parse(data: &str) -> Result<(HashMap>, Vec)> { +pub fn parse(data: &str) -> Result<(BookNotes, Vec)> { let title = get_h3_title(data)?; let re_hi_or_note = Regex::new(r#"(?s)(.*?)"#) .with_context(|| "Failed to create regex for webexport highlight/note")?; let mut output: HashMap> = HashMap::new(); for cap in re_hi_or_note.captures_iter(data) { - let entry = output.entry(title.to_string()).or_insert_with(Vec::new); - let tidy_entry = cap[2].replace("\r", "").replace("\n", ""); + let entry = output.entry(title.to_string()).or_default(); + let tidy_entry = cap[2].replace(['\r', '\n'], " "); if !tidy_entry.is_empty() { let highlight_type = match &cap[1] { "highlight" => HighlightType::Highlight,