MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveRustModuleFile

Function resolveRustModuleFile

src/resolution/import-resolver.ts:1891–1943  ·  view source on GitHub ↗

* Resolve a Rust module path (segments WITHOUT the leaf symbol) to the file of * the last module segment — `crate::a::b` → ` /a/b.rs` (or `.../b/mod.rs`). * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative.

(
  segments: string[],
  fromFile: string,
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

1889 * Anchors on `crate` / `self` / `super`; a bare path is tried crate-relative.
1890 */
1891function resolveRustModuleFile(
1892 segments: string[],
1893 fromFile: string,
1894 context: ResolutionContext
1895): string | null {
1896 if (segments.length === 0) return null;
1897 const projectRoot = context.getProjectRoot();
1898 const fromAbs = path.join(projectRoot, fromFile);
1899 const toRel = (p: string) => path.relative(projectRoot, p).replace(/\\/g, '/');
1900
1901 // Walk a sequence of module segments down from `startDir`, mapping each to a
1902 // `<seg>.rs` or `<seg>/mod.rs` file. Returns the leaf module's file, or null
1903 // if `startDir` is null or any segment has no file on disk.
1904 const resolveUnder = (startDir: string | null, rest: string[]): string | null => {
1905 if (!startDir) return null;
1906 let dir = startDir;
1907 let targetFile: string | null = null;
1908 for (const seg of rest) {
1909 if (seg === 'self' || seg === 'crate' || seg === 'super') continue;
1910 const asFile = toRel(path.join(dir, seg + '.rs'));
1911 const asMod = toRel(path.join(dir, seg, 'mod.rs'));
1912 if (context.fileExists(asFile)) targetFile = asFile;
1913 else if (context.fileExists(asMod)) targetFile = asMod;
1914 else return null;
1915 dir = path.join(dir, seg);
1916 }
1917 return targetFile;
1918 };
1919
1920 const first = segments[0]!;
1921 if (first === 'crate') {
1922 return resolveUnder(rustCrateRootDir(fromAbs, context), segments.slice(1));
1923 }
1924 if (first === 'self') {
1925 return resolveUnder(rustSelfModuleDir(fromAbs), segments.slice(1));
1926 }
1927 if (first === 'super') {
1928 let supers = 0;
1929 while (segments[supers] === 'super') supers++;
1930 let dir: string | null = rustSelfModuleDir(fromAbs);
1931 for (let s = 0; s < supers && dir; s++) dir = path.dirname(dir);
1932 return resolveUnder(dir, segments.slice(supers));
1933 }
1934 // Bare path. In expression position (`submodule::item()` — the router-assembly
1935 // and general cross-module-call pattern) the prefix is a SUBMODULE of the
1936 // current module, i.e. 2018 `self::`-relative — so try self-relative FIRST.
1937 // Fall back to crate-relative for 2015-edition / crate-root items. External
1938 // crate paths (`serde::de::Error`) miss both and fall through to name-matching.
1939 return (
1940 resolveUnder(rustSelfModuleDir(fromAbs), segments) ??
1941 resolveUnder(rustCrateRootDir(fromAbs, context), segments)
1942 );
1943}
1944
1945/**
1946 * Resolve a Java/Kotlin reference whose receiver is the simple name of

Callers 1

resolveRustPathReferenceFunction · 0.85

Calls 5

resolveUnderFunction · 0.85
rustCrateRootDirFunction · 0.85
rustSelfModuleDirFunction · 0.85
getProjectRootMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected