62 lines
2.1 KiB
Rust
62 lines
2.1 KiB
Rust
use std::sync::{Arc, Mutex};
|
|
|
|
use super::TrackerState;
|
|
use crate::ui::NormalizedBoxCoords;
|
|
|
|
pub fn process_incoming_string(
|
|
message: String,
|
|
identity_boxes: &Arc<Mutex<TrackerState>>, // This goes all the way back to the GUI thread for drawing boxes
|
|
) -> 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")?;
|
|
|
|
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),
|
|
});
|
|
}
|
|
|
|
// Replace the memory address in the mutex guard with that of the created vec above
|
|
if let Ok(mut ib) = identity_boxes.lock() {
|
|
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;
|
|
|
|
// Replace the memory address in the mutex guard with that of the created vec above
|
|
ib.identity_boxes = boxes;
|
|
}
|
|
|
|
Ok(())
|
|
}
|