This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
/
rustc.rs
107 lines (87 loc) · 2.76 KB
/
rustc.rs
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
use std::path::{self, Component, Path as StdPath, PathBuf};
use colored::Colorize;
use super::rust_repo;
#[derive(Debug, Eq, PartialEq)]
pub struct Path<'p> {
rustc_prefix: PathBuf,
rust_repo_path: rust_repo::Path<'p>,
}
impl<'p> Path<'p> {
pub fn from_std_path(path: &'p StdPath) -> Option<Self> {
if !path.is_absolute() {
return None;
}
let mut components = path.components();
let mut rustc_prefix = PathBuf::new();
for component in &mut components {
rustc_prefix.push(component);
if let Component::Normal(component) = component {
if component == "rustc" {
break;
}
}
}
let hash = super::get_component_normal(components.next()?)?.to_str()?;
if !hash.chars().all(|c| char::is_ascii_hexdigit(&c)) {
return None;
}
rustc_prefix.push(hash);
let rust_repo_path = rust_repo::Path::from_std_path(components.as_path());
Some(Path {
rustc_prefix,
rust_repo_path,
})
}
pub fn format_short(&self) -> String {
format!(
"[rust]{}{}",
path::MAIN_SEPARATOR,
self.rust_repo_path.format()
)
}
pub fn format_highlight(&self) -> String {
format!(
"{}{}{}",
self.rustc_prefix.display().to_string().dimmed(),
path::MAIN_SEPARATOR,
self.rust_repo_path.format_highlight()
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn end_to_end() {
let home = dirs::home_dir().unwrap();
let home = home.to_str().unwrap();
let input = PathBuf::from(home)
.join("rustc")
.join("9bc8c42bb2f19e745a63f3445f1ac248fb015e53")
.join("library")
.join("core")
.join("src")
.join("panicking.rs");
let rustc_prefix = PathBuf::from(home)
.join("rustc")
.join("9bc8c42bb2f19e745a63f3445f1ac248fb015e53");
let path = Path::from_std_path(&input).unwrap();
let expected_path = PathBuf::from("src").join("panicking.rs");
let expected = Path {
rustc_prefix,
rust_repo_path: rust_repo::Path::One52(rust_repo::One52Path {
library: "library",
crate_name: "core",
path: &expected_path,
}),
};
assert_eq!(expected, path);
let expected = PathBuf::from("[rust]")
.join("library")
.join("core")
.join("src")
.join("panicking.rs");
let formatted_str = path.format_short();
assert_eq!(expected.to_string_lossy(), formatted_str);
}
}