59 lines
1.9 KiB
Rust
59 lines
1.9 KiB
Rust
|
|
||
|
use std::sync::{Arc, Mutex};
|
||
|
|
||
|
use crate:: ui::NormalizedBoxCoords;
|
||
|
|
||
|
|
||
|
pub fn process_incoming_string(
|
||
|
message: String,
|
||
|
identity_boxes: &Arc<Mutex<Vec<NormalizedBoxCoords>>>, // 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() {
|
||
|
// Replace the memory address in the mutex guard with that of the created vec above
|
||
|
*ib = boxes;
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|