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