Skip to content

Commit 5022ae7

Browse files
committed
refactor: use jsonc-parser to parse jsonc
1 parent 0811486 commit 5022ae7

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ path = "src/sq.rs"
2525
[dependencies]
2626
serde = { version = "1.0", features = ["derive"] }
2727
serde_json = "1.0"
28-
jsonc-to-json="0.1.1"
28+
jsonc-parser={version = "0.26", features = ["serde"]}
2929
serde-xml-rs = "0.6"
3030
toml = "0.8"
3131
java-properties = "2"

src/runners/fleet.rs

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::collections::HashMap;
2-
use std::process::Output;
3-
use colored::Colorize;
4-
use serde::{Deserialize, Serialize};
1+
use crate::command_utils::{is_command_available, run_command_with_env_vars};
52
use crate::errors::KeeperError;
63
use crate::models::Task;
74
use crate::task;
5+
use colored::Colorize;
86
use error_stack::{report, Result};
9-
use jsonc_to_json::jsonc_to_json;
10-
use crate::command_utils::{is_command_available, run_command_with_env_vars};
7+
use jsonc_parser::parse_to_serde_value;
8+
use serde::{Deserialize, Serialize};
9+
use std::collections::HashMap;
10+
use std::process::Output;
1111

1212
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
1313
#[serde(rename_all = "camelCase")]
@@ -95,7 +95,9 @@ pub fn is_available() -> bool {
9595
}
9696

9797
pub fn list_tasks() -> Result<Vec<Task>, KeeperError> {
98-
Ok(parse_run_json().configurations.iter()
98+
Ok(parse_run_json()
99+
.configurations
100+
.iter()
99101
.map(|configuration| {
100102
let description = if &configuration.type_value == "command" {
101103
configuration.program.clone().unwrap()
@@ -111,14 +113,24 @@ fn parse_run_json() -> FleetRunJson {
111113
std::env::current_dir()
112114
.map(|dir| dir.join(".fleet").join("run.json"))
113115
.map(|path| std::fs::read_to_string(path).unwrap_or("{}".to_owned()))
114-
.map(|data| jsonc_to_json(&data).to_string())
115-
.map(|data| serde_json::from_str::<FleetRunJson>(&data).unwrap())
116+
.map(|data| {
117+
parse_to_serde_value(&data, &Default::default())
118+
.unwrap()
119+
.unwrap()
120+
})
121+
.map(|json_value| serde_json::from_value::<FleetRunJson>(json_value).unwrap())
116122
.unwrap()
117123
}
118124

119-
pub fn run_task(task_name: &str, _task_args: &[&str], _global_args: &[&str], verbose: bool) -> Result<Output, KeeperError> {
125+
pub fn run_task(
126+
task_name: &str,
127+
_task_args: &[&str],
128+
_global_args: &[&str],
129+
verbose: bool,
130+
) -> Result<Output, KeeperError> {
120131
let run_json = parse_run_json();
121-
let result = run_json.configurations
132+
let result = run_json
133+
.configurations
122134
.iter()
123135
.find(|configuration| configuration.formatted_name() == task_name);
124136
if let Some(configuration) = result {
@@ -133,9 +145,19 @@ fn run_configuration(configuration: &Configuration, verbose: bool) -> Result<Out
133145
let args = get_command_args(configuration);
134146
let args: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
135147
if is_command_available(&command_name) {
136-
Ok(run_command_with_env_vars(&command_name, &args, &configuration.working_dir, &configuration.environment, verbose).unwrap())
148+
Ok(run_command_with_env_vars(
149+
&command_name,
150+
&args,
151+
&configuration.working_dir,
152+
&configuration.environment,
153+
verbose,
154+
)
155+
.unwrap())
137156
} else {
138-
println!("{}", format!("{} is not available", command_name).bold().red());
157+
println!(
158+
"{}",
159+
format!("{} is not available", command_name).bold().red()
160+
);
139161
Err(report!(KeeperError::CommandNotFound(command_name)))
140162
}
141163
}
@@ -153,7 +175,10 @@ fn get_command_name(configuration: &Configuration) -> String {
153175
"node" => "node".to_owned(),
154176
"npm" => "npm".to_owned(),
155177
"php" => "php".to_owned(),
156-
"go" => configuration.go_exec_path.clone().unwrap_or("go".to_owned()),
178+
"go" => configuration
179+
.go_exec_path
180+
.clone()
181+
.unwrap_or("go".to_owned()),
157182
"command" => configuration.program.clone().unwrap_or_default(),
158183
_ => "".to_owned(),
159184
}
@@ -167,12 +192,24 @@ fn get_command_args(configuration: &Configuration) -> Vec<String> {
167192
"maven-run" => {
168193
if let Some(args) = &configuration.args {
169194
let args_text = args.join(" ");
170-
vec!["compile".to_owned(), "exec:java".to_owned(),
171-
format!("-Dexec.mainClass='{}'", configuration.main_class.clone().unwrap_or_default()),
172-
format!("-Dexec.args='{}'", args_text)]
195+
vec![
196+
"compile".to_owned(),
197+
"exec:java".to_owned(),
198+
format!(
199+
"-Dexec.mainClass='{}'",
200+
configuration.main_class.clone().unwrap_or_default()
201+
),
202+
format!("-Dexec.args='{}'", args_text),
203+
]
173204
} else {
174-
vec!["compile".to_owned(), "exec:java".to_owned(),
175-
format!("-Dexec.mainClass={}", configuration.main_class.clone().unwrap_or_default())]
205+
vec![
206+
"compile".to_owned(),
207+
"exec:java".to_owned(),
208+
format!(
209+
"-Dexec.mainClass={}",
210+
configuration.main_class.clone().unwrap_or_default()
211+
),
212+
]
176213
}
177214
}
178215
"docker-run" => {
@@ -183,7 +220,10 @@ fn get_command_args(configuration: &Configuration) -> Vec<String> {
183220
args.push(configuration.image_id_or_name.clone().unwrap_or_default());
184221
args
185222
} else {
186-
vec!["run".to_owned(), configuration.image_id_or_name.clone().unwrap_or_default()]
223+
vec![
224+
"run".to_owned(),
225+
configuration.image_id_or_name.clone().unwrap_or_default(),
226+
]
187227
}
188228
}
189229
"python" => configuration.python_full_args().clone(),
@@ -193,7 +233,11 @@ fn get_command_args(configuration: &Configuration) -> Vec<String> {
193233
args
194234
}
195235
"fastapi" => {
196-
let module_and_app = format!("{}{}", &configuration.module.clone().unwrap_or("".to_owned()), &configuration.application.clone().unwrap_or("".to_owned()));
236+
let module_and_app = format!(
237+
"{}{}",
238+
&configuration.module.clone().unwrap_or("".to_owned()),
239+
&configuration.application.clone().unwrap_or("".to_owned())
240+
);
197241
let mut args = vec!["-m".to_owned(), "unicorn".to_owned(), module_and_app];
198242
args.extend(configuration.arguments.clone().unwrap_or_default());
199243
args
@@ -208,13 +252,13 @@ fn get_command_args(configuration: &Configuration) -> Vec<String> {
208252
}
209253
"go" => configuration.build_params.clone().unwrap_or_default(),
210254
"node" => {
211-
let mut args = vec![];
212-
if let Some(file) = &configuration.file {
213-
args.push(file.clone());
214-
}
215-
args.extend(configuration.app_options.clone().unwrap_or_default());
216-
args
217-
},
255+
let mut args = vec![];
256+
if let Some(file) = &configuration.file {
257+
args.push(file.clone());
258+
}
259+
args.extend(configuration.app_options.clone().unwrap_or_default());
260+
args
261+
}
218262
"npm" => {
219263
let mut args = vec![];
220264
if let Some(command) = &configuration.command {
@@ -225,12 +269,11 @@ fn get_command_args(configuration: &Configuration) -> Vec<String> {
225269
args.push(scripts.clone());
226270
}
227271
args
228-
},
272+
}
229273
_ => vec![],
230274
}
231275
}
232276

233-
234277
#[cfg(test)]
235278
mod tests {
236279
use super::*;
@@ -250,7 +293,8 @@ mod tests {
250293

251294
#[test]
252295
fn test_run_configuration() {
253-
let configuration = Configuration::new_command("my-ip", "curl", &["https://httpbin.org/ip".to_owned()]);
296+
let configuration =
297+
Configuration::new_command("my-ip", "curl", &["https://httpbin.org/ip".to_owned()]);
254298
run_configuration(&configuration, true).unwrap();
255299
}
256300
}

src/runners/vstasks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::errors::KeeperError;
33
use crate::models::Task;
44
use crate::task;
55
use error_stack::Result;
6-
use jsonc_to_json::jsonc_to_json;
76
use serde::{Deserialize, Serialize};
87
use std::process::Output;
8+
use jsonc_parser::parse_to_serde_value;
99

1010
#[derive(Serialize, Deserialize, Debug, Default)]
1111
struct TasksJson {
@@ -57,8 +57,8 @@ fn parse_run_json() -> TasksJson {
5757
std::env::current_dir()
5858
.map(|dir| dir.join(".vscode").join("tasks.json"))
5959
.map(|path| std::fs::read_to_string(path).unwrap_or("{}".to_owned()))
60-
.map(|data| jsonc_to_json(&data).to_string())
61-
.map(|data| serde_json::from_str::<TasksJson>(&data).expect(".vscode/tasks.json format"))
60+
.map(|data| parse_to_serde_value(&data, &Default::default()).unwrap().unwrap())
61+
.map(|json_value| serde_json::from_value::<TasksJson>(json_value).expect(".vscode/tasks.json format"))
6262
.unwrap()
6363
}
6464

src/runners/zed.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::errors::KeeperError;
77
use crate::models::Task;
88
use crate::task;
99
use error_stack::{report, Result};
10-
use jsonc_to_json::jsonc_to_json;
10+
use jsonc_parser::parse_to_serde_value;
1111
use crate::command_utils::{is_command_available, run_command_with_env_vars};
1212

1313
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -62,8 +62,8 @@ fn parse_tasks_json() -> Vec<Configuration> {
6262
std::env::current_dir()
6363
.map(|dir| dir.join(".zed").join("tasks.json"))
6464
.map(|path| std::fs::read_to_string(path).unwrap_or("[]".to_owned()))
65-
.map(|data| jsonc_to_json(&data).to_string())
66-
.map(|data| serde_json::from_str::<Vec<Configuration>>(&data).unwrap())
65+
.map(|data| parse_to_serde_value(&data, &Default::default()).unwrap().unwrap())
66+
.map(|json_value| serde_json::from_value::<Vec<Configuration>>(json_value).unwrap())
6767
.unwrap()
6868
}
6969

0 commit comments

Comments
 (0)