tosho_kmkc/models/
errors.rs

1//! A module containing all the errors used in the library.
2//!
3//! If something is missing, please [open an issue](https://github.com/noaione/tosho-mango/issues/new/choose) or a [pull request](https://github.com/noaione/tosho-mango/compare).
4
5use tosho_common::{ToshoError, make_error};
6
7/// The used error type for the API.
8#[derive(Debug)]
9pub struct KMAPIError {
10    /// The error code from the API.
11    pub error_code: i32,
12    /// The error message from the API.
13    pub message: String,
14}
15
16impl std::fmt::Display for KMAPIError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(
19            f,
20            "An error occurred with status {}: {}",
21            self.error_code, self.message
22        )
23    }
24}
25
26impl std::error::Error for KMAPIError {}
27
28/// An error when you don't have enough point to buy a titles chapters.
29#[derive(Debug)]
30pub struct KMAPINotEnoughPointsError {
31    /// The error message
32    pub message: String,
33    /// The amount of points you need to buy the chapters.
34    pub points_needed: u64,
35    /// The amount of points you have.
36    pub points_have: u64,
37}
38
39impl std::fmt::Display for KMAPINotEnoughPointsError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(
42            f,
43            "{} ({} points needed, {} points have)",
44            self.message, self.points_needed, self.points_have
45        )
46    }
47}
48
49impl std::error::Error for KMAPINotEnoughPointsError {}
50
51impl From<KMAPIError> for ToshoError {
52    fn from(value: KMAPIError) -> Self {
53        make_error!("{}", value)
54    }
55}
56
57impl From<KMAPINotEnoughPointsError> for ToshoError {
58    fn from(value: KMAPINotEnoughPointsError) -> Self {
59        make_error!("{}", value)
60    }
61}