For country-based lookups... hmm...
let country = COUNTRY
.as_country_matcher()?
.lookup(request.header("x-forwarded-for");
if BANNED_COUNTRIES.matches(country) {
return garbage("banned-country");
}
This'd work. We can use AhoCorasick here, because the .lookup()
function in this case will return the ISO code of the country, always two characters, and always unique, so there's no partial matching.
This is pretty much fine as it is. One has to decide whether to use a matcher, or a .contains()
on a string list (the latter requires less boilerplate; the difference in speed in small sets should be negligible though), but other than that, I don't think I have much to improve for this case.