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)]
struct Bang<'a> {
pub url: &'a str,
pub keys: &'a [&'a str]
pub aliases: &'a [&'a str]
}
impl<'a> Bang<'_> {
fn new(url: &'a str, keys: &'a [&'a str]) -> Bang<'a> {
Bang {url, keys}
fn new(url: &'a str, aliases: &'a [&'a str]) -> Bang<'a> {
Bang {url, aliases}
}
}
@ -53,15 +53,15 @@ pub fn redirect_bang(query: &String) -> Option<String> {
}).map(|(bang_start_idx, _)| {
let rest = query.get(bang_start_idx + 1..query.len()).unwrap();
BUILTIN_BANGS.iter().map(|(_, bang)| {
let key = bang.keys.iter()
.filter(|key| rest.starts_with(**key))
let alias = bang.aliases.iter()
.filter(|alias| rest.starts_with(**alias))
.filter(
|key| rest.chars()
.nth(key.len())
|alias| rest.chars()
.nth(alias.len())
.unwrap_or(' ')
.is_whitespace())
.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())