AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
//! Entity detection (semantic tags): email / url / ip / uuid.
//! A field is tagged when ≥90% of ≥20 non-null sampled values match.
use regex::Regex;
use std::sync::OnceLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Entity {
Email,
Url,
Ip,
Uuid,
}
impl Entity {
pub fn as_str(&self) -> &'static str {
match self {
Entity::Email => "email",
Entity::Url => "url",
Entity::Ip => "ip",
Entity::Uuid => "uuid",
}
}
/// Tie-break rank for a field-level election, lowest wins. Kept in the
/// order `classify` below tries the tests in — most specific first — so an
/// ambiguous FIELD resolves the way an ambiguous VALUE does: an exact
/// `IpAddr` parse, then the fixed 36-char uuid shape, then email, then the
/// loosest test, url. Distinct per variant, which is what makes an
/// ordering built on it total. Change this only alongside `classify`.
pub fn precedence(&self) -> u8 {
match self {
Entity::Ip => 0,
Entity::Uuid => 1,
Entity::Email => 2,
Entity::Url => 3,
}
}
}
struct Res {
email: Regex,
url: Regex,
uuid: Regex,
}
fn res() -> &'static Res {
static R: OnceLock<Res> = OnceLock::new();
R.get_or_init(|| Res {
email: Regex::new(r"^[^@\s]{1,64}@[^@\s]+\.[A-Za-z]{2,}$").unwrap(),
url: Regex::new(r"^[a-z][a-z0-9+.-]*://\S+$").unwrap(),
uuid: Regex::new(
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
)
.unwrap(),
})
}
pub fn classify(s: &str) -> Option<Entity> {
let t = s.trim();
if t.is_empty() || t.len() > 512 {
return None;
}
// IpAddr::parse is exact — version strings like "1.2.3" fail it.
if t.parse::<std::net::IpAddr>().is_ok() {
return Some(Entity::Ip);
}
let r = res();
if r.uuid.is_match(t) {
return Some(Entity::Uuid);
}
if r.email.is_match(t) {
return Some(Entity::Email);
}
if r.url.is_match(t) {
return Some(Entity::Url);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entities() {
assert_eq!(classify("[email protected]"), Some(Entity::Email));
assert_eq!(classify("https://x.example/y"), Some(Entity::Url));
assert_eq!(classify("192.231.71.123"), Some(Entity::Ip));
assert_eq!(classify("::1"), Some(Entity::Ip));
assert_eq!(classify("1.2.3"), None);
assert_eq!(
classify("741e7b6b-dbd2-4a7f-93a9-4ba50fb561d3"),
Some(Entity::Uuid)
);
assert_eq!(classify("hello"), None);
}
}