zynko 0.1.0

Biblioteca oficial para bots do Zynko: API, gateway e voz.
Documentation
//! The library's error type.

/// Any error, boxed: what event handlers return.
pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

/// `Result` with the library's [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Everything that can go wrong.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// An answer the API refused (docs.zynko.org/api/#erros).
    #[error("{message} ({status} {code})")]
    Api {
        /// HTTP status.
        status: u16,
        /// `bad_request`, `forbidden`, `not_found`, `rate_limited`...
        code: String,
        message: String,
    },
    /// The request did not reach the server, or the answer did not come back.
    #[error("falha na requisição: {0}")]
    Http(#[from] reqwest::Error),
    #[error("JSON inválido: {0}")]
    Json(#[from] serde_json::Error),
    #[error("{0}")]
    Io(#[from] std::io::Error),
    /// The gateway refused the token, or the client was closed.
    #[error("gateway: {0}")]
    Gateway(String),
    /// Waited too long (see `Client::wait_for`).
    #[error("tempo esgotado: {0}")]
    Timeout(String),
    /// Joining or playing in a voice room.
    #[error("voz: {0}")]
    Voice(String),
    /// An event handler returned an error or panicked.
    #[error("erro num handler: {0}")]
    Handler(BoxError),
    #[error("{0}")]
    Other(String),
}

impl Error {
    /// The HTTP status, for API errors.
    pub fn status(&self) -> Option<u16> {
        match self {
            Error::Api { status, .. } => Some(*status),
            Error::Http(e) => e.status().map(|s| s.as_u16()),
            _ => None,
        }
    }

    /// The API's error code (`not_found`, `forbidden`...), for API errors.
    pub fn code(&self) -> Option<&str> {
        match self {
            Error::Api { code, .. } => Some(code),
            _ => None,
        }
    }
}