Skip to main content

dry4rust/
normalization_context.rs

1// Copyright (c) 2026 Matjaz Domen Pecan
2// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
3// Licensed under the MIT License
4// SPDX-License-Identifier: MIT
5
6use crate::node::PlaceholderKind;
7use std::collections::HashMap;
8
9/// Tracks identifier-to-placeholder mappings during normalization.
10pub struct NormalizationContext {
11    /// Maps (identifier_string, kind) -> placeholder index
12    mappings: HashMap<(String, PlaceholderKind), usize>,
13    /// Per-kind counters
14    counters: HashMap<PlaceholderKind, usize>,
15}
16
17impl NormalizationContext {
18    #[must_use]
19    pub fn new() -> Self {
20        Self {
21            mappings: HashMap::new(),
22            counters: HashMap::new(),
23        }
24    }
25
26    /// Get or assign a placeholder index for the given identifier and kind.
27    pub fn placeholder(&mut self, name: &str, kind: PlaceholderKind) -> usize {
28        let key = (name.to_string(), kind);
29        if let Some(&idx) = self.mappings.get(&key) {
30            return idx;
31        }
32        let counter = self.counters.entry(kind).or_insert(0);
33        let idx = *counter;
34        *counter += 1;
35        self.mappings.insert(key, idx);
36        idx
37    }
38}
39
40impl Default for NormalizationContext {
41    fn default() -> Self {
42        Self::new()
43    }
44}