1use serde_derive::Serialize;
2
3use crate::ast;
4
5#[derive(Serialize, Clone, Debug)]
6pub enum Symbol<'a> {
7 Package(&'a ast::Package),
8 Import(&'a ast::Import),
9 Interface(&'a ast::Interface, &'a ast::Package),
10 Parcelable(&'a ast::Parcelable, &'a ast::Package),
11 Enum(&'a ast::Enum, &'a ast::Package),
12 Method(&'a ast::Method, &'a ast::Interface),
13 Arg(&'a ast::Arg, &'a ast::Method),
14 Const(&'a ast::Const, ConstOwner<'a>),
15 Field(&'a ast::Field, &'a ast::Parcelable),
16 EnumElement(&'a ast::EnumElement, &'a ast::Enum),
17 Type(&'a ast::Type),
18}
19
20#[derive(Serialize, Clone, Debug)]
21pub enum ConstOwner<'a> {
22 Interface(&'a ast::Interface),
23 Parcelable(&'a ast::Parcelable),
24}
25
26impl<'a> ConstOwner<'a> {
27 pub fn get_name(&self) -> &str {
28 match self {
29 ConstOwner::Interface(i) => &i.name,
30 ConstOwner::Parcelable(p) => &p.name,
31 }
32 }
33}
34
35impl<'a> Symbol<'a> {
36 pub fn get_name(&self) -> Option<String> {
37 match self {
38 Symbol::Package(p) => Some(p.name.clone()),
39 Symbol::Import(i) => Some(i.get_qualified_name()),
40 Symbol::Interface(i, _) => Some(i.name.clone()),
41 Symbol::Parcelable(p, _) => Some(p.name.clone()),
42 Symbol::Enum(e, _) => Some(e.name.clone()),
43 Symbol::Method(m, _) => Some(m.name.clone()),
44 Symbol::Arg(a, _) => a.name.clone(),
45 Symbol::Const(c, _) => Some(c.name.clone()),
46 Symbol::Field(m, _) => Some(m.name.clone()),
47 Symbol::EnumElement(e, _) => Some(e.name.clone()),
48 Symbol::Type(t) => Some(t.name.clone()),
49 }
50 }
51
52 pub fn get_qualified_name(&self) -> Option<String> {
53 match self {
54 Symbol::Package(p) => Some(p.name.clone()),
55 Symbol::Import(i) => Some(i.get_qualified_name()),
56 Symbol::Interface(i, pkg) => Some(format!("{}.{}", pkg.name, i.name)),
57 Symbol::Parcelable(p, pkg) => Some(format!("{}.{}", pkg.name, p.name)),
58 Symbol::Enum(e, pkg) => Some(format!("{}{}", pkg.name, e.name)),
59 Symbol::Method(m, i) => Some(format!("{}::{}", i.name, m.name)),
60 Symbol::Arg(a, _) => a.name.clone(),
61 Symbol::Const(c, o) => Some(format!("{}::{}", o.get_name(), c.name)),
62 Symbol::Field(m, p) => Some(format!("{}::{}", p.name, m.name)),
63 Symbol::EnumElement(el, e) => Some(format!("{}::{}", e.name, el.name)),
64 Symbol::Type(ast::Type {
65 kind: ast::TypeKind::ResolvedItem(qualified_name, _),
66 ..
67 }) => Some(qualified_name.clone()),
68 Symbol::Type(_) => None,
69 }
70 }
71
72 pub fn get_range(&self) -> &ast::Range {
73 match self {
74 Symbol::Package(p) => &p.symbol_range,
75 Symbol::Import(i) => &i.symbol_range,
76 Symbol::Interface(i, _) => &i.symbol_range,
77 Symbol::Parcelable(p, _) => &p.symbol_range,
78 Symbol::Enum(e, _) => &e.symbol_range,
79 Symbol::Method(m, _) => &m.symbol_range,
80 Symbol::Arg(a, _) => &a.symbol_range,
81 Symbol::Const(c, _) => &c.symbol_range,
82 Symbol::Field(m, _) => &m.symbol_range,
83 Symbol::EnumElement(e, _) => &e.symbol_range,
84 Symbol::Type(t) => &t.symbol_range,
85 }
86 }
87
88 pub fn get_full_range(&self) -> &ast::Range {
89 match self {
90 Symbol::Package(p) => &p.full_range,
91 Symbol::Import(i) => &i.full_range,
92 Symbol::Interface(i, _) => &i.full_range,
93 Symbol::Parcelable(p, _) => &p.full_range,
94 Symbol::Enum(e, _) => &e.full_range,
95 Symbol::Method(m, _) => &m.full_range,
96 Symbol::Arg(a, _) => &a.full_range,
97 Symbol::Const(c, _) => &c.full_range,
98 Symbol::Field(m, _) => &m.full_range,
99 Symbol::EnumElement(e, _) => &e.full_range,
100 Symbol::Type(t) => &t.full_range,
101 }
102 }
103
104 pub fn get_details(&self) -> Option<String> {
105 fn get_type_str(t: &ast::Type) -> String {
106 if t.generic_types.is_empty() {
107 t.name.clone()
108 } else {
109 format!(
110 "{}<{}>",
111 t.name,
112 t.generic_types
113 .iter()
114 .map(get_type_str)
115 .collect::<Vec<_>>()
116 .join(", ")
117 )
118 }
119 }
120
121 fn get_arg_str(a: &ast::Arg) -> String {
122 let direction_str = match a.direction {
123 ast::Direction::In(_) => "in ",
124 ast::Direction::Out(_) => "out ",
125 ast::Direction::InOut(_) => "inout ",
126 ast::Direction::Unspecified => "",
127 };
128
129 format!("{}{}", direction_str, get_type_str(&a.arg_type))
130 }
131
132 Some(match self {
133 Symbol::Package(..) => String::from("package"),
134 Symbol::Import(..) => String::from("import"),
135 Symbol::Interface(..) => String::from("interface"),
136 Symbol::Parcelable(..) => String::from("parcelable"),
137 Symbol::Enum(..) => String::from("enum"),
138 Symbol::Method(m, _) => {
139 format!(
140 "{}({})",
141 get_type_str(&m.return_type),
142 m.args
143 .iter()
144 .map(get_arg_str)
145 .collect::<Vec<_>>()
146 .join(", ")
147 )
148 }
149 Symbol::Arg(a, _) => get_arg_str(a),
150 Symbol::Const(c, _) => format!("const {}", get_type_str(&c.const_type)),
151 Symbol::Field(m, _) => get_type_str(&m.field_type),
152 Symbol::EnumElement(..) => return None,
153 Symbol::Type(t) => get_type_str(t),
154 })
155 }
156
157 pub fn get_signature(&self) -> String {
158 fn get_type_str(t: &ast::Type) -> String {
159 if t.generic_types.is_empty() {
160 t.name.clone()
161 } else {
162 format!(
163 "{}<{}>",
164 t.name,
165 t.generic_types
166 .iter()
167 .map(get_type_str)
168 .collect::<Vec<_>>()
169 .join(", ")
170 )
171 }
172 }
173
174 fn get_arg_str(a: &ast::Arg) -> String {
175 let direction_str = match a.direction {
176 ast::Direction::In(_) => "in ",
177 ast::Direction::Out(_) => "out ",
178 ast::Direction::InOut(_) => "inout ",
179 ast::Direction::Unspecified => "",
180 };
181
182 let suffix = a.name.as_ref().map(|s| format!(" {s}")).unwrap_or_default();
183
184 format!("{}{}{}", direction_str, get_type_str(&a.arg_type), suffix)
185 }
186
187 match self {
188 Symbol::Package(p) => format!("package {}", p.name),
189 Symbol::Import(i) => format!("import {}", i.get_qualified_name()),
190 Symbol::Parcelable(p, _) => format!("parcelable {}", p.name),
191 Symbol::Interface(i, _) => format!("interface {}", i.name),
192 Symbol::Enum(e, _) => format!("enum {}", e.name),
193 Symbol::Method(m, _) => {
194 format!(
195 "{} {}({})",
196 get_type_str(&m.return_type),
197 m.name,
198 m.args
199 .iter()
200 .map(get_arg_str)
201 .collect::<Vec<_>>()
202 .join(", ")
203 )
204 }
205 Symbol::Arg(a, _) => get_arg_str(a),
206 Symbol::Const(c, _) => format!("const {} {}", get_type_str(&c.const_type), c.name),
207 Symbol::Field(m, _) => format!("{} {}", get_type_str(&m.field_type), m.name),
208 Symbol::EnumElement(el, _) => el.name.clone(),
209 Symbol::Type(t) => get_type_str(t),
210 }
211 }
212}