1 | use super::*; |
2 | |
3 | pub struct MethodNames(HashMap<String, u32>); |
4 | |
5 | impl MethodNames { |
6 | pub fn new() -> Self { |
7 | Self(HashMap::new()) |
8 | } |
9 | |
10 | pub fn add(&mut self, method: MethodDef) -> TokenStream { |
11 | let name: String = method_def_special_name(row:method); |
12 | let overload: &mut u32 = self.0.entry(name.to_string()).or_insert(default:0); |
13 | *overload += 1; |
14 | if *overload > 1 { |
15 | format!(" {name}{overload}" ).into() |
16 | } else { |
17 | to_ident(&name) |
18 | } |
19 | } |
20 | } |
21 | |
22 | fn method_def_special_name(row: MethodDef) -> String { |
23 | let name = row.name(); |
24 | if row.flags().contains(MethodAttributes::SpecialName) { |
25 | if name.starts_with("get" ) { |
26 | name[4..].to_string() |
27 | } else if name.starts_with("put" ) { |
28 | format!("Set {}" , &name[4..]) |
29 | } else if name.starts_with("add" ) { |
30 | name[4..].to_string() |
31 | } else if name.starts_with("remove" ) { |
32 | format!("Remove {}" , &name[7..]) |
33 | } else { |
34 | name.to_string() |
35 | } |
36 | } else { |
37 | if let Some(attribute) = row.find_attribute("OverloadAttribute" ) { |
38 | for (_, arg) in attribute.args() { |
39 | if let Value::Str(name) = arg { |
40 | return name.to_string(); |
41 | } |
42 | } |
43 | } |
44 | name.to_string() |
45 | } |
46 | } |
47 | |