1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use proc_macro2::Span;
use syn::Ident;

pub fn is_local_metric(metric_type: Ident) -> bool {
    metric_type.to_string().starts_with("Local")
}

pub fn to_non_local_metric_type(metric_type: Ident) -> Ident {
    let metric_type_str = metric_type.to_string();
    if metric_type_str.starts_with("Local") {
        Ident::new(&metric_type_str[5..], Span::call_site())
    } else {
        metric_type
    }
}

pub fn get_metric_vec_type(metric_type: Ident) -> Ident {
    Ident::new(&format!("{}Vec", metric_type), Span::call_site())
}

pub fn get_label_struct_name(struct_name: Ident, label_index: usize) -> Ident {
    let mut struct_name = struct_name.to_string();
    if label_index > 0 {
        struct_name.push_str(&(label_index + 1).to_string());
    }
    Ident::new(&struct_name, Span::call_site())
}

pub fn get_member_type(
    struct_name: Ident,
    label_index: usize,
    metric_type: Ident,
    is_last_label: bool,
) -> Ident {
    if is_last_label {
        metric_type
    } else {
        get_label_struct_name(struct_name, label_index + 1)
    }
}

pub fn get_inner_member_type(
    struct_name: Ident,
    label_index: usize,
    metric_type: Ident,
    is_last_label: bool,
) -> Ident {
    if is_last_label {
        metric_type
    } else {
        Ident::new(
            &format!(
                "{}Inner",
                get_label_struct_name(struct_name, label_index + 1)
            ),
            Span::call_site(),
        )
    }
}

pub fn get_delegator_member_type(
    struct_name: Ident,
    label_index: usize,
    is_last_label: bool,
) -> Ident {
    if is_last_label {
        Ident::new("usize", Span::call_site())
    } else {
        Ident::new(
            &format!(
                "{}{}",
                get_label_struct_name(struct_name, label_index + 1),
                "Delegator"
            ),
            Span::call_site(),
        )
    }
}