From bf8f533d9f75dc6a1e218b5c576fe9a1275b17ec Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 27 Aug 2023 01:27:45 -0500 Subject: use thiserror for azalea_nbt::Error --- Cargo.lock | 9 +++++---- azalea-nbt/Cargo.toml | 1 + azalea-nbt/src/error.rs | 36 ++++++++++-------------------------- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a6d3bd3..9173f94e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -416,6 +416,7 @@ dependencies = [ "graphite_binary", "log", "serde", + "thiserror", "valence_nbt", ] @@ -2621,18 +2622,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.46" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9207952ae1a003f42d3d5e892dac3c6ba42aa6ac0c79a6a91a2b5cb4253e75c" +checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.46" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1728216d3244de4f14f14f8c15c79be1a7c67867d28d69b719690e2a19fb445" +checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" dependencies = [ "proc-macro2", "quote", diff --git a/azalea-nbt/Cargo.toml b/azalea-nbt/Cargo.toml index 393350a4..ca0dcb27 100644 --- a/azalea-nbt/Cargo.toml +++ b/azalea-nbt/Cargo.toml @@ -16,6 +16,7 @@ enum-as-inner = "0.6.0" flate2 = "^1.0.27" log = "0.4.20" serde = { version = "^1.0", features = ["derive"], optional = true } +thiserror = "1.0.47" [dev-dependencies] criterion = { version = "^0.5.1", features = ["html_reports"] } diff --git a/azalea-nbt/src/error.rs b/azalea-nbt/src/error.rs index d3c5ff44..ace7fcd3 100755 --- a/azalea-nbt/src/error.rs +++ b/azalea-nbt/src/error.rs @@ -1,31 +1,15 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Debug, Error)] pub enum Error { + #[error("Invalid tag type: {0}")] InvalidTagType(u8), + #[error("Invalid tag")] InvalidTag, - WriteError(std::io::Error), - Utf8Error(std::str::Utf8Error), + #[error("Write error: {0}")] + WriteError(#[from] std::io::Error), + #[error("Utf8 error: {0}")] + Utf8Error(#[from] std::str::Utf8Error), + #[error("Unexpected EOF")] UnexpectedEof, } - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - Error::InvalidTagType(id) => write!(f, "Invalid tag type: {id}"), - Error::InvalidTag => write!(f, "Invalid tag"), - Error::WriteError(e) => write!(f, "Write error: {e}"), - Error::Utf8Error(e) => write!(f, "Utf8 error: {e}"), - Error::UnexpectedEof => write!(f, "Unexpected EOF"), - } - } -} - -impl From for Error { - fn from(e: std::io::Error) -> Self { - Error::WriteError(e) - } -} -impl From for Error { - fn from(e: std::str::Utf8Error) -> Self { - Error::Utf8Error(e) - } -} -- cgit v1.2.3