Skip to main content

normalize_and_hash

Function normalize_and_hash 

Source
pub fn normalize_and_hash(kind: &str, children: &[NormalizeNode]) -> String
Expand description

Computes a canonical blake3 fingerprint for a pattern AST subtree.

§Algorithm

Depth-first canonical walk. For a node with kind = K and ordered children [c1, c2, …], the input to blake3::keyed_hash(FINGERPRINT_KEY, _) is the byte concatenation of:

  • K (UTF-8 bytes)
  • the separator byte 0x00
  • for each child: the byte 0x01 followed by the 32 digest bytes of the recursively-computed child fingerprint

The 0x00 / 0x01 framing prevents a leaf node K from colliding with an internal node K whose only child starts with K.

Empty children produces the same digest as crate::fingerprint::fingerprint_node_kind(kind): blake3::keyed_hash(FINGERPRINT_KEY, kind.as_bytes()) (the 0x00 suffix on an empty children list makes the input identical to the raw kind bytes followed by 0x00, but fingerprint_node_kind feeds only kind.as_bytes() — so they must be byte-identical).

Wait — to maintain equivalence with fingerprint_node_kind(kind) for empty children, the algorithm for empty children uses kind.as_bytes() directly (no separator, no framing), matching fingerprint_node_kind.

Returns a 64-character lowercase hex string.

§Examples

use sdivi_patterns::normalize::normalize_and_hash;
use sdivi_patterns::fingerprint::fingerprint_node_kind;

// Empty children must match fingerprint_node_kind.
let fp1 = fingerprint_node_kind("try_expression").to_hex();
let fp2 = normalize_and_hash("try_expression", &[]);
assert_eq!(fp1, fp2);