Skip to content

Commit

Permalink
1.2.6 use Result Error handling + send the deserializing error as the…
Browse files Browse the repository at this point in the history
… function return
  • Loading branch information
QuentinGruber committed Nov 25, 2023
1 parent c2b8019 commit 02a1170
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "h1emu-core"
version = "1.2.5"
version = "1.2.6"
edition = "2021"
description = "Utility library used in h1emu."
license = "BSD-3-Clause"
Expand Down
10 changes: 1 addition & 9 deletions src/gatewayprotocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use wasm_bindgen::prelude::*;

use super::gatewayprotocol_packets_structs::*;
use super::lib_utils::read_prefixed_string_le;
use super::protocol_errors::gen_deserializing_error_json;

#[wasm_bindgen]
pub enum GatewayChannels {
Expand Down Expand Up @@ -63,14 +62,10 @@ impl GatewayProtocol {
ticket,
client_protocol,
client_build,
error: None,
})
}
pub fn pack_login_reply_packet(&mut self, logged_in: bool) -> Vec<u8> {
self.pack_login_reply_object(LoginReplyPacket {
logged_in,
error: None,
})
self.pack_login_reply_object(LoginReplyPacket { logged_in })
}
pub fn pack_tunnel_data_packet_for_client(&mut self, data: Vec<u8>, channel: u8) -> Vec<u8> {
self._pack_tunnel_data_packet(0x05, data, channel)
Expand Down Expand Up @@ -154,9 +149,6 @@ impl GatewayProtocol {
}

pub fn pack_login_request_object(&mut self, packet: LoginRequestPacket) -> Vec<u8> {
if packet.error.is_some() {
return gen_deserializing_error_json();
}
self.wtr.clear();
self.wtr.write_u8(0x01).unwrap_or_default();
self.wtr
Expand Down
6 changes: 0 additions & 6 deletions src/gatewayprotocol_packets_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ pub struct LoginRequestPacket {
pub ticket: String,
pub client_protocol: String,
pub client_build: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<bool>, // used internnaly to identify deserialization errors
}

#[derive(Serialize, Deserialize, Clone)]
pub struct LoginReplyPacket {
pub logged_in: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<bool>, // used internnaly to identify deserialization errors
}

#[derive(Serialize, Deserialize)]
pub struct ChannelIsRoutablePacket {
pub is_routable: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<bool>, // used internnaly to identify deserialization errors
}

#[derive(Serialize)]
Expand Down
21 changes: 13 additions & 8 deletions src/protocol_errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io::Cursor;

use wasm_bindgen::prelude::wasm_bindgen;

pub fn gen_size_error_json(rdr: Cursor<&std::vec::Vec<u8>>) -> String {
format!(
r#"{{"name":"Error","error":"size","size":{},"raw":{:?}}}"#,
Expand Down Expand Up @@ -29,12 +31,15 @@ pub fn gen_corruption_error_json(
)
}

pub fn gen_deserializing_error_json() -> Vec<u8> {
vec![] // maybe encoding a null string with error log would be better
/* return json!({
"name": "Error",
"error": "deserializing",
"raw": packet
})
.to_string();*/
#[wasm_bindgen]
pub enum ErrorType {
Deserializing = 0x99,
}

pub fn gen_deserializing_error_json(error: serde_json::Error) -> Vec<u8> {
let error_string = format!("{}", error);
// create a vec starting with the error type and then the error stringify
let mut error_vec: Vec<u8> = vec![0x00, ErrorType::Deserializing as u8];
error_vec.append(&mut error_string.as_bytes().to_vec());
error_vec
}
Loading

0 comments on commit 02a1170

Please sign in to comment.