From 7064c5f22c8e331853198659f94646d7b9a816f3 Mon Sep 17 00:00:00 2001 From: cflmy Date: Wed, 25 Nov 2026 11:37:39 +0800 Subject: [PATCH] analysis: keep function and module identities injective Escape the module/function delimiter when a function name contains '::', so a path segment containing the delimiter cannot make two FunctionId renderings collide. Drop the temporary path restriction that rejected a legal '::' in a module path; the structured path and name fields remain authoritative. Add an identity regression test and revalidate the schema. --- crates/marqdo-ir/src/lib.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/marqdo-ir/src/lib.rs b/crates/marqdo-ir/src/lib.rs index cd58a07..b5f19e9 100644 --- a/crates/marqdo-ir/src/lib.rs +++ b/crates/marqdo-ir/src/lib.rs @@ -117,10 +117,19 @@ pub struct FunctionId { impl fmt::Display for FunctionId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}::{}", self.module, self.name) + write!(f, "{}::{}", self.module, escape_function_name(&self.name)) } } +/// Escape the module/function delimiter inside a function name. +/// +/// The canonical form is `::`; a name that itself contains `::` +/// would make the delimiter ambiguous. Normal names are unchanged. `%` is +/// escaped first so a literal `%3A%3A` cannot collide with an escaped `::`. +pub fn escape_function_name(name: &str) -> String { + name.replace('%', "%25").replace("::", "%3A%3A") +} + /// Identity of an addressable heading range. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub struct UnitId { @@ -601,6 +610,26 @@ mod tests { assert_eq!(module.id.to_string(), "root:default::index.mq.md"); } + #[test] + fn function_ids_escape_the_module_delimiter_inside_names() { + let module = ModuleId::from("index.mq.md"); + let plain = FunctionId { + module: module.clone(), + name: "数学.计算".to_string(), + }; + assert_eq!(plain.to_string(), "root:default::index.mq.md::数学.计算"); + let dotted = FunctionId { + module: module.clone(), + name: "a::b".to_string(), + }; + assert_eq!(dotted.to_string(), "root:default::index.mq.md::a%3A%3Ab"); + let literal = FunctionId { + module, + name: "a%3A%3Ab".to_string(), + }; + assert_ne!(literal.to_string(), dotted.to_string()); + } + #[test] fn unit_ids_are_injective_across_separator_characters() { let module = ModuleId::from("index.mq.md");