1use std::path::PathBuf;
7
8#[derive(Debug, Clone)]
10pub struct FunctionInfo {
11 pub name: String,
13
14 pub module_path: String,
16
17 pub signature: String,
19
20 pub body: String,
22
23 pub is_public: bool,
25
26 pub doc_comments: Vec<String>,
28
29 pub source_file: PathBuf,
31
32 pub line_number: usize,
34}
35
36#[derive(Debug, Clone)]
38pub struct StructInfo {
39 pub name: String,
41
42 pub module_path: String,
44
45 pub definition: String,
47
48 pub is_public: bool,
50
51 pub doc_comments: Vec<String>,
53
54 pub source_file: PathBuf,
56
57 pub line_number: usize,
59
60 pub fields: Vec<FieldInfo>,
62}
63
64#[derive(Debug, Clone)]
66pub struct FieldInfo {
67 pub name: Option<String>,
69
70 pub ty: String,
72
73 pub is_public: bool,
75
76 pub doc_comments: Vec<String>,
78}
79
80#[derive(Debug, Clone)]
82pub struct EnumInfo {
83 pub name: String,
85
86 pub module_path: String,
88
89 pub definition: String,
91
92 pub is_public: bool,
94
95 pub doc_comments: Vec<String>,
97
98 pub source_file: PathBuf,
100
101 pub line_number: usize,
103
104 pub variants: Vec<VariantInfo>,
106}
107
108#[derive(Debug, Clone)]
110pub struct VariantInfo {
111 pub name: String,
113
114 pub doc_comments: Vec<String>,
116
117 pub fields: Vec<FieldInfo>,
119}
120
121#[derive(Debug, Clone)]
123pub struct TraitInfo {
124 pub name: String,
126
127 pub module_path: String,
129
130 pub definition: String,
132
133 pub is_public: bool,
135
136 pub doc_comments: Vec<String>,
138
139 pub source_file: PathBuf,
141
142 pub line_number: usize,
144}
145
146#[derive(Debug, Clone)]
148pub struct ImplInfo {
149 pub self_ty: String,
151
152 pub trait_name: Option<String>,
154
155 pub module_path: String,
157
158 pub methods: Vec<FunctionInfo>,
160
161 pub source_file: PathBuf,
163
164 pub line_number: usize,
166}
167
168#[derive(Debug, Clone)]
170pub struct ConstInfo {
171 pub name: String,
173
174 pub module_path: String,
176
177 pub ty: String,
179
180 pub value: String,
182
183 pub is_public: bool,
185
186 pub doc_comments: Vec<String>,
188
189 pub source_file: PathBuf,
191
192 pub line_number: usize,
194}
195
196#[derive(Debug, Clone)]
198pub struct StaticInfo {
199 pub name: String,
201
202 pub module_path: String,
204
205 pub ty: String,
207
208 pub value: String,
210
211 pub is_mutable: bool,
213
214 pub is_public: bool,
216
217 pub doc_comments: Vec<String>,
219
220 pub source_file: PathBuf,
222
223 pub line_number: usize,
225}
226
227#[derive(Debug, Clone)]
229pub struct TypeAliasInfo {
230 pub name: String,
232
233 pub module_path: String,
235
236 pub aliased_type: String,
238
239 pub is_public: bool,
241
242 pub doc_comments: Vec<String>,
244
245 pub source_file: PathBuf,
247
248 pub line_number: usize,
250}
251
252#[derive(Debug, Clone)]
254pub struct MacroInfo {
255 pub name: String,
257
258 pub module_path: String,
260
261 pub definition: String,
263
264 pub doc_comments: Vec<String>,
266
267 pub source_file: PathBuf,
269
270 pub line_number: usize,
272}
273
274#[derive(Debug, Default)]
276pub struct CrateSource {
277 pub name: String,
279
280 pub version: String,
282
283 pub root_path: PathBuf,
285
286 pub functions: Vec<FunctionInfo>,
288
289 pub structs: Vec<StructInfo>,
291
292 pub enums: Vec<EnumInfo>,
294
295 pub traits: Vec<TraitInfo>,
297
298 pub impls: Vec<ImplInfo>,
300
301 pub constants: Vec<ConstInfo>,
303
304 pub statics: Vec<StaticInfo>,
306
307 pub type_aliases: Vec<TypeAliasInfo>,
309
310 pub macros: Vec<MacroInfo>,
312}
313
314impl CrateSource {
315 #[must_use]
317 pub fn new(name: String, version: String, root_path: PathBuf) -> Self {
318 Self {
319 name,
320 version,
321 root_path,
322 ..Default::default()
323 }
324 }
325
326 #[must_use]
328 pub fn find_function(&self, path: &str) -> Option<&FunctionInfo> {
329 self.functions.iter().find(|f| {
330 let full_path = format!("{}::{}", f.module_path, f.name);
331 full_path == path
332 })
333 }
334
335 #[must_use]
337 pub fn find_struct(&self, path: &str) -> Option<&StructInfo> {
338 self.structs.iter().find(|s| {
339 let full_path = format!("{}::{}", s.module_path, s.name);
340 full_path == path
341 })
342 }
343
344 #[must_use]
346 pub fn find_enum(&self, path: &str) -> Option<&EnumInfo> {
347 self.enums.iter().find(|e| {
348 let full_path = format!("{}::{}", e.module_path, e.name);
349 full_path == path
350 })
351 }
352
353 #[must_use]
355 pub fn find_trait(&self, path: &str) -> Option<&TraitInfo> {
356 self.traits.iter().find(|t| {
357 let full_path = format!("{}::{}", t.module_path, t.name);
358 full_path == path
359 })
360 }
361
362 #[must_use]
364 pub fn find_const(&self, path: &str) -> Option<&ConstInfo> {
365 self.constants.iter().find(|c| {
366 let full_path = format!("{}::{}", c.module_path, c.name);
367 full_path == path
368 })
369 }
370
371 #[must_use]
373 pub fn find_static(&self, path: &str) -> Option<&StaticInfo> {
374 self.statics.iter().find(|s| {
375 let full_path = format!("{}::{}", s.module_path, s.name);
376 full_path == path
377 })
378 }
379
380 #[must_use]
382 pub fn private_items_in_module(&self, module_path: &str) -> Vec<PrivateItem<'_>> {
383 let mut items = Vec::new();
384
385 for f in &self.functions {
386 if !f.is_public && f.module_path == module_path {
387 items.push(PrivateItem::Function(f));
388 }
389 }
390
391 for s in &self.structs {
392 if !s.is_public && s.module_path == module_path {
393 items.push(PrivateItem::Struct(s));
394 }
395 }
396
397 for e in &self.enums {
398 if !e.is_public && e.module_path == module_path {
399 items.push(PrivateItem::Enum(e));
400 }
401 }
402
403 items
404 }
405}
406
407#[derive(Debug)]
409pub enum PrivateItem<'a> {
410 Function(&'a FunctionInfo),
412
413 Struct(&'a StructInfo),
415
416 Enum(&'a EnumInfo),
418
419 Const(&'a ConstInfo),
421
422 TypeAlias(&'a TypeAliasInfo),
424}