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
200
201
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, &'a ast::Package),
    Parcelable(&'a ast::Parcelable, &'a ast::Package),
    Enum(&'a ast::Enum, &'a ast::Package),
    Method(&'a ast::Method, &'a ast::Interface),
    Arg(&'a ast::Arg, &'a ast::Method),
    Const(&'a ast::Const, &'a ast::Interface),
    Field(&'a ast::Field, &'a ast::Parcelable),
    EnumElement(&'a ast::EnumElement, &'a ast::Enum),
    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::Field(m, _) => Some(m.name.clone()),
            Symbol::EnumElement(e, _) => Some(e.name.clone()),
            Symbol::Type(t) => Some(t.name.clone()),
        }
    }

    pub fn get_qualified_name(&self) -> Option<String> {
        match self {
            Symbol::Package(p) => Some(p.name.clone()),
            Symbol::Import(i) => Some(i.get_qualified_name()),
            Symbol::Interface(i, pkg) => Some(format!("{}.{}", pkg.name, i.name)),
            Symbol::Parcelable(p, pkg) => Some(format!("{}.{}", pkg.name, p.name)),
            Symbol::Enum(e, pkg) => Some(format!("{}{}", pkg.name, e.name)),
            Symbol::Method(m, i) => Some(format!("{}::{}", i.name, m.name)),
            Symbol::Arg(a, _) => a.name.clone(),
            Symbol::Const(c, i) => Some(format!("{}::{}", i.name, c.name)),
            Symbol::Field(m, p) => Some(format!("{}::{}", p.name, m.name)),
            Symbol::EnumElement(el, e) => Some(format!("{}::{}", e.name, el.name)),
            Symbol::Type(ast::Type {
                kind: ast::TypeKind::Resolved(qualified_name, _),
                ..
            }) => Some(qualified_name.clone()),
            Symbol::Type(_) => None,
        }
    }

    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::Field(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::Field(m, _) => &m.full_range,
            Symbol::EnumElement(e, _) => &e.full_range,
            Symbol::Type(t) => &t.full_range,
        }
    }

    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::Field(m, _) => get_type_str(&m.field_type),
            Symbol::EnumElement(..) => return None,
            Symbol::Type(t) => get_type_str(t),
        })
    }

    pub fn get_signature(&self) -> 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)
        }

        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::Field(m, _) => format!("{} {}", get_type_str(&m.field_type), m.name),
            Symbol::EnumElement(el, _) => el.name.clone(),
            Symbol::Type(t) => get_type_str(t),
        }
    }
}