Fix json output using join method
This commit is contained in:
@@ -232,9 +232,9 @@ fn main() {
|
|||||||
if let Some(output_json) = matches.get_one::<bool>("json") {
|
if let Some(output_json) = matches.get_one::<bool>("json") {
|
||||||
if *output_json {
|
if *output_json {
|
||||||
let properties = &device.get_device_state().device_properties;
|
let properties = &device.get_device_state().device_properties;
|
||||||
let mut headset_info_string = "{\n".to_string();
|
let mut headset_info_json = "{\n ".to_string();
|
||||||
|
|
||||||
let mut json_properties: Vec<String> = properties
|
let json_properties: Vec<String> = properties
|
||||||
.get_properties()
|
.get_properties()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|property| match property {
|
.map(|property| match property {
|
||||||
@@ -255,35 +255,17 @@ fn main() {
|
|||||||
property_descriptor,
|
property_descriptor,
|
||||||
) => match &property_descriptor.data {
|
) => match &property_descriptor.data {
|
||||||
Some(data) => {
|
Some(data) => {
|
||||||
format!(" \"{}\": \"{}\",\n", property_descriptor.name, data)
|
format!("\"{}\": \"{}\"", property_descriptor.name, data)
|
||||||
}
|
}
|
||||||
_ => "".to_string(),
|
_ => "".to_string(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// The last property needs to end without a comma
|
headset_info_json += &json_properties.join(",\n ");
|
||||||
match json_properties.last_mut() {
|
|
||||||
#[allow(unused_assignments)]
|
|
||||||
Some(mut json_property) => {
|
|
||||||
let mut last_property = json_property[0..(json_property.len() - 2)].to_string();
|
|
||||||
last_property += "\n";
|
|
||||||
json_property = &mut last_property;
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// Unreachable:
|
|
||||||
// The program exits if no device is found
|
|
||||||
// but also has a property for the device being connected
|
|
||||||
unreachable!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for json_property in json_properties {
|
headset_info_json += "\n}";
|
||||||
headset_info_string += &json_property;
|
println!("{}", headset_info_json);
|
||||||
}
|
|
||||||
|
|
||||||
headset_info_string += "}";
|
|
||||||
println!("{}", headset_info_string);
|
|
||||||
} else {
|
} else {
|
||||||
println!("{}", device.get_device_state().device_properties);
|
println!("{}", device.get_device_state().device_properties);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user