1pub(crate) use aidaemon_tools::text::truncate_str as truncate_chars;
5
6pub(crate) fn words(text: &str) -> impl Iterator<Item = String> + '_ {
11 text.split(|c: char| !c.is_alphanumeric())
12 .filter(|word| !word.is_empty())
13 .map(str::to_lowercase)
14}
15
16pub(crate) fn normalize_fact_value(value: &str) -> String {
19 words(value).collect::<Vec<_>>().join(" ")
20}
21
22pub(crate) fn canonicalize_fact_key(key: &str) -> String {
23 let raw = key.trim();
24 if raw.is_empty() {
25 return String::new();
26 }
27
28 let mut out = String::with_capacity(raw.len());
29 let mut last_was_sep = false;
30 for ch in raw.chars() {
31 if ch.is_ascii_alphanumeric() {
32 out.push(ch.to_ascii_lowercase());
33 last_was_sep = false;
34 } else if !last_was_sep {
35 out.push('_');
36 last_was_sep = true;
37 }
38 }
39
40 let trimmed = out.trim_matches('_');
41 if trimmed.is_empty() {
42 raw.to_string()
43 } else {
44 trimmed.to_string()
45 }
46}
47
48pub fn canonical_personal_key(key: &str) -> String {
51 match canonicalize_fact_key(key).as_str() {
52 "birthday" | "date_of_birth" | "dob" => "birth_date".to_string(),
53 "current_residence" | "home" | "location" => "residence".to_string(),
54 "place_of_birth" => "birthplace".to_string(),
55 "favorite_name" | "preferred_first_name" => "preferred_name".to_string(),
56 "address_as" | "call_me" | "preferred_address" | "salutation" => "addressing".to_string(),
57 "avoid_names" | "rejected_name" | "rejected_names" | "do_not_call" => {
58 "rejected_alias".to_string()
59 }
60 other => other.to_string(),
61 }
62}
63
64pub fn owner_projection_family(key: &str) -> String {
67 match canonical_personal_key(key).as_str() {
68 "name" | "full_name" | "legal_name_variant" => "name".to_string(),
69 "username" | "online_nickname" | "online_handle" | "handle" => "online_handle".to_string(),
70 other => other.to_string(),
71 }
72}
73
74pub fn is_owner_name_key(key: &str) -> bool {
77 matches!(
78 canonical_personal_key(key).as_str(),
79 "name"
80 | "full_name"
81 | "legal_name_variant"
82 | "first_name"
83 | "last_name"
84 | "preferred_name"
85 | "nickname"
86 | "addressing"
87 | "rejected_alias"
88 )
89}
90
91pub fn is_owner_profile_key(key: &str) -> bool {
93 is_owner_name_key(key)
94 || matches!(
95 canonical_personal_key(key).as_str(),
96 "username"
97 | "online_nickname"
98 | "online_handle"
99 | "handle"
100 | "birth_date"
101 | "residence"
102 | "birthplace"
103 )
104}