- add one Node/one Edge model over canonical JSON-tuple keys; root units map to their document node and Exec unit/function nodes are the same node - build contains tree, statement and resource nodes, asset nodes and inert external nodes; keep every repeated link occurrence as its own edge - resolve ordinary links relative to the source document with exactly one percent decode, fragment rules, numbered anchors, exact canonical-name fallback and explicit missing/ambiguous/not_loaded/out_of_scope/invalid states - never re-bind a resolved upstream Marqdo target; preserve not_loaded and resolve it in root scope only through exact anchor/name rules; surface interface conflicts when upstream and knowledge mappings disagree - map imports and calls to real nodes or explicit external/unresolved edges; builtin/plugin targets stay external symbols, never fabricated functions - add deterministic logical digest and tests for contains, externals, repeated occurrences, ambiguity, anchors and missing-path distinctions
124 lines
3.7 KiB
Rust
124 lines
3.7 KiB
Rust
//! Canonical identity for nodes, aliases and edges.
|
|
//!
|
|
//! Every key is a structured tuple encoded by [`NodeKey`]. No key is formed by
|
|
//! concatenating raw `#`, `::` or `/`. Unit keys carry an explicit occurrence
|
|
//! index before the path segments, so a path segment that looks like an index
|
|
//! can never collide with the index itself.
|
|
|
|
use crate::model::NodeKey;
|
|
|
|
pub fn document_key(rel_path: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["document", rel_path])
|
|
}
|
|
|
|
/// Key for a non-root unit. The root unit is the document node.
|
|
pub fn unit_key(rel_path: &str, occurrence: u32, path: &[String]) -> NodeKey {
|
|
let mut parts: Vec<&str> = Vec::with_capacity(path.len() + 3);
|
|
parts.push("unit");
|
|
parts.push(rel_path);
|
|
let occurrence = occurrence.to_string();
|
|
parts.push(&occurrence);
|
|
for segment in path {
|
|
parts.push(segment.as_str());
|
|
}
|
|
NodeKey::from_parts(&parts)
|
|
}
|
|
|
|
pub fn statement_key(
|
|
rel_path: &str,
|
|
occurrence: u32,
|
|
unit_path: &[String],
|
|
ordinal: usize,
|
|
span_start: u64,
|
|
) -> NodeKey {
|
|
let occurrence = occurrence.to_string();
|
|
let ordinal = ordinal.to_string();
|
|
let span_start = span_start.to_string();
|
|
let mut parts: Vec<&str> = Vec::with_capacity(unit_path.len() + 6);
|
|
parts.push("statement");
|
|
parts.push(rel_path);
|
|
parts.push(&occurrence);
|
|
for segment in unit_path {
|
|
parts.push(segment.as_str());
|
|
}
|
|
parts.push(&ordinal);
|
|
parts.push(&span_start);
|
|
NodeKey::from_parts(&parts)
|
|
}
|
|
|
|
pub fn resource_key(rel_path: &str, resource_id: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["resource", rel_path, resource_id])
|
|
}
|
|
|
|
pub fn asset_key(rel_path: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["asset", rel_path])
|
|
}
|
|
|
|
pub fn external_url_key(url: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "url", url])
|
|
}
|
|
|
|
pub fn external_module_key(module_id: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "module", module_id])
|
|
}
|
|
|
|
pub fn external_unit_key(unit_id: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "unit", unit_id])
|
|
}
|
|
|
|
pub fn external_function_key(function_id: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "function", function_id])
|
|
}
|
|
|
|
pub fn external_symbol_key(kind: &str, name: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "symbol", kind, name])
|
|
}
|
|
|
|
pub fn external_file_key(rel_path: &str) -> NodeKey {
|
|
NodeKey::from_parts(&["external", "file", rel_path])
|
|
}
|
|
|
|
/// Edge deduplication key: one row per occurrence, including its source span.
|
|
pub fn edge_key(
|
|
source: &str,
|
|
kind: &str,
|
|
target: Option<&str>,
|
|
raw_target: &str,
|
|
span: Option<(u64, u64)>,
|
|
) -> String {
|
|
let (start, end) = span.unwrap_or((0, 0));
|
|
format!(
|
|
"{source}\u{1f}{kind}\u{1f}{}\u{1f}{raw_target}\u{1f}{start}\u{1f}{end}",
|
|
target.unwrap_or("")
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn unit_keys_with_index_like_segments_do_not_collide() {
|
|
let a = unit_key("a.md", 0, &["occ:1".to_string()]);
|
|
let b = unit_key("a.md", 1, &[]);
|
|
assert_ne!(a, b);
|
|
let c = unit_key("a.md", 0, &["x".to_string()]);
|
|
let d = unit_key("a.md", 1, &["x".to_string()]);
|
|
assert_ne!(c, d);
|
|
}
|
|
|
|
#[test]
|
|
fn keys_round_trip_as_opaque_ids() {
|
|
let key = unit_key("dir/doc.md", 2, &["力学".to_string(), "计算力".to_string()]);
|
|
assert_eq!(NodeKey::from_encoded(key.as_str()).as_ref(), Some(&key));
|
|
assert!(!key.as_str().contains('#'));
|
|
}
|
|
|
|
#[test]
|
|
fn edge_keys_include_every_occurrence() {
|
|
let first = edge_key("s", "references", Some("t"), "u", Some((1, 2)));
|
|
let second = edge_key("s", "references", Some("t"), "u", Some((3, 4)));
|
|
assert_ne!(first, second);
|
|
}
|
|
}
|