2023-07-21 16:55:49 +00:00
|
|
|
use std::{fs::File, io, path::Path};
|
2023-07-21 15:27:44 +00:00
|
|
|
|
2023-07-21 16:55:49 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-07-21 15:27:44 +00:00
|
|
|
use serde_json::from_reader;
|
|
|
|
|
2023-07-21 16:55:49 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2023-07-21 15:27:44 +00:00
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum SocketEvent {
|
|
|
|
/// Wait for input data that matches this.
|
2023-07-21 16:55:49 +00:00
|
|
|
Receive { expected: Vec<u8> },
|
2023-07-21 15:27:44 +00:00
|
|
|
/// Send data to client.
|
2023-07-21 16:55:49 +00:00
|
|
|
Send { payload: Vec<u8> },
|
2023-07-21 15:27:44 +00:00
|
|
|
/// Expect client to disconnect.
|
|
|
|
WaitForDisconnect,
|
|
|
|
/// Disconnect the client.
|
|
|
|
Disconnect,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SocketEvent {
|
|
|
|
pub fn from_file<P>(path: P) -> Result<Vec<Self>, io::Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
let file = File::open(path)?;
|
|
|
|
|
|
|
|
Ok(from_reader(file)?)
|
|
|
|
}
|
2023-07-21 16:55:49 +00:00
|
|
|
}
|