1use std::{ops::Deref, sync::Arc};
2
3use faststr::FastStr;
4use pilota::Bytes;
5
6use super::ty::Ty;
7use crate::{
8 middle::ext::{FileExts, ItemExts, ModExts},
9 symbol::{DefId, EnumRepr, FileId, Ident, Symbol},
10 tags::TagId,
11};
12
13#[derive(Clone, Debug, PartialEq, Eq, Hash)]
14pub enum Literal {
15 Path(Path),
16 Bool(bool),
17 String(Arc<str>),
18 Int(i64),
19 Float(Arc<str>),
20 List(Vec<Literal>),
21 Map(Vec<(Literal, Literal)>),
22}
23
24#[derive(Clone, Debug, PartialEq, Eq, Hash)]
25pub struct Arg {
26 pub ty: Ty,
27 pub def_id: DefId,
28 pub name: Ident,
29 pub id: i32,
30 pub tags_id: TagId,
31 pub kind: FieldKind,
32}
33
34#[derive(Clone, Debug)]
35pub struct ExceptionVariant {
36 pub id: i32,
37 pub ty: Ty,
38}
39
40#[derive(Clone, Debug, PartialEq, Eq, Hash)]
41pub enum MethodSource {
42 Extend(DefId),
43 Own,
44}
45#[derive(Clone, Debug, PartialEq, Eq, Hash)]
46pub struct Method {
47 pub def_id: DefId,
48 pub name: Ident,
49 pub args: Vec<Arc<Arg>>,
50 pub ret: Ty,
51 pub oneway: bool,
52 pub exceptions: Option<Path>,
53 pub source: MethodSource,
54 pub leading_comments: FastStr,
55 pub trailing_comments: FastStr,
56 pub item_exts: ItemExts,
57}
58
59#[derive(Clone, Debug, PartialEq, Eq, Hash)]
60pub struct Service {
61 pub name: Ident,
62 pub methods: Vec<Arc<Method>>,
63 pub extend: Vec<Path>,
64 pub leading_comments: FastStr,
65 pub trailing_comments: FastStr,
66 pub item_exts: ItemExts,
67}
68
69#[derive(Clone, Debug, PartialEq, Eq, Hash)]
70pub struct Const {
71 pub name: Ident,
72 pub ty: Ty,
73 pub lit: Literal,
74 pub leading_comments: FastStr,
75 pub trailing_comments: FastStr,
76}
77
78#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
79pub enum FieldKind {
80 Required,
81 Optional,
82}
83
84#[derive(Clone, Debug, PartialEq, Eq, Hash)]
85pub struct Field {
86 pub did: DefId,
87 pub name: Ident,
88 pub id: i32,
89 pub ty: Ty,
90 pub kind: FieldKind,
91 pub tags_id: TagId,
92 pub default: Option<Literal>,
93 pub leading_comments: FastStr,
94 pub trailing_comments: FastStr,
95 pub item_exts: ItemExts,
96}
97
98impl Field {
99 pub fn is_optional(&self) -> bool {
100 matches!(self.kind, FieldKind::Optional)
101 }
102
103 pub fn local_var_name(&self) -> String {
104 format!("var_{}", self.id)
105 }
106}
107
108#[derive(Clone, Debug, PartialEq, Eq, Hash)]
109pub struct Message {
110 pub name: Ident,
111 pub fields: Vec<Arc<Field>>,
112 pub is_wrapper: bool,
113 pub item_exts: ItemExts,
114 pub leading_comments: FastStr,
115 pub trailing_comments: FastStr,
116}
117
118#[derive(Clone, Debug, PartialEq, Eq, Hash)]
119pub struct EnumVariant {
120 pub id: Option<i32>,
121 pub did: DefId,
122 pub name: Ident,
123 pub discr: Option<i64>,
124 pub fields: Vec<Ty>,
125 pub leading_comments: FastStr,
126 pub trailing_comments: FastStr,
127 pub item_exts: ItemExts,
128}
129
130#[derive(Clone, Debug, PartialEq, Eq, Hash)]
131pub struct Enum {
132 pub name: Ident,
133 pub variants: Vec<Arc<EnumVariant>>,
134 pub repr: Option<EnumRepr>,
135 pub leading_comments: FastStr,
136 pub trailing_comments: FastStr,
137 pub item_exts: ItemExts,
138}
139
140#[derive(Clone, Debug, PartialEq, Eq, Hash)]
141pub struct NewType {
142 pub name: Ident,
143 pub ty: Ty,
144 pub leading_comments: FastStr,
145 pub trailing_comments: FastStr,
146}
147
148#[derive(Clone, Debug, PartialEq, Eq, Hash)]
149pub struct Mod {
150 pub name: Ident,
151 pub items: Vec<DefId>,
152 pub extensions: ModExts,
153}
154
155#[derive(Clone, Debug, PartialEq, Eq, Hash)]
156pub enum Item {
157 Message(Message),
158 Enum(Enum),
159 Service(Service),
160 NewType(NewType),
161 Const(Const),
162 Mod(Mod),
163}
164
165impl Item {
166 pub fn symbol_name(&self) -> Symbol {
167 match self {
168 Item::Message(s) => (*s.name).clone(),
169 Item::Enum(e) => (*e.name).clone(),
170 Item::Service(s) => (*s.name).clone(),
171 Item::NewType(t) => (*t.name).clone(),
172 Item::Const(c) => (*c.name).clone(),
173 Item::Mod(m) => (*m.name).clone(),
174 }
175 }
176
177 pub fn is_ty(&self) -> bool {
178 matches!(
179 self,
180 Item::Message(_) | Item::Enum(_) | Item::Service(_) | Item::NewType(_)
181 )
182 }
183}
184
185#[derive(Debug, Clone, Hash, PartialEq, Eq, Copy)]
186pub enum DefKind {
187 Type,
188 Value,
189 Mod,
190}
191
192#[derive(Debug, Clone, Hash, PartialEq, Eq)]
193pub struct Path {
194 pub kind: DefKind,
195 pub did: DefId,
196}
197
198#[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)]
199pub struct ItemPath(Arc<[Symbol]>);
200
201impl Deref for ItemPath {
202 type Target = [Symbol];
203
204 fn deref(&self) -> &Self::Target {
205 &self.0
206 }
207}
208
209impl<T> From<T> for ItemPath
210where
211 T: Into<Arc<[Symbol]>>,
212{
213 fn from(t: T) -> Self {
214 ItemPath(t.into())
215 }
216}
217
218#[derive(PartialEq, Eq, Clone, Debug)]
219pub struct File {
220 pub package: ItemPath,
221 pub items: Vec<DefId>,
222 pub file_id: FileId,
223 pub uses: Vec<FileId>,
224 pub descriptor: Bytes,
225 pub extensions: FileExts,
226 pub comments: FastStr,
227}
228
229#[derive(Debug, PartialEq, Eq, Clone)]
230pub enum NodeKind {
231 Item(Arc<Item>),
232 Variant(Arc<EnumVariant>),
233 Field(Arc<Field>),
234 Method(Arc<Method>),
235 Arg(Arc<Arg>),
236}
237
238#[derive(Debug, PartialEq, Eq, Clone)]
239pub struct Node {
240 pub file_id: FileId,
241 pub kind: NodeKind,
242 pub parent: Option<DefId>,
243 pub tags: TagId,
244 pub related_nodes: Vec<DefId>,
245}
246
247impl Node {
248 pub(crate) fn expect_item(&self) -> &Item {
249 match &self.kind {
250 NodeKind::Item(item) => item,
251 _ => panic!(),
252 }
253 }
254
255 pub(crate) fn name(&self) -> Symbol {
256 match &self.kind {
257 NodeKind::Item(item) => item.symbol_name(),
258 NodeKind::Variant(v) => v.name.sym.clone(),
259 NodeKind::Field(f) => f.name.sym.clone(),
260 NodeKind::Method(m) => m.name.sym.clone(),
261 NodeKind::Arg(a) => a.name.sym.clone(),
262 }
263 }
264}
265
266#[derive(Clone, Debug, PartialEq, Eq)]
267pub struct Pkg {
268 pub path: ItemPath,
269 pub items: Vec<DefId>,
270}