MCPcopy Index your code
hub / github.com/carllerche/assert-struct / absolute_source_path

Function absolute_source_path

assert-struct/src/error.rs:77–103  ·  view source on GitHub ↗

Derive the absolute source file path from two compile-time constants: - `manifest_dir`: value of `env!("CARGO_MANIFEST_DIR")` — absolute path to the package root - `file_path`: value of `file!()` — path relative to the workspace root In a Rust workspace, `file!()` is workspace-root-relative while `CARGO_MANIFEST_DIR` is package-root-absolute. The package dir name(s) appear as a suffix of `manifes

(manifest_dir: &str, file_path: &str)

Source from the content-addressed store, hash-verified

75///
76/// Works for nested members too (overlap spans multiple path components).
77fn absolute_source_path(manifest_dir: &str, file_path: &str) -> PathBuf {
78 use std::path::Path;
79
80 let manifest = Path::new(manifest_dir);
81 let file = Path::new(file_path);
82
83 // Collect path components for each side.
84 let manifest_components: Vec<_> = manifest.components().collect();
85 let file_components: Vec<_> = file.components().collect();
86
87 // Find the longest suffix of manifest_components that matches a prefix of file_components.
88 let mut overlap = 0;
89 for len in 1..=manifest_components.len().min(file_components.len()) {
90 let suffix = &manifest_components[manifest_components.len() - len..];
91 let prefix = &file_components[..len];
92 if suffix == prefix {
93 overlap = len;
94 }
95 }
96
97 // Workspace root = manifest_dir minus the overlapping suffix.
98 let workspace_root: PathBuf = manifest_components[..manifest_components.len() - overlap]
99 .iter()
100 .collect();
101
102 workspace_root.join(file)
103}
104
105/// Compute the byte offset of a (line, col) position within a source string.
106/// `line` is 1-indexed; `col` is 0-indexed (as returned by proc_macro2's span locations).

Callers 1

newMethod · 0.85

Calls 1

lenMethod · 0.80

Tested by

no test coverage detected