structuralSignature function

String structuralSignature(
  1. SemanticNode node
)

Structural fingerprint of a subtree: what a node IS, not what it says. Two list rows with different text but the same shape share a signature; a row whose toggle state differs, or that lacks a button, does not.

Implementation

String structuralSignature(SemanticNode node) {
  final b = StringBuffer(node.role.name);
  final affs = node.affordances.map((a) => a.name).toList()..sort();
  if (affs.isNotEmpty) b.write('[${affs.join(',')}]');
  switch (node) {
    case SemanticContainer c:
      b.write(':${c.hint ?? ''}');
    case SemanticButton bt:
      b
        ..write(bt.label != null ? ':L' : ':')
        ..write(bt.isToggle ? 'T' : '')
        ..write(bt.toggleState ?? '')
        ..write(bt.selected == true ? 'S' : '');
    case SemanticInput i:
      b
        ..write(':H${i.hint ?? ''}')
        ..write(i.hasValue ? 'V' : '')
        ..write(i.error != null ? 'E${i.error}' : '');
    case SemanticUnknown u:
      b.write(':${u.label}');
    case SemanticMapItem m:
      b.write(':${m.label}');
    default:
      break;
  }
  if (node.children.isNotEmpty) {
    b.write('(');
    for (final c in node.children) {
      b
        ..write(structuralSignature(c))
        ..write(',');
    }
    b.write(')');
  }
  return b.toString();
}