aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-08-06 07:22:19 +0000
committerGitHub <noreply@github.com>2022-08-06 02:22:19 -0500
commit5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch)
treeb006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-core/src
parent1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff)
downloadazalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz
Better errors (#14)
* make reading use thiserror * finish implementing all the error things * clippy warnings related to ok_or * fix some errors in other places * thiserror in more places * don't use closures in a couple places * errors in writing packet * rip backtraces * change some BufReadError::Custom to UnexpectedEnumVariant * Errors say what packet is bad * error on leftover data and fix it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-core/src')
-rwxr-xr-xazalea-core/src/difficulty.rs4
-rwxr-xr-xazalea-core/src/game_type.rs33
-rw-r--r--azalea-core/src/particle/mod.rs8
-rw-r--r--azalea-core/src/position.rs8
-rwxr-xr-xazalea-core/src/resource_location.rs6
-rw-r--r--azalea-core/src/slot.rs4
6 files changed, 34 insertions, 29 deletions
diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs
index f2877f79..65950e8d 100755
--- a/azalea-core/src/difficulty.rs
+++ b/azalea-core/src/difficulty.rs
@@ -3,7 +3,7 @@ use std::{
io::{Read, Write},
};
-use azalea_buf::{McBufReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
#[derive(Hash, Clone, Debug, PartialEq, Eq)]
pub enum Difficulty {
@@ -67,7 +67,7 @@ impl Difficulty {
}
impl McBufReadable for Difficulty {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
Ok(Difficulty::by_id(u8::read_from(buf)?))
}
}
diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs
index d41e5b84..429af324 100755
--- a/azalea-core/src/game_type.rs
+++ b/azalea-core/src/game_type.rs
@@ -1,4 +1,4 @@
-use azalea_buf::{McBufReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Hash, Copy, Clone, Debug)]
@@ -27,22 +27,24 @@ impl GameType {
}
}
- pub fn from_id(id: u8) -> Result<GameType, String> {
- Ok(match id {
+ pub fn from_id(id: u8) -> Option<GameType> {
+ Some(match id {
0 => GameType::SURVIVAL,
1 => GameType::CREATIVE,
2 => GameType::ADVENTURE,
3 => GameType::SPECTATOR,
- _ => return Err(format!("Unknown game type id: {}", id)),
+ _ => return None,
})
}
- pub fn from_optional_id(id: i8) -> Result<OptionalGameType, String> {
- Ok(match id {
- -1 => None,
- id => Some(GameType::from_id(id as u8)?),
- }
- .into())
+ pub fn from_optional_id(id: i8) -> Option<OptionalGameType> {
+ Some(
+ match id {
+ -1 => None,
+ id => Some(GameType::from_id(id as u8)?),
+ }
+ .into(),
+ )
}
pub fn short_name(&self) -> &'static str {
@@ -77,8 +79,9 @@ impl GameType {
}
impl McBufReadable for GameType {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
- GameType::from_id(u8::read_from(buf)?)
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
+ let id = u8::read_from(buf)?;
+ GameType::from_id(id).ok_or_else(|| BufReadError::UnexpectedEnumVariant { id: id as i32 })
}
}
@@ -105,8 +108,10 @@ impl From<OptionalGameType> for Option<GameType> {
}
impl McBufReadable for OptionalGameType {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
- GameType::from_optional_id(i8::read_from(buf)?)
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
+ let id = i8::read_from(buf)?;
+ GameType::from_optional_id(id)
+ .ok_or_else(|| BufReadError::UnexpectedEnumVariant { id: id as i32 })
}
}
diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs
index 46f88407..c2fa3337 100644
--- a/azalea-core/src/particle/mod.rs
+++ b/azalea-core/src/particle/mod.rs
@@ -1,5 +1,5 @@
use crate::{BlockPos, Slot};
-use azalea_buf::{McBuf, McBufReadable, McBufVarReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufVarReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Debug, Clone, McBuf)]
@@ -153,7 +153,7 @@ pub struct VibrationParticle {
}
impl ParticleData {
- pub fn read_from_particle_id(buf: &mut impl Read, id: u32) -> Result<Self, String> {
+ pub fn read_from_particle_id(buf: &mut impl Read, id: u32) -> Result<Self, BufReadError> {
Ok(match id {
0 => ParticleData::AmbientEntityEffect,
1 => ParticleData::AngryVillager,
@@ -243,13 +243,13 @@ impl ParticleData {
85 => ParticleData::WaxOff,
86 => ParticleData::ElectricSpark,
87 => ParticleData::Scrape,
- _ => return Err(format!("Unknown particle id: {}", id)),
+ _ => return Err(BufReadError::UnexpectedEnumVariant { id: id as i32 }),
})
}
}
impl McBufReadable for ParticleData {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let id = u32::var_read_from(buf)?;
ParticleData::read_from_particle_id(buf, id)
}
diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs
index adc5cafa..7371d530 100644
--- a/azalea-core/src/position.rs
+++ b/azalea-core/src/position.rs
@@ -1,5 +1,5 @@
use crate::ResourceLocation;
-use azalea_buf::{McBufReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use std::{
io::{Read, Write},
ops::Rem,
@@ -225,7 +225,7 @@ impl From<&EntityPos> for ChunkPos {
}
impl McBufReadable for BlockPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let val = u64::read_from(buf)?;
let x = (val >> 38) as i32;
let y = (val & 0xFFF) as i32;
@@ -235,7 +235,7 @@ impl McBufReadable for BlockPos {
}
impl McBufReadable for GlobalPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
Ok(GlobalPos {
dimension: ResourceLocation::read_from(buf)?,
pos: BlockPos::read_from(buf)?,
@@ -244,7 +244,7 @@ impl McBufReadable for GlobalPos {
}
impl McBufReadable for ChunkSectionPos {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let long = i64::read_from(buf)?;
Ok(ChunkSectionPos {
x: (long >> 42) as i32,
diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs
index 945b6d80..4c0764e5 100755
--- a/azalea-core/src/resource_location.rs
+++ b/azalea-core/src/resource_location.rs
@@ -1,6 +1,6 @@
//! A resource, like minecraft:stone
-use azalea_buf::{McBufReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Hash, Clone, PartialEq, Eq)]
@@ -13,7 +13,7 @@ static DEFAULT_NAMESPACE: &str = "minecraft";
// static REALMS_NAMESPACE: &str = "realms";
impl ResourceLocation {
- pub fn new(resource_string: &str) -> Result<ResourceLocation, String> {
+ pub fn new(resource_string: &str) -> Result<ResourceLocation, BufReadError> {
let sep_byte_position_option = resource_string.chars().position(|c| c == ':');
let (namespace, path) = if let Some(sep_byte_position) = sep_byte_position_option {
if sep_byte_position == 0 {
@@ -46,7 +46,7 @@ impl std::fmt::Debug for ResourceLocation {
}
impl McBufReadable for ResourceLocation {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let location_string = String::read_from(buf)?;
ResourceLocation::new(&location_string)
}
diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs
index 8b1f784d..38abcf61 100644
--- a/azalea-core/src/slot.rs
+++ b/azalea-core/src/slot.rs
@@ -1,6 +1,6 @@
// TODO: have an azalea-inventory or azalea-container crate and put this there
-use azalea_buf::{McBuf, McBufReadable, McBufWritable};
+use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
use std::io::{Read, Write};
#[derive(Debug, Clone)]
@@ -18,7 +18,7 @@ pub struct SlotData {
}
impl McBufReadable for Slot {
- fn read_from(buf: &mut impl Read) -> Result<Self, String> {
+ fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
let present = bool::read_from(buf)?;
if !present {
return Ok(Slot::Empty);