From 9f4e7a8be266efbbffb068e7dad85e28f7fe724b Mon Sep 17 00:00:00 2001 From: AndrielFR Date: Thu, 2 Jan 2025 11:29:20 -0300 Subject: [PATCH] fix: docs tests - use thiserror to make custom errors --- Cargo.toml | 1 + src/client.rs | 45 +++++++++++++++++++++++++-------------------- src/error.rs | 38 ++++++++++++-------------------------- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f655574..5948e32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ edition = "2021" tokio = { version = "1", features = ["macros", "rt-multi-thread"] } serde = { version = "1", features = ["derive"] } reqwest = "*" +thiserror = "2" serde_json = "1" diff --git a/src/client.rs b/src/client.rs index e2a36bc..9864f13 100644 --- a/src/client.rs +++ b/src/client.rs @@ -40,11 +40,12 @@ impl Client { /// /// # Example /// - /// ```no_run - /// use anilist::Client; + /// ``` + /// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> { + /// let anime = client.get_anime(Some(1), None).await?; /// - /// let client = Client::default(); - /// let anime = client.get_anime(Some(1), None).await.unwrap(); + /// # Ok(()) + /// # } /// ``` pub async fn get_anime( &self, @@ -83,11 +84,12 @@ impl Client { /// /// # Example /// - /// ```no_run - /// use anilist::Client; + /// ``` + /// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> { + /// let manga = client.get_manga(Some(1), None).await?; /// - /// let client = Client::default(); - /// let manga = client.get_manga(Some(1), None).await.unwrap(); + /// # Ok(()) + /// # } /// ``` pub async fn get_manga( &self, @@ -125,11 +127,12 @@ impl Client { /// /// # Example /// - /// ```no_run - /// use anilist::Client; + /// ``` + /// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> { + /// let character = client.get_character(1).await?; /// - /// let client = Client::default(); - /// let character = client.get_character(1).await.unwrap(); + /// # Ok(()) + /// # } /// ``` pub async fn get_character(&self, id: i64) -> Result { let data = self @@ -163,11 +166,12 @@ impl Client { /// /// # Example /// - /// ```no_run - /// use anilist::Client; + /// ``` + /// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> { + /// let character = client.get_char(1).await?; /// - /// let client = Client::default(); - /// let character = client.get_char(1).await.unwrap(); + /// # Ok(()) + /// # } /// ``` pub async fn get_char(&self, id: i64) -> Result { self.get_character(id).await @@ -185,11 +189,12 @@ impl Client { /// /// # Example /// - /// ```no_run - /// use anilist::Client; + /// ``` + /// # async fn f(client: rust_anilist::Client) -> rust_anilist::Result<()> { + /// let person = client.get_person(1).await?; /// - /// let client = Client::default(); - /// let person = client.get_person(1).await.unwrap(); + /// # Ok(()) + /// # } /// ``` pub async fn get_person(&self, id: i64) -> Result { let data = self diff --git a/src/error.rs b/src/error.rs index 141579a..780e649 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,36 +1,22 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2022-2025 Andriel Ferreira -use std::error::Error as StdError; -use std::fmt; +use thiserror::Error as TError; +/// A specialized `Result` type for operations that can return an `Error`. +/// +/// This is defined as a convenience to avoid writing out `std::result::Result` +/// with the `Error` type repeatedly. pub type Result = std::result::Result; -#[derive(Debug)] +/// Represents the various errors that can occur in the application. +/// +/// This enum defines different types of errors that can be encountered, +/// such as invalid IDs and API errors. +#[derive(TError, Debug)] pub enum Error { + #[error("invalid ID")] InvalidId, - InvalidMediaType, + #[error("api error: `{0}`")] ApiError(String), } - -#[derive(Debug)] -struct InvalidId; - -impl StdError for InvalidId {} - -impl fmt::Display for InvalidId { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "The given id is invalid") - } -} - -#[derive(Debug)] -struct InvalidMediaType; - -impl StdError for InvalidMediaType {} - -impl fmt::Display for InvalidMediaType { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "The given media type is invalid") - } -}