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
// Signal collector implementations for RustcCollector, ClippyCollector, and TestCollector.
// Included by signal_collector.rs - shares parent module scope.
#[async_trait]
impl SignalCollector for RustcCollector {
fn source(&self) -> SignalSource {
SignalSource::Rustc
}
async fn collect(&self, project_path: &Path) -> Result<Vec<SignalEvidence>> {
let output = Command::new("cargo")
.args(["build", "--message-format=json"])
.current_dir(project_path)
.output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut signals = Vec::new();
for line in stdout.lines() {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(line) {
if json.get("reason").and_then(|r| r.as_str()) == Some("compiler-message") {
if let Some(message) = json.get("message") {
if let Some(level) = message.get("level").and_then(|l| l.as_str()) {
if level == "error" {
let code = message
.get("code")
.and_then(|c| c.get("code"))
.and_then(|c| c.as_str())
.map(String::from);
let rendered = message
.get("rendered")
.and_then(|r| r.as_str())
.unwrap_or("")
.to_string();
signals.push(SignalEvidence {
source: SignalSource::Rustc,
raw_message: rendered,
error_code: code,
weight: 1.0,
});
}
}
}
}
}
}
Ok(signals)
}
}
#[async_trait]
impl SignalCollector for ClippyCollector {
fn source(&self) -> SignalSource {
SignalSource::Clippy
}
async fn collect(&self, project_path: &Path) -> Result<Vec<SignalEvidence>> {
let output = Command::new("cargo")
.args(["clippy", "--message-format=json", "--", "-D", "warnings"])
.current_dir(project_path)
.output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut signals = Vec::new();
for line in stdout.lines() {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(line) {
if json.get("reason").and_then(|r| r.as_str()) == Some("compiler-message") {
if let Some(message) = json.get("message") {
if let Some(level) = message.get("level").and_then(|l| l.as_str()) {
if level == "warning" || level == "error" {
let code = message
.get("code")
.and_then(|c| c.get("code"))
.and_then(|c| c.as_str())
.map(String::from);
let rendered = message
.get("rendered")
.and_then(|r| r.as_str())
.unwrap_or("")
.to_string();
// Weight based on lint category
let weight = if code
.as_ref()
.map(|c| c.starts_with("clippy::correctness"))
.unwrap_or(false)
{
1.0
} else if code
.as_ref()
.map(|c| c.starts_with("clippy::suspicious"))
.unwrap_or(false)
{
0.9
} else if code
.as_ref()
.map(|c| c.starts_with("clippy::complexity"))
.unwrap_or(false)
{
0.7
} else {
0.5
};
signals.push(SignalEvidence {
source: SignalSource::Clippy,
raw_message: rendered,
error_code: code,
weight,
});
}
}
}
}
}
}
Ok(signals)
}
}
#[async_trait]
impl SignalCollector for TestCollector {
fn source(&self) -> SignalSource {
SignalSource::CargoTest
}
async fn collect(&self, project_path: &Path) -> Result<Vec<SignalEvidence>> {
let output = Command::new("cargo")
.args([
"test",
"--no-fail-fast",
"--",
"--format=json",
"-Z",
"unstable-options",
])
.current_dir(project_path)
.output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut signals = Vec::new();
for line in stdout.lines() {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(line) {
if json.get("type").and_then(|t| t.as_str()) == Some("test")
&& json.get("event").and_then(|e| e.as_str()) == Some("failed")
{
let name = json
.get("name")
.and_then(|n| n.as_str())
.unwrap_or("unknown")
.to_string();
let stdout_text = json
.get("stdout")
.and_then(|s| s.as_str())
.unwrap_or("")
.to_string();
signals.push(SignalEvidence {
source: SignalSource::CargoTest,
raw_message: format!("Test failed: {}\n{}", name, stdout_text),
error_code: None,
weight: 1.0,
});
}
}
}
Ok(signals)
}
}