diff --git a/src/db/dir.rs b/src/db/dir.rs index 43d6448..4dae2cf 100644 --- a/src/db/dir.rs +++ b/src/db/dir.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashSet; use std::fmt::{self, Display, Formatter}; use serde::{Deserialize, Serialize}; @@ -12,7 +13,7 @@ pub struct DirV4<'a> { pub rank: Rank, pub last_accessed: Epoch, #[serde(borrow)] - pub aliases: Vec>, + pub aliases: HashSet>, } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -26,7 +27,7 @@ pub struct DirV3<'a> { pub trait Dir { fn path(&self) -> &str; fn score(&self, now: Epoch) -> Rank; - fn aliases(&self) -> &[Cow<'_, str>]; + fn aliases(&self) -> Option<&HashSet>>; } impl Dir for DirV4<'_> { @@ -48,8 +49,8 @@ impl Dir for DirV4<'_> { } } - fn aliases(&self) -> &[Cow<'_, str>] { - &self.aliases + fn aliases(&self) -> Option<&HashSet>> { + Some(&self.aliases) } } @@ -78,8 +79,8 @@ impl Dir for DirV3<'_> { } } - fn aliases(&self) -> &[Cow<'_, str>] { - return &[]; + fn aliases(&self) -> Option<&HashSet>> { + None } } @@ -118,8 +119,10 @@ impl<'a, T: Dir> Display for DirDisplay<'a, T> { write!(f, "{score:>6.1}{}", self.separator)?; } - if self.aliases { - for alias in self.dir.aliases() { + if self.aliases + && let Some(aliases) = self.dir.aliases() + { + for alias in aliases { write!(f, "{} ", alias)?; } write!(f, "{}", self.separator)?; diff --git a/src/db/mod.rs b/src/db/mod.rs index dab0cf0..046297c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,7 @@ mod dir; mod stream; +use std::collections::HashSet; use std::path::{Path, PathBuf}; use std::{fs, io}; @@ -78,12 +79,15 @@ impl Database { Some(dir) => { dir.rank = (dir.rank + by).max(0.0); if let Some(al) = alias { - dir.aliases.push(al.into().into()); + dir.aliases.insert(al.into().into()); } } None => { - let aliases = - if let Some(alias) = alias { vec![alias.into().into()] } else { Vec::new() }; + let aliases = if let Some(alias) = alias { + HashSet::from([alias.into().into()]) + } else { + HashSet::new() + }; dirs.push(DirV4 { path: path.into().into(), rank: by.max(0.0), @@ -107,8 +111,11 @@ impl Database { alias: Option + Into>, ) { self.with_dirs_mut(|dirs| { - let aliases = - if let Some(alias) = alias { vec![alias.into().into()] } else { Vec::new() }; + let aliases = if let Some(alias) = alias { + HashSet::from([alias.into().into()]) + } else { + HashSet::new() + }; dirs.push(DirV4 { path: path.into().into(), rank, last_accessed: now, aliases }) }); self.with_dirty_mut(|dirty| *dirty = true); @@ -128,12 +135,15 @@ impl Database { dir.rank = (dir.rank + by).max(0.0); dir.last_accessed = now; if let Some(al) = alias { - dir.aliases.push(al.into().into()); + dir.aliases.insert(al.into().into()); } } None => { - let aliases = - if let Some(alias) = alias { vec![alias.into().into()] } else { Vec::new() }; + let aliases = if let Some(alias) = alias { + HashSet::from([alias.into().into()]) + } else { + HashSet::new() + }; dirs.push(DirV4 { path: path.into().into(), rank: by.max(0.0), @@ -278,7 +288,7 @@ impl Database { path: dir.path, rank: dir.rank, last_accessed: dir.last_accessed, - aliases: Vec::new(), + aliases: HashSet::new(), }) .collect() } diff --git a/src/db/stream.rs b/src/db/stream.rs index 62d2efc..aa9251c 100644 --- a/src/db/stream.rs +++ b/src/db/stream.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashSet; use std::iter::Rev; use std::ops::Range; use std::path::Path; @@ -109,10 +110,10 @@ impl<'a> Stream<'a> { true } - fn match_aliases(&self, aliases: &[Cow<'a, str>]) -> bool { + fn match_aliases(&self, aliases: &HashSet>) -> bool { for keyword in self.options.keywords.iter().rev() { // Alias matching is intended to be case-sensitive - if aliases.iter().any(|a| a == keyword.as_str()) { + if aliases.contains(keyword.as_str()) { return true; } } diff --git a/src/import/atuin.rs b/src/import/atuin.rs index 7596013..310e850 100644 --- a/src/import/atuin.rs +++ b/src/import/atuin.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashSet; use std::io::{BufRead, BufReader}; use std::process::{Child, ChildStdout, Command, Stdio}; use std::str; @@ -64,7 +65,7 @@ impl Iter { path: Cow::Owned(path.to_string()), rank: 1.0, last_accessed: timestamp as Epoch, - aliases: Vec::new(), + aliases: HashSet::new(), }; Ok(dir) } diff --git a/src/import/autojump.rs b/src/import/autojump.rs index 06ba392..df59fb9 100644 --- a/src/import/autojump.rs +++ b/src/import/autojump.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashSet; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::PathBuf; @@ -56,7 +57,7 @@ impl Iter { path: Cow::Owned(path.to_string()), rank, last_accessed: 0, - aliases: Vec::new(), + aliases: HashSet::new(), }) } } diff --git a/src/import/z.rs b/src/import/z.rs index d43bcaf..43e51c5 100644 --- a/src/import/z.rs +++ b/src/import/z.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashSet; use std::fs::File; use std::io::{BufRead, BufReader}; use std::path::PathBuf; @@ -54,7 +55,12 @@ impl Iter { let path = split.next().ok_or_else(err)?; - Ok(DirV4 { path: Cow::Owned(path.to_string()), rank, last_accessed, aliases: Vec::new() }) + Ok(DirV4 { + path: Cow::Owned(path.to_string()), + rank, + last_accessed, + aliases: HashSet::new(), + }) } }