dry4rust/
normalization_context.rs1use crate::node::PlaceholderKind;
7use std::collections::HashMap;
8
9pub struct NormalizationContext {
11 mappings: HashMap<(String, PlaceholderKind), usize>,
13 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 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}