crawk 0.5.2

Dependency crawler for Rust. It crawls so you don't have to untangle
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Module for parsing Rust source files and extracting type references.
//!
//! Provides [`CrateAnalyzer`] for collecting [`TypeReference`]s from multiple
//! source files within a crate.

mod visitor;

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use crate::cache::ParseCache;
use crate::utils::{ReadFileError, descend_inline_module, read_source_file};

use syn::File;
use syn::visit::Visit;
use thiserror::Error;

use tracing::info;

use crate::reference::{PathPrefix, TypeReference};
use visitor::ModuleVisitor;

/// Errors that can occur while reading or parsing a Rust source file.
///
/// Returned as the `source` field of [`AnalysisError::ModuleAnalysisFailed`](crate::AnalysisError::ModuleAnalysisFailed).
#[derive(Debug, Error)]
pub(crate) enum AnalyzerError {
    /// The source file could not be read from disk.
    #[error("Failed to read file '{path}': {source}")]
    FileRead {
        /// Path to the file that could not be read.
        path: PathBuf,
        /// The underlying I/O error.
        #[source]
        source: std::io::Error,
    },

    /// The source file exceeds the maximum allowed size and was not parsed.
    ///
    /// This limit exists to prevent excessive memory usage on unexpectedly large files.
    #[error("File too large '{path}': {size} bytes (limit {limit} bytes)")]
    FileTooLarge {
        /// Path to the oversized file.
        path: PathBuf,
        /// Actual file size in bytes.
        size: u64,
        /// Maximum allowed size in bytes.
        limit: u64,
    },

    /// The source file could not be parsed as valid Rust syntax.
    #[error("Failed to parse file '{path}': {message}")]
    Parse {
        /// Path to the file that failed to parse.
        path: PathBuf,
        /// Description of the parse error from `syn`.
        message: String,
    },
}

/// Result type for analyzer operations.
pub(crate) type Result<T> = std::result::Result<T, AnalyzerError>;

/// Analyzer for collecting type references from a Rust crate.
#[derive(Debug, Clone)]
pub(crate) struct CrateAnalyzer {
    /// Name of the crate being analyzed.
    crate_name: String,

    /// Collected references per module path.
    files: HashMap<String, Vec<TypeReference>>,

    /// Order in which files were parsed (for deterministic iteration).
    file_order: Vec<String>,
}

impl CrateAnalyzer {
    /// Creates a new analyzer for the given crate.
    pub(crate) fn new(crate_name: impl Into<String>) -> Self {
        Self {
            crate_name: crate_name.into(),
            files: HashMap::new(),
            file_order: Vec::new(),
        }
    }

    /// Parses a single source file and collects type references.
    ///
    /// When `inline_scope` is non-empty, the visitor is scoped to only the items
    /// inside the target inline module instead of visiting the entire file.
    /// For example, if parsing `glob_patterns::utilities` from `glob_patterns.rs`,
    /// `inline_scope` would be `["utilities"]`.
    pub(crate) fn parse_file(
        &mut self,
        module: impl Into<String>,
        path: &Path,
        inline_scope: &[String],
        children: HashSet<String>,
        cache: &mut ParseCache,
    ) -> Result<Vec<TypeReference>> {
        let syntax: Rc<File> = cache.get_or_parse(path, |p| {
            let content = read_source_file(p).map_err(|e| match e {
                ReadFileError::Io(source) => AnalyzerError::FileRead {
                    path: p.to_path_buf(),
                    source,
                },
                ReadFileError::TooLarge { size, limit } => AnalyzerError::FileTooLarge {
                    path: p.to_path_buf(),
                    size,
                    limit,
                },
            })?;
            syn::parse_file(&content).map_err(|e| AnalyzerError::Parse {
                path: p.to_path_buf(),
                message: e.to_string(),
            })
        })?;

        let module = module.into();
        let package_name = Some(self.crate_name.clone());
        let mut visitor = ModuleVisitor::new(module.clone(), children, package_name);

        if inline_scope.is_empty() {
            visitor.visit_file(&syntax);
        } else if let Some(items) = descend_inline_module(&syntax.items, inline_scope) {
            for item in items {
                visitor.visit_item(item);
            }
        }

        let result: Vec<TypeReference> = visitor.references.all().cloned().collect();
        info!(
            "Parsed '{module}': {} references from {}{}",
            result.len(),
            path.display(),
            if inline_scope.is_empty() {
                String::new()
            } else {
                format!(" (inline {inline_scope:?})")
            }
        );

        if !self.files.contains_key(&module) {
            self.file_order.push(module.clone());
        }
        self.files.insert(module, result.clone());

        Ok(result)
    }

    /// Returns all collected crate internal references by module, in parse order.
    ///
    /// `children_map` maps each module path to the set of its direct child module
    /// names. This allows bare `use child::Item` paths (valid in Rust ≥2018 for
    /// direct children declared via `mod`) to be recognised as internal references.
    pub(crate) fn all_crate_references<'a>(
        &'a self,
        children_map: &'a HashMap<String, HashSet<String>>,
    ) -> impl Iterator<Item = (&'a String, Vec<&'a TypeReference>)> {
        self.file_order.iter().filter_map(move |module| {
            self.files.get(module).map(|refs| {
                let crate_refs: Vec<&TypeReference> = refs
                    .iter()
                    .filter(|r| {
                        r.is_relative()
                            || r.is_from_crate(&self.crate_name)
                            || Self::is_bare_child(r, module, children_map)
                    })
                    .collect();
                (module, crate_refs)
            })
        })
    }

    /// Check if a `PathPrefix::None` reference targets an internal module.
    ///
    /// Two resolution rules are checked:
    /// - **Edition 2018+**: bare `use child::Item` resolves to a direct child
    ///   declared via `mod child;` in the current module.
    /// - **Edition 2015**: bare `use sibling::Item` resolves from the crate
    ///   root, so any top-level module is reachable from anywhere.
    ///
    /// Both are covered by checking `children_map[module]` (direct children)
    /// and `children_map[""]` (crate-root children / top-level modules).
    fn is_bare_child(
        r: &TypeReference,
        module: &str,
        children_map: &HashMap<String, HashSet<String>>,
    ) -> bool {
        if r.prefix() != PathPrefix::None {
            return false;
        }
        let first = r.segments().first();
        first.is_some_and(|s| {
            children_map
                .get(module)
                .is_some_and(|ch| ch.contains(s.as_str()))
                || children_map
                    .get("")
                    .is_some_and(|ch| ch.contains(s.as_str()))
        })
    }
}

#[cfg(test)]
impl CrateAnalyzer {
    pub(crate) fn crate_name(&self) -> &str {
        &self.crate_name
    }

    pub(crate) fn total_references(&self) -> usize {
        self.files.values().map(Vec::len).sum()
    }

    pub(crate) fn file_count(&self) -> usize {
        self.files.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::reference::PathPrefix;
    use std::io::Write;
    use std::path::Path;
    use tempfile::NamedTempFile;

    fn parse_use(code: &str) -> Vec<TypeReference> {
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("", HashSet::new(), None);
        visitor.visit_file(&syntax);
        visitor.references.all().cloned().collect()
    }

    #[test]
    fn test_simple_use() {
        let refs = parse_use("use std::collections::HashMap;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "std::collections::HashMap");
    }

    #[test]
    fn test_use_alias() {
        let refs = parse_use("use std::collections::HashMap as Map;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "std::collections::HashMap as Map");
    }

    #[test]
    fn test_use_glob() {
        let refs = parse_use("use std::collections::*;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "std::collections::*");
        assert!(refs[0].has_glob());
    }

    #[test]
    fn test_use_crate() {
        let refs = parse_use("use crate::module::Type;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "crate::module::Type");
        assert!(refs[0].is_relative());
    }

    #[test]
    fn test_use_self() {
        // self:: at crate root resolves to crate::
        let refs = parse_use("use self::submodule::Type;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "crate::submodule::Type");
        assert_eq!(refs[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_use_super() {
        // super:: at crate root cannot be resolved (invalid), stays as super::
        let refs = parse_use("use super::sibling::Type;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "super::sibling::Type");
        assert_eq!(refs[0].prefix(), PathPrefix::Super(1));
    }

    #[test]
    fn test_use_super_multiple() {
        // super::super:: at crate root cannot be resolved, stays as super::super::
        let refs = parse_use("use super::super::ancestor::Type;");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "super::super::ancestor::Type");
        assert_eq!(refs[0].prefix(), PathPrefix::Super(2));
    }

    #[test]
    fn test_use_group() {
        let refs = parse_use("use std::collections::{HashMap, HashSet};");
        assert_eq!(refs.len(), 1);
        assert!(refs[0].has_group());
        assert_eq!(
            refs[0].to_path_string(),
            "std::collections::{HashMap, HashSet}"
        );
    }

    #[test]
    fn test_use_group_with_self() {
        let refs = parse_use("use std::collections::{self, HashMap};");
        assert_eq!(refs.len(), 1);
        assert!(refs[0].has_group());
    }

    #[test]
    fn test_use_nested_group() {
        let refs = parse_use("use std::{collections::{HashMap, HashSet}, io::Read};");
        assert_eq!(refs.len(), 1);
        assert!(refs[0].has_group());
    }

    #[test]
    fn test_use_group_path_items_produce_nested_with_items() {
        // Regression: `client::ClientAction` inside a group must produce
        // Nested { prefix: ["client"], items: [Simple("ClientAction")] }
        // so that expand_groups can expand it correctly.
        use crate::analyzer::expand_groups;
        use crate::reference::GroupItem;

        let refs = parse_use(
            "use crate::args::{client::ClientAction, system::{PingArgs, StatsArgs}, topic::TopicAction};",
        );
        assert_eq!(refs.len(), 1);
        assert!(refs[0].has_group());

        let expanded = expand_groups(&refs[0]);
        let paths: Vec<String> = expanded.iter().map(TypeReference::to_path_string).collect();

        assert!(
            paths.contains(&"crate::args::client::ClientAction".to_owned()),
            "client::ClientAction missing from expanded: {paths:?}"
        );
        assert!(
            paths.contains(&"crate::args::topic::TopicAction".to_owned()),
            "topic::TopicAction missing from expanded: {paths:?}"
        );
        assert!(
            paths.contains(&"crate::args::system::PingArgs".to_owned()),
            "system::PingArgs missing from expanded: {paths:?}"
        );
        assert!(
            paths.contains(&"crate::args::system::StatsArgs".to_owned()),
            "system::StatsArgs missing from expanded: {paths:?}"
        );
        assert_eq!(expanded.len(), 4);

        // Verify the group items are Nested with non-empty items
        if let crate::reference::PathSuffix::Group(items) = refs[0].suffix() {
            for item in items {
                if let GroupItem::Nested { prefix, items } = item {
                    assert!(
                        !items.is_empty(),
                        "Nested group item {prefix:?} has empty items"
                    );
                }
            }
        }
    }

    #[test]
    fn test_crate_analyzer() {
        let analyzer = CrateAnalyzer::new("test_crate");
        assert_eq!(analyzer.crate_name(), "test_crate");
        assert_eq!(analyzer.file_count(), 0);
        assert_eq!(analyzer.total_references(), 0);
    }

    #[test]
    fn test_type_path_collection() {
        let code = "
            fn foo(x: crate::MyType) -> crate::Result {
                let y: crate::Other = x;
                y
            }
        ";
        let refs = parse_use(code);
        assert!(refs.len() >= 2, "Should capture type annotations");

        // Should capture both MyType and Result (and possibly Other)
        let paths: Vec<String> = refs.iter().map(TypeReference::to_path_string).collect();
        assert!(paths.iter().any(|p| p.contains("MyType")));
        assert!(paths.iter().any(|p| p.contains("Result")));
    }

    #[test]
    fn test_expr_path_collection() {
        let code = "
            fn foo() {
                crate::module::function();
                let x = crate::module::Type::new();
            }
        ";
        let refs = parse_use(code);
        assert!(refs.len() >= 2, "Should capture expression paths");

        let paths: Vec<String> = refs.iter().map(TypeReference::to_path_string).collect();
        assert!(paths.iter().any(|p| p.contains("function")));
        assert!(paths.iter().any(|p| p.contains("Type")));
    }

    #[test]
    fn test_impl_trait_collection() {
        let code = "
            impl crate::MyTrait for Foo {
                fn bar() {}
            }
        ";
        let refs = parse_use(code);
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].to_path_string(), "crate::MyTrait");
    }

    #[test]
    fn test_struct_pattern_collection() {
        let code = "
            fn foo(x: Something) {
                match x {
                    crate::module::Variant { field } => field,
                }
            }
        ";
        let refs = parse_use(code);
        assert!(refs.iter().any(|r| r.to_path_string().contains("Variant")));
    }

    #[test]
    fn test_macro_path_collection() {
        let code = "
            fn foo() {
                crate::macros::my_macro!();
            }
        ";
        let refs = parse_use(code);
        assert_eq!(refs.len(), 1);
        assert!(refs[0].to_path_string().contains("my_macro"));
    }

    #[test]
    fn test_resolve_self_in_module() {
        // Test resolution of self:: in a nested module
        let code = "use self::submodule::Type;";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils::parser", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(
            uses[0].to_path_string(),
            "crate::utils::parser::submodule::Type"
        );
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_resolve_super_in_nested_module() {
        // Test resolution of super:: in a nested module
        let code = "use super::sibling::Type;";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils::parser", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(uses[0].to_path_string(), "crate::utils::sibling::Type");
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_resolve_super_multiple_in_deeply_nested_module() {
        // Test resolution of super::super:: in a deeply nested module
        let code = "use super::super::ancestor::Type;";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("a::b::c", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(uses[0].to_path_string(), "crate::a::ancestor::Type");
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_resolve_preserves_groups() {
        // Test that resolution works with grouped imports
        let code = "use self::{foo, bar::Baz};";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(uses[0].to_path_string(), "crate::utils::{foo, bar::Baz}");
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_resolve_preserves_glob() {
        // Test that resolution works with glob imports
        let code = "use self::submodule::*;";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(uses[0].to_path_string(), "crate::utils::submodule::*");
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
        assert!(uses[0].has_glob());
    }

    #[test]
    fn test_resolve_preserves_alias() {
        // Test that resolution works with aliased imports
        let code = "use self::submodule::Type as MyType;";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils", HashSet::new(), None);
        visitor.visit_file(&syntax);

        let uses = &visitor.references.use_statements;
        assert_eq!(uses.len(), 1);
        assert_eq!(
            uses[0].to_path_string(),
            "crate::utils::submodule::Type as MyType"
        );
        assert_eq!(uses[0].prefix(), PathPrefix::Crate);
    }

    #[test]
    fn test_resolve_expression_paths() {
        // Test that resolution works for paths in expressions
        let code = "
            fn foo() {
                self::helper::do_something();
                super::sibling::bar();
            }
        ";
        let syntax: File = syn::parse_file(code).unwrap();
        let mut visitor = ModuleVisitor::new("utils::parser", HashSet::new(), None);
        visitor.visit_file(&syntax);

        assert!(visitor.references.value_refs.len() >= 2);

        // Check that paths are resolved
        let paths: Vec<String> = visitor
            .references
            .value_refs
            .iter()
            .map(TypeReference::to_path_string)
            .collect();

        assert!(
            paths
                .iter()
                .any(|p| p.contains("crate::utils::parser::helper"))
        );
        assert!(paths.iter().any(|p| p.contains("crate::utils::sibling")));
    }

    #[test]
    fn parse_file_returns_file_read_error_for_nonexistent_file() {
        let mut analyzer = CrateAnalyzer::new("test");
        let mut cache = ParseCache::new();
        let err = analyzer
            .parse_file(
                "mod",
                Path::new("/nonexistent/file.rs"),
                &[],
                HashSet::new(),
                &mut cache,
            )
            .unwrap_err();
        assert!(matches!(err, AnalyzerError::FileRead { .. }));
    }

    #[test]
    fn parse_file_returns_parse_error_for_invalid_syntax() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "this is not valid rust !!!").unwrap();
        let mut analyzer = CrateAnalyzer::new("test");
        let mut cache = ParseCache::new();
        let err = analyzer
            .parse_file("mod", f.path(), &[], HashSet::new(), &mut cache)
            .unwrap_err();
        assert!(matches!(err, AnalyzerError::Parse { .. }));
    }

    #[test]
    fn read_source_file_returns_file_too_large_when_size_exceeds_limit() {
        use crate::utils::{MAX_FILE_BYTES, ReadFileError};
        use std::io::Write;
        let mut f = NamedTempFile::new().unwrap();
        let chunk = vec![b' '; 1024];
        for _ in 0..=(MAX_FILE_BYTES / 1024) {
            f.write_all(&chunk).unwrap();
        }
        f.flush().unwrap();
        let err = read_source_file(f.path()).unwrap_err();
        assert!(matches!(err, ReadFileError::TooLarge { limit, .. } if limit == MAX_FILE_BYTES));
    }
}