stern4rust/finding/model/qualified_call.rs
1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5// A call reached through a path the file never imported.
6//
7// The correction is the interesting part. Every such path can be repaired by
8// importing enough of it that at most one segment is left at the call site, and
9// the two shapes differ: `syn::parse_file` has nothing worth keeping as a
10// qualifier, so the whole path is imported and the call becomes `parse_file`,
11// while `std::env::args` keeps `env` because it says something -- `use std::env`
12// and `env::args`.
13pub struct QualifiedCall {
14 pub path: String,
15 pub line: usize,
16}
17
18impl QualifiedCall {
19 pub fn new(path: &str, line: usize) -> Self {
20 Self {
21 path: path.to_string(),
22 line,
23 }
24 }
25
26 // What the call site reads as afterwards: the last segment on its own, or
27 // the last two when a module qualifier is worth keeping.
28 pub fn call(&self) -> String {
29 let segments = self.segments();
30 if segments.len() <= 2 {
31 return segments.last().copied().unwrap_or_default().to_string();
32 }
33 segments[segments.len() - 2..].join("::")
34 }
35
36 // What to import so that at most one imported segment is left at the call.
37 pub fn import(&self) -> String {
38 let segments = self.segments();
39 if segments.len() <= 2 {
40 return self.path.clone();
41 }
42 segments[..segments.len() - 1].join("::")
43 }
44
45 fn segments(&self) -> Vec<&str> {
46 self.path.split("::").collect()
47 }
48}