foukoapi 0.1.0-alpha.1

Cross-platform bot framework in Rust. Write your handlers once, run the same bot on Telegram and Discord with shared accounts, embeds, keyboards and SQLite storage.
Documentation
//! Errors produced by FoukoApi.

use std::fmt;

/// Shorthand for `std::result::Result<T, foukoapi::Error>`.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Top-level error type used across FoukoApi.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
    /// An adapter failed to start, connect or send a message.
    #[error("platform error ({platform}): {message}")]
    Platform {
        /// Human-readable platform name, e.g. `"telegram"`.
        platform: &'static str,
        /// Description of what went wrong.
        message: String,
    },

    /// The bot was asked to run but no platforms were registered.
    #[error("no platforms registered - add at least one with Bot::add_platform")]
    NoPlatforms,

    /// A handler returned an error.
    #[error("handler error: {0}")]
    Handler(Box<dyn std::error::Error + Send + Sync>),

    /// Catch-all for anything that doesn't fit a specific variant yet.
    #[error("{0}")]
    Other(String),
}

impl Error {
    /// Build a [`Error::Platform`] from a platform name and any display-able error.
    pub fn platform<E: fmt::Display>(platform: &'static str, err: E) -> Self {
        Self::Platform {
            platform,
            message: err.to_string(),
        }
    }
}

impl From<std::env::VarError> for Error {
    fn from(e: std::env::VarError) -> Self {
        Self::Other(format!("missing env var: {e}"))
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self::Other(format!("io error: {e}"))
    }
}