From 88816705fb580b828fdb011aa6708eb67ab4892c Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 21 Apr 2024 23:03:39 +0300 Subject: [PATCH] Removed the anyhow crate and added the error mod instead --- Cargo.toml | 9 ++++----- examples/minecraft-async/Cargo.toml | 3 +-- examples/minecraft-async/src/main.rs | 6 +++--- examples/minecraft-sync/src/main.rs | 13 ++++++------- src/error.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 3 ++- src/sync.rs | 4 +--- 7 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 src/error.rs diff --git a/Cargo.toml b/Cargo.toml index 6bce41e..3bbcddc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,9 @@ authors = ["Wynd"] members = ["examples/*"] [dependencies] -serde = { version = "1.0.198", features = ["derive"] } -serde_json = { version = "1.0.116" } -reqwest = { version = "0.12.4", features = ["json", "blocking"] } -anyhow = { version = "1.0.82" } +serde = { version = "1", features = ["derive"] } +serde_json = { version = "1" } +reqwest = { version = "0.12", features = ["json", "blocking"] } [features] -sync = [] +sync = [] \ No newline at end of file diff --git a/examples/minecraft-async/Cargo.toml b/examples/minecraft-async/Cargo.toml index ccba2e4..b375526 100644 --- a/examples/minecraft-async/Cargo.toml +++ b/examples/minecraft-async/Cargo.toml @@ -6,5 +6,4 @@ authors = ["Wynd"] [dependencies] playerdb-rs = { path = "../../"} -tokio = { version = "1.37", features = ["full"] } -anyhow = { version = "1" } +tokio = { version = "1.37", features = ["full"] } \ No newline at end of file diff --git a/examples/minecraft-async/src/main.rs b/examples/minecraft-async/src/main.rs index b67d4ee..abd56a6 100644 --- a/examples/minecraft-async/src/main.rs +++ b/examples/minecraft-async/src/main.rs @@ -1,12 +1,12 @@ -use playerdb_rs::PlayerDBApi; +use playerdb_rs::{error::Result, PlayerDBApi}; #[tokio::main] -async fn main() -> anyhow::Result<()> { +async fn main() -> Result<()> { let api = PlayerDBApi::default(); let res = api.get_minecraft_player("notch").await?; - print!("{:#?}", res); + println!("{res:#?}"); Ok(()) } diff --git a/examples/minecraft-sync/src/main.rs b/examples/minecraft-sync/src/main.rs index 2ba0f01..1ebec9a 100644 --- a/examples/minecraft-sync/src/main.rs +++ b/examples/minecraft-sync/src/main.rs @@ -1,12 +1,11 @@ -use playerdb_rs::sync::PlayerDBApi; +use playerdb_rs::{error::Result, sync::PlayerDBApi}; -fn main() { +fn main() -> Result<()> { let api = PlayerDBApi::default(); - let res = api.get_minecraft_player("notch"); + let res = api.get_minecraft_player("notch")?; - match res { - Ok(x) => println!("{:#?}", x), - Err(e) => eprintln!("{e}"), - } + println!("{res:#?}"); + + Ok(()) } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..4fa8d9f --- /dev/null +++ b/src/error.rs @@ -0,0 +1,28 @@ +pub type Result = std::result::Result; + +#[derive(Debug)] +pub enum Error { + Other(String), + + Reqwest(reqwest::Error), +} + +impl std::error::Error for Error {} + +impl std::fmt::Display for Error { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { + write!(fmt, "{self:?}") + } +} + +impl From<&str> for Error { + fn from(val: &str) -> Self { + Self::Other(val.to_string()) + } +} + +impl From for Error { + fn from(err: reqwest::Error) -> Self { + Self::Reqwest(err) + } +} diff --git a/src/lib.rs b/src/lib.rs index 17ebc3b..7b459ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ use std::sync::Arc; -use anyhow::Result; +use crate::error::Result; +pub mod error; pub mod minecraft; #[cfg(feature = "sync")] pub mod sync; diff --git a/src/sync.rs b/src/sync.rs index fe7c41a..1a06dc5 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,8 +1,6 @@ use std::sync::Arc; -use anyhow::Result; - -use crate::{minecraft, APP_USER_AGENT, BASE_URI}; +use crate::{error::Result, minecraft, APP_USER_AGENT, BASE_URI}; pub struct PlayerDBApi { agent: Arc,