[backup] games from server with images

This commit is contained in:
2024-11-13 23:51:28 +01:00
parent 5d45c4ce4b
commit a6ed6e04fe
13 changed files with 214 additions and 113 deletions

View File

@ -7,7 +7,7 @@ use tracing::error;
pub enum Request {
ListGames,
GetGame { id: String },
Invalid(Vec<u8>, String),
Invalid(Bytes, String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -15,29 +15,29 @@ pub enum Response {
Games(Vec<Game>),
Game(Game),
GameNotFound(String),
InvalidRequest(Vec<u8>, String),
InvalidRequest(Bytes, String),
EncodingError(String),
DecodingError(Vec<u8>, String),
DecodingError(Bytes, String),
}
// Add Message trait
pub trait Message {
fn decode(bytes: &[u8]) -> Self;
fn decode(bytes: Bytes) -> Self;
fn encode(&self) -> Bytes;
}
// Implement for Request
impl Message for Request {
fn decode(bytes: &[u8]) -> Self {
match serde_json::from_slice(bytes) {
fn decode(bytes: Bytes) -> Self {
match serde_json::from_slice(&bytes) {
Ok(t) => t,
Err(e) => {
tracing::error!(
"got invalid request from client (error: {}): {}",
e,
String::from_utf8_lossy(bytes)
String::from_utf8_lossy(&bytes)
);
Request::Invalid(bytes.into(), e.to_string())
Request::Invalid(bytes, e.to_string())
}
}
}
@ -55,10 +55,10 @@ impl Message for Request {
// Implement for Response
impl Message for Response {
fn decode(bytes: &[u8]) -> Self {
match serde_json::from_slice(bytes) {
fn decode(bytes: Bytes) -> Self {
match serde_json::from_slice(&bytes) {
Ok(t) => t,
Err(e) => Response::DecodingError(bytes.into(), e.to_string()),
Err(e) => Response::DecodingError(bytes, e.to_string()),
}
}