//! Generic tag matching: the `osmium tags-filter` equivalent. //! //! Each layer script narrows a PBF with an expression like //! //! ```text //! osmium tags-filter n/highway=speed_camera,stop,traffic_signals \ //! n/man_made=surveillance \ //! n/enforcement=maxspeed //! ``` //! //! [`Select`] is that expression: a set of `key=v1,v2` clauses, matching when //! **any** of them does, which is what osmium's OR semantics mean. A clause with //! no `=` matches on key presence alone (`w/maxspeed`). //! //! ## Why a lookup closure rather than a tag map //! //! [`Select::matches`] takes `impl Fn(&str) -> Option<&str>`, the shape //! [`crate::osm::Tags::get_str`] already has. That means no map is built per //! element on the hot path, and -- more usefully -- a selector can be unit-tested //! against a plain array of pairs with no PBF anywhere in sight, exactly like the //! classifiers in [`crate::tags`]. //! //! This is a pre-filter, not the layer's schema decision. It exists to skip the //! overwhelming majority of elements cheaply; the per-layer classifier still has //! the final say on what is emitted and as what. use crate::proto::{Error, Result}; /// One `key` or `key=v1,v2` clause. #[derive(Debug, Clone, PartialEq)] pub struct Clause { pub key: String, /// Empty means "any value", i.e. match on the key being present at all. pub values: Vec, } impl Clause { fn matches(&self, value: Option<&str>) -> bool { match value { None => false, Some(v) => { if self.values.is_empty() { // osmium's bare `w/maxspeed` matches a present key whatever // its value, including the empty string. true } else { self.values.iter().any(|w| w == v) } } } } } #[derive(Debug, Clone, Default, PartialEq)] pub struct Select { pub clauses: Vec, } impl Select { /// Parse one clause per element of `specs`, in the `key` or `key=v1,v2` form. pub fn parse(specs: &[&str]) -> Result