Removed the anyhow crate and added the error mod instead

master
Wynd 2024-04-21 23:03:39 +03:00
parent 25863f9e46
commit 88816705fb
7 changed files with 45 additions and 21 deletions

View File

@ -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 = []

View File

@ -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"] }

View File

@ -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(())
}

View File

@ -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(())
}

28
src/error.rs 100644
View File

@ -0,0 +1,28 @@
pub type Result<T> = std::result::Result<T, Error>;
#[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<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Self::Reqwest(err)
}
}

View File

@ -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;

View File

@ -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<reqwest::blocking::Client>,