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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use serde_derive::Serialize;

use crate::ast;

#[derive(Serialize, Clone, Debug)]
pub enum Symbol<'a> {
    Package(&'a ast::Package),
    Import(&'a ast::Import),
    Interface(&'a ast::Interface),
    Parcelable(&'a ast::Parcelable),
    Enum(&'a ast::Enum),
    Method(&'a ast::Method),
    Arg(&'a ast::Arg),
    Const(&'a ast::Const),
    Member(&'a ast::Member),
    EnumElement(&'a ast::EnumElement),
    Type(&'a ast::Type),
}

impl<'a> Symbol<'a> {
    pub fn get_name(&self) -> Option<String> {
        match self {
            Symbol::Package(p) => Some(p.name.clone()),
            Symbol::Import(i) => Some(i.get_qualified_name()),
            Symbol::Interface(i) => Some(i.name.clone()),
            Symbol::Parcelable(p) => Some(p.name.clone()),
            Symbol::Enum(e) => Some(e.name.clone()),
            Symbol::Method(m) => Some(m.name.clone()),
            Symbol::Arg(a) => a.name.clone(),
            Symbol::Const(c) => Some(c.name.clone()),
            Symbol::Member(m) => Some(m.name.clone()),
            Symbol::EnumElement(e) => Some(e.name.clone()),
            Symbol::Type(t) => Some(t.name.clone()),
        }
    }

    pub fn get_range(&self) -> &ast::Range {
        match self {
            Symbol::Package(p) => &p.symbol_range,
            Symbol::Import(i) => &i.symbol_range,
            Symbol::Interface(i) => &i.symbol_range,
            Symbol::Parcelable(p) => &p.symbol_range,
            Symbol::Enum(e) => &e.symbol_range,
            Symbol::Method(m) => &m.symbol_range,
            Symbol::Arg(a) => &a.symbol_range,
            Symbol::Const(c) => &c.symbol_range,
            Symbol::Member(m) => &m.symbol_range,
            Symbol::EnumElement(e) => &e.symbol_range,
            Symbol::Type(t) => &t.symbol_range,
        }
    }

    pub fn get_full_range(&self) -> &ast::Range {
        match self {
            Symbol::Package(p) => &p.full_range,
            Symbol::Import(i) => &i.full_range,
            Symbol::Interface(i) => &i.full_range,
            Symbol::Parcelable(p) => &p.full_range,
            Symbol::Enum(e) => &e.full_range,
            Symbol::Method(m) => &m.full_range,
            Symbol::Arg(a) => &a.full_range,
            Symbol::Const(c) => &c.full_range,
            Symbol::Member(m) => &m.full_range,
            Symbol::EnumElement(e) => &e.full_range,
            Symbol::Type(t) => &t.symbol_range, // TODO: consider full range (might be different for generics)?
        }
    }

    pub fn get_details(&self) -> Option<String> {
        fn get_type_str(t: &ast::Type) -> String {
            if t.generic_types.is_empty() {
                t.name.clone()
            } else {
                format!(
                    "{}<{}>",
                    t.name,
                    t.generic_types
                        .iter()
                        .map(get_type_str)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
        }

        fn get_arg_str(a: &ast::Arg) -> String {
            let direction_str = match a.direction {
                ast::Direction::In(_) => "in ",
                ast::Direction::Out(_) => "out ",
                ast::Direction::InOut(_) => "inout ",
                ast::Direction::Unspecified => "",
            };

            format!("{}{}", direction_str, get_type_str(&a.arg_type))
        }

        Some(match self {
            Symbol::Package(_) => String::from("package"),
            Symbol::Import(_) => String::from("import"),
            Symbol::Interface(_) => String::from("interface"),
            Symbol::Parcelable(_) => String::from("parcelable"),
            Symbol::Enum(_) => String::from("enum"),
            Symbol::Method(m) => {
                format!(
                    "{}({})",
                    get_type_str(&m.return_type),
                    m.args
                        .iter()
                        .map(get_arg_str)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
            Symbol::Arg(a) => get_arg_str(a),
            Symbol::Const(c) => format!("const {}", get_type_str(&c.const_type)),
            Symbol::Member(m) => get_type_str(&m.member_type),
            Symbol::EnumElement(_) => return None,
            Symbol::Type(t) => {
                if t.generic_types.is_empty() {
                    return None;
                }
                t.generic_types
                    .iter()
                    .map(get_type_str)
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        })
    }

    pub fn get_signature(&self) -> Option<String> {
        fn get_type_str(t: &ast::Type) -> String {
            if t.generic_types.is_empty() {
                t.name.clone()
            } else {
                format!(
                    "{}<{}>",
                    t.name,
                    t.generic_types
                        .iter()
                        .map(get_type_str)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
        }

        fn get_arg_str(a: &ast::Arg) -> String {
            let direction_str = match a.direction {
                ast::Direction::In(_) => "in ",
                ast::Direction::Out(_) => "out ",
                ast::Direction::InOut(_) => "inout ",
                ast::Direction::Unspecified => "",
            };

            let suffix = a
                .name
                .as_ref()
                .map(|s| format!(" {}", s))
                .unwrap_or_default();

            format!("{}{}{}", direction_str, get_type_str(&a.arg_type), suffix)
        }

        Some(match self {
            Symbol::Package(p) => format!("package {}", p.name),
            Symbol::Import(i) => format!("import {}", i.get_qualified_name()),
            Symbol::Parcelable(p) => format!("parcelable {}", p.name),
            Symbol::Interface(i) => format!("interface {}", i.name),
            Symbol::Enum(e) => format!("enum {}", e.name),
            Symbol::Method(m) => {
                format!(
                    "{} {}({})",
                    get_type_str(&m.return_type),
                    m.name,
                    m.args
                        .iter()
                        .map(get_arg_str)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
            Symbol::Arg(a) => get_arg_str(a),
            Symbol::Const(c) => format!("const {} {}", get_type_str(&c.const_type), c.name),
            Symbol::Member(m) => format!("{} {}", get_type_str(&m.member_type), m.name),
            Symbol::EnumElement(el) => el.name.clone(),
            Symbol::Type(t) => {
                if t.generic_types.is_empty() {
                    return None;
                }
                t.generic_types
                    .iter()
                    .map(get_type_str)
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        })
    }
}