I was just writing the PR and realised that aliases is a better name than keys

This commit is contained in:
Book-reader 2025-05-08 20:18:00 +12:00
parent 3696d4cb6d
commit 24067eca99

View file

@ -8,12 +8,12 @@ pub static BANG_PREFIX: &str = "!";
#[derive(Debug)] #[derive(Debug)]
struct Bang<'a> { struct Bang<'a> {
pub url: &'a str, pub url: &'a str,
pub keys: &'a [&'a str] pub aliases: &'a [&'a str]
} }
impl<'a> Bang<'_> { impl<'a> Bang<'_> {
fn new(url: &'a str, keys: &'a [&'a str]) -> Bang<'a> { fn new(url: &'a str, aliases: &'a [&'a str]) -> Bang<'a> {
Bang {url, keys} Bang {url, aliases}
} }
} }
@ -53,15 +53,15 @@ pub fn redirect_bang(query: &String) -> Option<String> {
}).map(|(bang_start_idx, _)| { }).map(|(bang_start_idx, _)| {
let rest = query.get(bang_start_idx + 1..query.len()).unwrap(); let rest = query.get(bang_start_idx + 1..query.len()).unwrap();
BUILTIN_BANGS.iter().map(|(_, bang)| { BUILTIN_BANGS.iter().map(|(_, bang)| {
let key = bang.keys.iter() let alias = bang.aliases.iter()
.filter(|key| rest.starts_with(**key)) .filter(|alias| rest.starts_with(**alias))
.filter( .filter(
|key| rest.chars() |alias| rest.chars()
.nth(key.len()) .nth(alias.len())
.unwrap_or(' ') .unwrap_or(' ')
.is_whitespace()) .is_whitespace())
.max_by(|a, b| a.len().cmp(&b.len()))?; .max_by(|a, b| a.len().cmp(&b.len()))?;
Some(BangLoc::new(bang.url, bang_start_idx, key.len())) Some(BangLoc::new(bang.url, bang_start_idx, alias.len()))
}).filter(|bang| bang.is_some()).map(|bang| bang.unwrap()).next() }).filter(|bang| bang.is_some()).map(|bang| bang.unwrap()).next()
}).filter(|bang| bang.is_some()) }).filter(|bang| bang.is_some())
.map(|bang| bang.unwrap()) .map(|bang| bang.unwrap())