Skip to content

Commit 33dfe29

Browse files
authored
Ensure all messages are JSON rpc payloads (#23362)
1 parent 9f4b690 commit 33dfe29

File tree

8 files changed

+44
-31
lines changed

8 files changed

+44
-31
lines changed

native_locator/src/common_python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn get_env_path(path: &str) -> Option<String> {
2424
fn report_path_python(dispatcher: &mut impl messaging::MessageDispatcher, path: &str) {
2525
let version = utils::get_version(path);
2626
let env_path = get_env_path(path);
27-
dispatcher.send_message(messaging::PythonEnvironment::new(
27+
dispatcher.report_environment(messaging::PythonEnvironment::new(
2828
"Python".to_string(),
2929
vec![path.to_string()],
3030
messaging::PythonEnvironmentCategory::System,

native_locator/src/conda.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ pub fn find_and_report(
371371
Some(conda_binary) => {
372372
let params =
373373
messaging::EnvManager::new(vec![conda_binary.to_string_lossy().to_string()], None);
374-
let message = messaging::EnvManagerMessage::new(params);
375-
dispatcher.send_message(message);
374+
dispatcher.report_environment_manager(params);
376375

377376
let envs = get_distinct_conda_envs(conda_binary.to_path_buf(), environment);
378377
for env in envs {
@@ -406,8 +405,7 @@ pub fn find_and_report(
406405
Some(env_path.clone()),
407406
Some(env_path),
408407
);
409-
let message = messaging::PythonEnvironmentMessage::new(params);
410-
dispatcher.send_message(message);
408+
dispatcher.report_environment(params);
411409
}
412410
}
413411
None => (),

native_locator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ fn main() {
4646
}
4747
}
4848

49-
dispatcher.send_message(messaging::ExitMessage::new());
49+
dispatcher.exit();
5050
}

native_locator/src/messaging.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::logging::{LogLevel, LogMessage};
55
use serde::{Deserialize, Serialize};
66

77
pub trait MessageDispatcher {
8-
fn send_message<T: serde::Serialize>(&mut self, message: T) -> ();
8+
fn report_environment_manager(&mut self, env: EnvManager) -> ();
9+
fn report_environment(&mut self, env: PythonEnvironment) -> ();
10+
fn exit(&mut self) -> ();
911
fn log_debug(&mut self, message: &str) -> ();
1012
fn log_info(&mut self, message: &str) -> ();
1113
fn log_warning(&mut self, message: &str) -> ();
@@ -84,7 +86,7 @@ impl PythonEnvironment {
8486
version,
8587
activated_run,
8688
env_path,
87-
sys_prefix_path
89+
sys_prefix_path,
8890
}
8991
}
9092
}
@@ -126,26 +128,35 @@ impl ExitMessage {
126128
}
127129

128130
pub struct JsonRpcDispatcher {}
131+
fn send_message<T: serde::Serialize>(message: T) -> () {
132+
let message = serde_json::to_string(&message).unwrap();
133+
print!(
134+
"Content-Length: {}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{}",
135+
message.len(),
136+
message
137+
);
138+
}
129139
impl MessageDispatcher for JsonRpcDispatcher {
130-
fn send_message<T: serde::Serialize>(&mut self, message: T) -> () {
131-
let message = serde_json::to_string(&message).unwrap();
132-
print!(
133-
"Content-Length: {}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{}",
134-
message.len(),
135-
message
136-
);
140+
fn report_environment_manager(&mut self, env: EnvManager) -> () {
141+
send_message(EnvManagerMessage::new(env));
142+
}
143+
fn report_environment(&mut self, env: PythonEnvironment) -> () {
144+
send_message(PythonEnvironmentMessage::new(env));
145+
}
146+
fn exit(&mut self) -> () {
147+
send_message(ExitMessage::new());
137148
}
138149
fn log_debug(&mut self, message: &str) -> () {
139-
self.send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
150+
send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
140151
}
141152
fn log_error(&mut self, message: &str) -> () {
142-
self.send_message(LogMessage::new(message.to_string(), LogLevel::Error));
153+
send_message(LogMessage::new(message.to_string(), LogLevel::Error));
143154
}
144155
fn log_info(&mut self, message: &str) -> () {
145-
self.send_message(LogMessage::new(message.to_string(), LogLevel::Info));
156+
send_message(LogMessage::new(message.to_string(), LogLevel::Info));
146157
}
147158
fn log_warning(&mut self, message: &str) -> () {
148-
self.send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
159+
send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
149160
}
150161
}
151162

native_locator/src/pyenv.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ pub fn find_and_report(
7373
let pyenv_dir = get_pyenv_dir(environment)?;
7474

7575
if let Some(pyenv_binary) = get_pyenv_binary(environment) {
76-
let params = messaging::EnvManager::new(vec![pyenv_binary], None);
77-
let message = messaging::EnvManagerMessage::new(params);
78-
dispatcher.send_message(message);
76+
dispatcher.report_environment_manager(messaging::EnvManager::new(vec![pyenv_binary], None));
7977
}
8078

8179
let versions_dir = PathBuf::from(&pyenv_dir)
@@ -95,7 +93,7 @@ pub fn find_and_report(
9593
if let Some(executable) = find_python_binary_path(&path) {
9694
let version = path.file_name().unwrap().to_string_lossy().to_string();
9795
let env_path = path.to_string_lossy().to_string();
98-
dispatcher.send_message(messaging::PythonEnvironment::new(
96+
dispatcher.report_environment(messaging::PythonEnvironment::new(
9997
"Python".to_string(),
10098
vec![executable.into_os_string().into_string().unwrap()],
10199
messaging::PythonEnvironmentCategory::Pyenv,

native_locator/src/windows_python.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::Path;
88

99
fn report_path_python(path: &str, dispatcher: &mut impl messaging::MessageDispatcher) {
1010
let version = utils::get_version(path);
11-
dispatcher.send_message(messaging::PythonEnvironment::new(
11+
dispatcher.report_environment(messaging::PythonEnvironment::new(
1212
"Python".to_string(),
1313
vec![path.to_string()],
1414
messaging::PythonEnvironmentCategory::WindowsStore,

native_locator/tests/common.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use std::{collections::HashMap, path::PathBuf};
55

6-
use python_finder::{known::Environment, messaging::MessageDispatcher};
6+
use python_finder::{known::Environment, messaging::{EnvManager, MessageDispatcher, PythonEnvironment}};
77
use serde_json::Value;
88

99
#[allow(dead_code)]
@@ -32,8 +32,14 @@ pub trait TestMessages {
3232
#[allow(dead_code)]
3333
pub fn create_test_dispatcher() -> TestDispatcher {
3434
impl MessageDispatcher for TestDispatcher {
35-
fn send_message<T: serde::Serialize>(&mut self, message: T) -> () {
36-
self.messages.push(serde_json::to_string(&message).unwrap());
35+
fn report_environment_manager(&mut self, env: EnvManager) -> () {
36+
self.messages.push(serde_json::to_string(&env).unwrap());
37+
}
38+
fn report_environment(&mut self, env: PythonEnvironment) -> () {
39+
self.messages.push(serde_json::to_string(&env).unwrap());
40+
}
41+
fn exit(&mut self) -> () {
42+
//
3743
}
3844
fn log_debug(&mut self, _message: &str) -> () {}
3945
fn log_error(&mut self, _message: &str) -> () {}

native_locator/tests/conda_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn find_conda_exe_and_empty_envs() {
4444
conda::find_and_report(&mut dispatcher, &known);
4545

4646
let conda_exe = join_test_paths(&[conda_dir.clone().as_str(), "conda"]);
47-
let expected_json = json!({"jsonrpc":"2.0","method":"envManager","params":{"executablePath":[conda_exe.clone()],"version":null}});
47+
let expected_json = json!({"executablePath":[conda_exe.clone()],"version":null});
4848
assert_messages(&[expected_json], &dispatcher)
4949
}
5050
#[test]
@@ -80,9 +80,9 @@ fn finds_two_conda_envs_from_txt() {
8080
let conda_1_exe = join_test_paths(&[conda_1.clone().as_str(), "python"]);
8181
let conda_2_exe = join_test_paths(&[conda_2.clone().as_str(), "python"]);
8282

83-
let expected_conda_env = json!({"jsonrpc":"2.0","method":"envManager","params":{"executablePath":[conda_exe.clone()],"version":null}});
84-
let expected_conda_1 = json!({"jsonrpc":"2.0","method":"pythonEnvironment","params":{"name":"envs/one","pythonExecutablePath":[conda_1_exe.clone()],"category":"conda","version":"10.0.1","activatedRun":[conda_exe.clone(),"run","-n","envs/one","python"],"envPath":conda_1.clone()}});
85-
let expected_conda_2 = json!({"jsonrpc":"2.0","method":"pythonEnvironment","params":{"name":"envs/two","pythonExecutablePath":[conda_2_exe.clone()],"category":"conda","version":null,"activatedRun":[conda_exe.clone(),"run","-n","envs/two","python"],"envPath":conda_2.clone()}});
83+
let expected_conda_env = json!({"executablePath":[conda_exe.clone()],"version":null});
84+
let expected_conda_1 = json!({"name":"envs/one","pythonExecutablePath":[conda_1_exe.clone()],"category":"conda","version":"10.0.1","activatedRun":[conda_exe.clone(),"run","-n","envs/one","python"],"envPath":conda_1.clone()});
85+
let expected_conda_2 = json!({"name":"envs/two","pythonExecutablePath":[conda_2_exe.clone()],"category":"conda","version":null,"activatedRun":[conda_exe.clone(),"run","-n","envs/two","python"],"envPath":conda_2.clone()});
8686
assert_messages(
8787
&[expected_conda_env, expected_conda_1, expected_conda_2],
8888
&dispatcher,

0 commit comments

Comments
 (0)