Packages
rbt_weave
0.2.67
0.2.67
0.2.66
0.2.65
0.2.64
0.2.63
0.2.62
0.2.61
0.2.60
0.2.59
0.2.58
0.2.57
0.2.56
0.2.55
0.2.54
0.2.53
0.2.52
0.2.51
0.2.50
0.2.49
0.2.48
0.2.47
0.2.46
0.2.45
0.2.44
0.2.43
0.2.42
0.2.41
0.2.40
0.2.39
0.2.38
0.2.37
0.2.36
0.2.35
0.2.34
0.2.33
0.2.32
0.2.31
0.2.30
0.2.29
0.2.28
0.2.27
0.2.26
0.2.25
0.2.24
0.2.23
0.2.22
0.2.21
0.2.20
0.2.19
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Graph conflict detection and image processing NIFs for OSINT knowledge graphs, powered by Rust.
Current section
Files
Jump to
Current section
Files
crates/weave-content/src/registry.rs
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use rayon::prelude::*;
use unicode_normalization::UnicodeNormalization;
use crate::entity::{Entity, Label};
use crate::parser::ParseError;
/// Maximum length of entity filename stem (without `.md`).
const MAX_FILENAME_LEN: usize = 200;
/// A loaded entity with its source file path.
#[derive(Debug)]
pub struct RegistryEntry {
pub entity: Entity,
pub path: PathBuf,
pub tags: Vec<String>,
}
/// Entity registry: holds all shared entities loaded from `people/` and
/// `organizations/` directories. Provides name-based lookup for cross-file
/// resolution.
#[derive(Debug)]
pub struct EntityRegistry {
entries: Vec<RegistryEntry>,
/// Name → index into `entries`. Names are case-sensitive.
name_index: HashMap<String, usize>,
/// Content root directory for computing file-path slugs.
content_root: Option<PathBuf>,
}
impl EntityRegistry {
/// Build a registry from a content root directory.
///
/// Scans `{root}/people/**/*.md` and `{root}/organizations/**/*.md`, parses
/// each file, validates for duplicates and filename mismatches. Supports both
/// flat and nested (country-based) directory layouts.
pub fn load(root: &Path) -> Result<Self, Vec<ParseError>> {
let mut entries = Vec::new();
let mut errors = Vec::new();
let actor_dir = root.join("people");
let institution_dir = root.join("organizations");
load_directory(&actor_dir, Label::Person, &mut entries, &mut errors);
load_directory(
&institution_dir,
Label::Organization,
&mut entries,
&mut errors,
);
// Build name index and detect duplicates
let name_index = build_name_index(&entries, &mut errors);
if errors.iter().any(|e| e.message.starts_with("duplicate")) {
return Err(errors);
}
// Filename mismatch warnings are non-fatal, report via errors but don't fail
// (caller can filter by message prefix if needed)
if errors.iter().any(|e| !e.message.starts_with("warning:")) {
return Err(errors);
}
// Warnings only -- attach them but succeed
if !errors.is_empty() {
for err in &errors {
eprintln!("{err}");
}
}
Ok(Self {
entries,
name_index,
content_root: Some(root.to_path_buf()),
})
}
/// Build a registry from pre-parsed entries.
pub fn from_entries(entries: Vec<RegistryEntry>) -> Result<Self, Vec<ParseError>> {
let mut errors = Vec::new();
let name_index = build_name_index(&entries, &mut errors);
let has_errors = errors.iter().any(|e| !e.message.starts_with("warning:"));
if has_errors {
return Err(errors);
}
Ok(Self {
entries,
name_index,
content_root: None,
})
}
/// Look up an entity by name. Returns None if not found.
pub fn get_by_name(&self, name: &str) -> Option<&RegistryEntry> {
self.name_index.get(name).map(|&idx| &self.entries[idx])
}
/// Number of entities in the registry.
pub fn len(&self) -> usize {
self.entries.len()
}
/// Whether the registry is empty.
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
/// All entity names in the registry.
pub fn names(&self) -> Vec<&str> {
self.entries
.iter()
.map(|e| e.entity.name.as_str())
.collect()
}
/// All registry entries.
pub fn entries(&self) -> &[RegistryEntry] {
&self.entries
}
/// Compute the file-path slug for an entry (path relative to content root, minus `.md`).
/// Returns `None` if content root is not set.
pub fn slug_for(&self, entry: &RegistryEntry) -> Option<String> {
let root = self.content_root.as_ref()?;
path_to_slug(&entry.path, root)
}
/// Content root directory, if set.
pub fn content_root(&self) -> Option<&Path> {
self.content_root.as_deref()
}
/// Check all entity filenames against expected naming convention.
/// Returns warning messages for mismatches (same checks as `load()`,
/// but accessible after loading for strict validation).
pub fn check_filenames(&self) -> Vec<ParseError> {
let mut warnings = Vec::new();
for entry in &self.entries {
validate_filename(&entry.path, &entry.entity, &mut warnings);
}
warnings
}
}
/// Compute file-path slug from an absolute path relative to content root.
/// Returns the path minus the `.md` extension, e.g. `people/id/harvey-moeis`.
/// Strips diacritics from the filename stem for ASCII-safe URLs.
pub fn path_to_slug(path: &Path, content_root: &Path) -> Option<String> {
let relative = path.strip_prefix(content_root).ok()?;
let s = relative.to_str()?;
// NFC-normalize to handle macOS NFD filesystem paths, then strip
// diacritics from the filename portion for ASCII-safe URL slugs.
let nfc: String = s.nfc().collect();
let without_ext = nfc.strip_suffix(".md").unwrap_or(&nfc);
// Only transliterate the filename, not directory segments (country codes etc.)
if let Some((dir, stem)) = without_ext.rsplit_once('/') {
let ascii_stem = strip_diacritics(stem);
Some(format!("{dir}/{ascii_stem}"))
} else {
Some(strip_diacritics(without_ext))
}
}
/// Strip diacritics from a string by NFD-decomposing and removing combining marks.
/// Also transliterates non-decomposable Latin variants (ł→l, đ→d, ı→i, ß→ss, etc.).
fn strip_diacritics(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.nfd().filter(|c| !is_combining_mark(*c)) {
match transliterate_latin(c) {
Some(replacement) => out.push_str(replacement),
None => out.push(c),
}
}
out
}
/// Transliterate non-decomposable Latin characters to ASCII equivalents.
/// NFD decomposition handles most diacritics (é→e+combining), but some
/// characters like ł, đ, ı are atomic and need explicit mapping.
/// Returns `Some` for characters that need transliteration, `None` to keep as-is.
fn transliterate_latin(c: char) -> Option<&'static str> {
match c {
'ł' => Some("l"),
'Ł' => Some("L"),
'đ' | 'ð' => Some("d"),
'Đ' | 'Ð' => Some("D"),
'ı' => Some("i"), // Turkish dotless i
'ø' => Some("o"),
'Ø' => Some("O"),
'æ' => Some("ae"),
'Æ' => Some("AE"),
'œ' => Some("oe"),
'Œ' => Some("OE"),
'ß' => Some("ss"), // German sharp s
'þ' => Some("th"), // Icelandic thorn
'Þ' => Some("TH"),
_ => None,
}
}
/// Load all `.md` files from a directory tree, parsing each as an entity file.
/// Supports both flat (`people/*.md`) and nested (`people/<country>/*.md`)
/// layouts. Uses rayon to parse files in parallel.
fn load_directory(
dir: &Path,
label: Label,
entries: &mut Vec<RegistryEntry>,
errors: &mut Vec<ParseError>,
) {
let mut paths = Vec::new();
collect_md_files(dir, &mut paths, 0);
// Sort for deterministic ordering
paths.sort();
// Parse all files in parallel, collect results
let results: Vec<ParseResult> = paths
.par_iter()
.map(|path| parse_entity_file(path, label))
.collect();
// Merge results sequentially to preserve deterministic order
for result in results {
if let Some(entry) = result.entry {
entries.push(entry);
}
errors.extend(result.errors);
}
}
/// Recursively collect `.md` files from a directory tree.
/// Max depth 2 supports `people/<country>/file.md` layout.
fn collect_md_files(dir: &Path, paths: &mut Vec<PathBuf>, depth: usize) {
const MAX_DEPTH: usize = 2;
if depth > MAX_DEPTH {
return;
}
let Ok(read_dir) = std::fs::read_dir(dir) else {
return;
};
let mut dir_entries: Vec<_> = read_dir.filter_map(Result::ok).collect();
dir_entries.sort_by_key(std::fs::DirEntry::file_name);
for entry in dir_entries {
let path = entry.path();
if path.is_dir() {
collect_md_files(&path, paths, depth + 1);
} else if path.extension().and_then(|e| e.to_str()) == Some("md") {
paths.push(path);
}
}
}
/// Result of parsing a single entity file.
struct ParseResult {
entry: Option<RegistryEntry>,
errors: Vec<ParseError>,
}
/// Parse a single entity file, returning the entry and any errors/warnings.
fn parse_entity_file(path: &Path, label: Label) -> ParseResult {
let content = match std::fs::read_to_string(path) {
Ok(c) => c,
Err(e) => {
return ParseResult {
entry: None,
errors: vec![ParseError {
line: 0,
message: format!("{}: error reading file: {e}", path.display()),
}],
};
}
};
let parsed = match crate::parser::parse_entity_file(&content) {
Ok(p) => p,
Err(parse_errors) => {
return ParseResult {
entry: None,
errors: parse_errors
.into_iter()
.map(|err| ParseError {
line: err.line,
message: format!("{}: {}", path.display(), err.message),
})
.collect(),
};
}
};
let mut field_errors = Vec::new();
let mut entity = crate::entity::parse_entity_file_body(
&parsed.name,
&parsed.body,
label,
parsed.id,
parsed.title_line,
&mut field_errors,
);
entity.tags.clone_from(&parsed.tags);
let mut errors: Vec<ParseError> = field_errors
.into_iter()
.map(|err| ParseError {
line: err.line,
message: format!("{}: {}", path.display(), err.message),
})
.collect();
// Validate filename matches content
validate_filename(path, &entity, &mut errors);
ParseResult {
entry: Some(RegistryEntry {
entity,
path: path.to_path_buf(),
tags: parsed.tags,
}),
errors,
}
}
/// Build name → index map, detecting duplicate names.
fn build_name_index(
entries: &[RegistryEntry],
errors: &mut Vec<ParseError>,
) -> HashMap<String, usize> {
let mut index = HashMap::new();
for (i, entry) in entries.iter().enumerate() {
let name = &entry.entity.name;
if let Some(&existing_idx) = index.get(name.as_str()) {
let existing: &RegistryEntry = &entries[existing_idx];
errors.push(ParseError {
line: entry.entity.line,
message: format!(
"duplicate entity name {name:?} in {} (first defined in {})",
entry.path.display(),
existing.path.display(),
),
});
} else {
index.insert(name.clone(), i);
}
}
index
}
/// Warn if entity filename doesn't match content.
/// Expected: `<name>--<qualifier>.md` in kebab-case.
fn validate_filename(path: &Path, entity: &Entity, errors: &mut Vec<ParseError>) {
let Some(raw_stem) = path.file_stem().and_then(|s| s.to_str()) else {
return;
};
// macOS APFS/HFS+ returns NFD filenames; normalize to NFC for comparison.
let stem: String = raw_stem.nfc().collect();
if stem.len() > MAX_FILENAME_LEN {
errors.push(ParseError {
line: 0,
message: format!(
"warning: {}: filename stem exceeds {MAX_FILENAME_LEN} chars",
path.display()
),
});
}
let expected_name = to_kebab_case(&entity.name);
let qualifier = entity
.fields
.iter()
.find(|(k, _)| k == "qualifier")
.and_then(|(_, v)| match v {
crate::entity::FieldValue::Single(s) => Some(s.as_str()),
crate::entity::FieldValue::List(_) => None,
});
let expected_stem = match qualifier {
Some(q) => format!("{expected_name}--{}", to_kebab_case(q)),
None => expected_name,
};
if stem != expected_stem {
errors.push(ParseError {
line: 0,
message: format!(
"warning: {}: filename {stem:?} doesn't match expected {expected_stem:?}",
path.display()
),
});
}
}
/// Convert a display name to kebab-case for filename comparison.
/// Strips diacritics by NFD-decomposing and removing combining marks,
/// producing ASCII-safe slugs (e.g. "Prlić" → "prlic", "José García" → "jose-garcia").
fn to_kebab_case(s: &str) -> String {
use unicode_normalization::UnicodeNormalization;
let mut transliterated = String::with_capacity(s.len());
for c in s.nfd().filter(|c| !is_combining_mark(*c)) {
match transliterate_latin(c) {
Some(replacement) => transliterated.push_str(replacement),
None => transliterated.push(c),
}
}
transliterated
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() {
c.to_ascii_lowercase()
} else if c.is_alphanumeric() {
// Non-ASCII alphanumeric after stripping diacritics (e.g. CJK, Arabic)
// — keep as-is in lowercase
c.to_lowercase().next().unwrap_or(c)
} else {
'-'
}
})
.collect::<String>()
.split('-')
.filter(|p| !p.is_empty())
.collect::<Vec<_>>()
.join("-")
}
/// Check if a character is a Unicode combining mark (category M).
fn is_combining_mark(c: char) -> bool {
// Combining marks: Mn (nonspacing), Mc (spacing combining), Me (enclosing)
// After NFD decomposition, diacritics become separate combining characters.
matches!(c as u32,
0x0300..=0x036F // Combining Diacritical Marks
| 0x1AB0..=0x1AFF // Combining Diacritical Marks Extended
| 0x1DC0..=0x1DFF // Combining Diacritical Marks Supplement
| 0x20D0..=0x20FF // Combining Diacritical Marks for Symbols
| 0xFE20..=0xFE2F // Combining Half Marks
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::entity::{Entity, FieldValue, Label};
fn make_entry(name: &str, label: Label, path: &str) -> RegistryEntry {
RegistryEntry {
entity: Entity {
name: name.to_string(),
label,
fields: Vec::new(),
id: None,
line: 1,
tags: Vec::new(),
slug: None,
},
path: PathBuf::from(path),
tags: Vec::new(),
}
}
#[test]
fn registry_from_entries_lookup() {
let entries = vec![
make_entry("Alice", Label::Person, "people/alice.md"),
make_entry("Corp Inc", Label::Organization, "organizations/corp-inc.md"),
];
let registry = EntityRegistry::from_entries(entries).unwrap();
assert_eq!(registry.len(), 2);
assert!(registry.get_by_name("Alice").is_some());
assert!(registry.get_by_name("Corp Inc").is_some());
assert!(registry.get_by_name("Bob").is_none());
}
#[test]
fn registry_detects_duplicate_names() {
let entries = vec![
make_entry("Alice", Label::Person, "people/alice-a.md"),
make_entry("Alice", Label::Person, "people/alice-b.md"),
];
let errors = EntityRegistry::from_entries(entries).unwrap_err();
assert!(errors.iter().any(|e| e.message.contains("duplicate")));
}
#[test]
fn registry_names_list() {
let entries = vec![
make_entry("Alice", Label::Person, "people/alice.md"),
make_entry("Bob", Label::Person, "people/bob.md"),
];
let registry = EntityRegistry::from_entries(entries).unwrap();
let names = registry.names();
assert!(names.contains(&"Alice"));
assert!(names.contains(&"Bob"));
}
#[test]
fn to_kebab_case_conversion() {
assert_eq!(to_kebab_case("Mark Bonnick"), "mark-bonnick");
assert_eq!(to_kebab_case("Arsenal FC"), "arsenal-fc");
assert_eq!(
to_kebab_case("English Football Club"),
"english-football-club"
);
assert_eq!(to_kebab_case("Bob"), "bob");
}
#[test]
fn to_kebab_case_strips_diacritics() {
assert_eq!(to_kebab_case("Prlić"), "prlic");
assert_eq!(to_kebab_case("José García"), "jose-garcia");
assert_eq!(
to_kebab_case("Médecins Sans Frontières"),
"medecins-sans-frontieres"
);
assert_eq!(to_kebab_case("Túpac Amaru"), "tupac-amaru");
assert_eq!(
to_kebab_case("Mészáros és Mészáros"),
"meszaros-es-meszaros"
);
assert_eq!(to_kebab_case("Sûreté du Québec"), "surete-du-quebec");
// Non-decomposable Latin characters
assert_eq!(to_kebab_case("Wałęsa"), "walesa");
assert_eq!(to_kebab_case("Đinđić"), "dindic");
assert_eq!(to_kebab_case("İbrahim Fırtına"), "ibrahim-firtina");
// German umlauts
assert_eq!(to_kebab_case("Müller Über Straße"), "muller-uber-strasse");
assert_eq!(to_kebab_case("Köln Düsseldorf"), "koln-dusseldorf");
}
#[test]
fn validate_filename_matching() {
let entity = Entity {
name: "Mark Bonnick".to_string(),
label: Label::Person,
fields: vec![(
"qualifier".to_string(),
FieldValue::Single("Arsenal Kit Manager".to_string()),
)],
id: None,
line: 1,
tags: Vec::new(),
slug: None,
};
let mut errors = Vec::new();
// Correct filename
validate_filename(
Path::new("people/mark-bonnick--arsenal-kit-manager.md"),
&entity,
&mut errors,
);
assert!(errors.is_empty(), "errors: {errors:?}");
// Wrong filename
validate_filename(Path::new("people/wrong-name.md"), &entity, &mut errors);
assert!(errors.iter().any(|e| e.message.contains("warning:")));
}
#[test]
fn validate_filename_no_qualifier() {
let entity = Entity {
name: "Bob".to_string(),
label: Label::Person,
fields: Vec::new(),
id: None,
line: 1,
tags: Vec::new(),
slug: None,
};
let mut errors = Vec::new();
validate_filename(Path::new("people/bob.md"), &entity, &mut errors);
assert!(errors.is_empty(), "errors: {errors:?}");
}
#[test]
fn empty_registry() {
let registry = EntityRegistry::from_entries(Vec::new()).unwrap();
assert!(registry.is_empty());
assert_eq!(registry.len(), 0);
assert!(registry.get_by_name("anything").is_none());
}
}