vcs-controller/src/remote_sources/process_box_string.rs

63 lines
2.1 KiB
Rust
Raw Normal View History

2024-04-27 19:33:18 -07:00
use std::sync::{Arc, Mutex};
2024-05-04 15:09:31 -07:00
use crate::ui::NormalizedBoxCoords;
use super::TrackerState;
2024-04-27 19:33:18 -07:00
pub fn process_incoming_string(
message: String,
2024-05-04 15:09:31 -07:00
identity_boxes: &Arc<Mutex<TrackerState>>, // This goes all the way back to the GUI thread for drawing boxes
2024-04-27 19:33:18 -07:00
) -> core::result::Result<(), String> {
let mut boxes: Vec<NormalizedBoxCoords> = Vec::new();
for line in message.lines() {
let parts: Vec<&str> = line.split(' ').collect();
let id = parts[0]
.replace(['[', ']'], "")
.parse()
.map_err(|_| "Invalid ID")?;
if parts.len() != 3 {
return Err("Invalid socket input format: number of parts".to_string());
}
let coords: Vec<&str> = parts[1].split(':').collect();
if coords.len() != 2 {
return Err("Invalid socket input format: coords 1".to_string());
}
let x1: u32 = coords[0].parse().map_err(|_| "Invalid x coordinate")?;
let y1: u32 = coords[1].parse().map_err(|_| "Invalid y coordinate")?;
let coords2: Vec<&str> = parts[2].split(':').collect();
if coords2.len() != 2 {
return Err("Invalid socket input format: coords 2".to_string());
}
let x2: u32 = coords2[0].parse().map_err(|_| "Invalid width")?;
let y2: u32 = coords2[1].parse().map_err(|_| "Invalid width")?;
2024-05-04 15:09:31 -07:00
boxes.push(NormalizedBoxCoords {
id,
x1: (x1 as f32 / 1000.0),
x2: (x2 as f32 / 1000.0),
y1: (y1 as f32 / 1000.0),
y2: (y2 as f32 / 1000.0),
});
2024-04-27 19:33:18 -07:00
}
// Replace the memory address in the mutex guard with that of the created vec above
if let Ok(mut ib) = identity_boxes.lock() {
2024-05-04 15:09:31 -07:00
let mut old_ids: Vec<u32> = ib.identity_boxes.iter().map(|x| x.id).collect();
old_ids.sort();
let mut new_ids: Vec<u32> = boxes.iter().map(|x| x.id).collect();
new_ids.sort();
ib.update_ids = new_ids == old_ids;
2024-04-27 19:33:18 -07:00
// Replace the memory address in the mutex guard with that of the created vec above
2024-05-04 15:09:31 -07:00
ib.identity_boxes = boxes;
2024-04-27 19:33:18 -07:00
}
Ok(())
2024-05-04 15:09:31 -07:00
}