Reverter errors from taking a &'static str to String

This commit is contained in:
CosminPerRam 2022-11-25 18:42:10 +02:00
parent dc0926bab7
commit 7b44c5f7eb
10 changed files with 59 additions and 59 deletions

View file

@ -73,33 +73,33 @@ impl Java {
let mut pos = 0;
if get_varint(&buf, &mut pos)? != 0 { //first var int is the packet id
return Err(GDError::PacketBad("Bad receive packet id."));
return Err(GDError::PacketBad("Bad receive packet id.".to_string()));
}
let json_response = get_string(&buf, &mut pos)?;
let value_response: Value = serde_json::from_str(&json_response)
.map_err(|e| GDError::JsonParse(e.to_string().as_str()))?;
.map_err(|e| GDError::JsonParse(e.to_string()))?;
let version_name = value_response["version"]["name"].as_str()
.ok_or(GDError::PacketBad("Couldn't get expected string."))?.to_string();
.ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string();
let version_protocol = value_response["version"]["protocol"].as_i64()
.ok_or(GDError::PacketBad("Couldn't get expected number."))? as i32;
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as i32;
let max_players = value_response["players"]["max"].as_u64()
.ok_or(GDError::PacketBad("Couldn't get expected number."))? as u32;
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
let online_players = value_response["players"]["online"].as_u64()
.ok_or(GDError::PacketBad("Couldn't get expected number."))? as u32;
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
let sample_players: Option<Vec<Player>> = match value_response["players"]["sample"].is_null() {
true => None,
false => Some({
let players_values = value_response["players"]["sample"].as_array()
.ok_or(GDError::PacketBad("Couldn't get expected array."))?;
.ok_or(GDError::PacketBad("Couldn't get expected array.".to_string()))?;
let mut players = Vec::with_capacity(players_values.len());
for player in players_values {
players.push(Player {
name: player["name"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string."))?.to_string(),
id: player["id"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string."))?.to_string()
name: player["name"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string(),
id: player["id"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string()
})
}

View file

@ -31,7 +31,7 @@ impl LegacyBV1_8 {
let mut pos = 0;
if get_u8(&buf, &mut pos)? != 0xFF {
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet."));
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet.".to_string()));
}
let length = get_u16_be(&buf, &mut pos)? * 2;
@ -44,9 +44,9 @@ impl LegacyBV1_8 {
let description = split[0].to_string();
let online_players = split[1].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
let max_players = split[2].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
Ok(Response {
version_name: "Beta 1.8+".to_string(),

View file

@ -32,7 +32,7 @@ impl LegacyV1_4 {
let mut pos = 0;
if get_u8(&buf, &mut pos)? != 0xFF {
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet."));
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet.".to_string()));
}
let length = get_u16_be(&buf, &mut pos)? * 2;
@ -49,9 +49,9 @@ impl LegacyV1_4 {
let description = split[0].to_string();
let online_players = split[1].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
let max_players = split[2].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
Ok(Response {
version_name: "1.4+".to_string(),

View file

@ -52,13 +52,13 @@ impl LegacyV1_6 {
error_by_expected_size(5, split.len())?;
let version_protocol = split[0].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
let version_name = split[1].to_string();
let description = split[2].to_string();
let max_players = split[3].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
let online_players = split[4].parse()
.map_err(|_| GDError::PacketBad("Failed to parse to expected int."))?;
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
Ok(Response {
version_name,
@ -81,14 +81,14 @@ impl LegacyV1_6 {
let mut pos = 0;
if get_u8(&buf, &mut pos)? != 0xFF {
return Err(GDError::ProtocolFormat("Expected a certain byte (0xFF) at the begin of the packet."));
return Err(GDError::ProtocolFormat("Expected a certain byte (0xFF) at the begin of the packet.".to_string()));
}
let length = get_u16_be(&buf, &mut pos)? * 2;
error_by_expected_size((length + 3) as usize, buf.len())?;
if !LegacyV1_6::is_protocol(&buf, &mut pos)? {
return Err(GDError::ProtocolFormat("Expected certain bytes at the beginning of the packet."));
return Err(GDError::ProtocolFormat("Expected certain bytes at the beginning of the packet.".to_string()));
}
LegacyV1_6::get_response(&buf, &mut pos)

View file

@ -29,7 +29,7 @@ pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>
return Ok(response);
}
Err(GDError::AutoQuery("No protocol returned a response.".to_string()))
Err(GDError::AutoQuery)
}
/// Queries a specific Minecraft Server type.

View file

@ -96,7 +96,7 @@ pub fn get_varint(buf: &[u8], pos: &mut usize) -> GDResult<i32> {
// The 5th byte is only allowed to have the 4 smallest bits set
if i == 4 && (current_byte & 0xf0 != 0) {
return Err(GDError::PacketBad("VarInt Overflow".to_string()))
return Err(GDError::PacketBad("Couldn't parse to VarInt: Overflow.".to_string()))
}
if (current_byte & msb) == 0 {
@ -140,7 +140,7 @@ pub fn get_string(buf: &[u8], pos: &mut usize) -> GDResult<String> {
}
Ok(String::from_utf8(text)
.map_err(|_| GDError::PacketBad("Minecraft bad String".to_string()))?)
.map_err(|_| GDError::PacketBad("Couldn't parse to a Minecraft String.".to_string()))?)
}
pub fn as_string(value: String) -> Vec<u8> {

View file

@ -13,13 +13,13 @@ impl TimeoutSettings {
pub fn new(read: Option<Duration>, write: Option<Duration>) -> GDResult<Self> {
if let Some(read_duration) = read {
if read_duration == Duration::new(0, 0) {
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings".to_string()))
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings.".to_string()))
}
}
if let Some(write_duration) = write {
if write_duration == Duration::new(0, 0) {
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings".to_string()))
return Err(GDError::InvalidInput("Can't pass duration 0 to timeout settings.".to_string()))
}
}

View file

@ -117,18 +117,18 @@ impl SplitPacket {
fn get_payload(&self) -> GDResult<Vec<u8>> {
if self.compressed {
let mut decoder = Decoder::new();
decoder.write(&self.payload).map_err(|e| GDError::Decompress(e.to_string().as_str()))?;
decoder.write(&self.payload).map_err(|e| GDError::Decompress(e.to_string()))?;
let decompressed_size = self.decompressed_size.unwrap() as usize;
let mut decompressed_payload = Vec::with_capacity(decompressed_size);
decoder.read(&mut decompressed_payload).map_err(|e| GDError::Decompress(e.to_string().as_str()))?;
decoder.read(&mut decompressed_payload).map_err(|e| GDError::Decompress(e.to_string()))?;
if decompressed_payload.len() != decompressed_size {
Err(GDError::Decompress("The decompressed payload size doesn't match the expected one."))
Err(GDError::Decompress("The decompressed payload size doesn't match the expected one.".to_string()))
}
else if crc32fast::hash(&decompressed_payload) != self.uncompressed_crc32.unwrap() {
Err(GDError::Decompress("The decompressed crc32 hash does not match the expected one."))
Err(GDError::Decompress("The decompressed crc32 hash does not match the expected one.".to_string()))
}
else {
Ok(decompressed_payload)
@ -420,7 +420,7 @@ fn get_response(address: &str, port: u16, app: App, gather_settings: GatheringSe
if let App::Source(x) = &app {
if let Some(appid) = x {
if *appid != info.appid {
return Err(GDError::BadGame(format!("Expected {}, found {} instead!", *appid, info.appid).as_str()));
return Err(GDError::BadGame(format!("Expected {}, found {} instead!", *appid, info.appid)));
}
}
}