Skip to content

Commit

Permalink
fix: docs tests
Browse files Browse the repository at this point in the history
- use thiserror to make custom errors
  • Loading branch information
AndrielFR committed Jan 2, 2025
1 parent e9516fa commit 9f4e7a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
45 changes: 25 additions & 20 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<crate::models::Character> {
let data = self
Expand Down Expand Up @@ -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<crate::models::Character> {
self.get_character(id).await
Expand All @@ -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<crate::models::Person> {
let data = self
Expand Down
38 changes: 12 additions & 26 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2025 Andriel Ferreira <https://github.com/AndrielFR>

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<T> = std::result::Result<T, Error>;

#[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")
}
}

0 comments on commit 9f4e7a8

Please sign in to comment.