// SPDX-FileCopyrightText: 2026 Contributors to brew-proxy <https://codeberg.org/HastD/brew-proxy>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::fmt;

use brew_proxy_utils::bundle::BundleSubcommandError;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    Dbus(zbus::fdo::Error),
    Io(std::io::Error),
    Pty(pty_process::Error),
    BundleSubcommand(BundleSubcommandError),
    Other(Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Dbus(err) => err.fmt(f),
            Self::Io(err) => err.fmt(f),
            Self::Pty(err) => err.fmt(f),
            Self::BundleSubcommand(err) => err.fmt(f),
            Self::Other(err) => err.fmt(f),
        }
    }
}

impl std::error::Error for Error {}

impl From<zbus::fdo::Error> for Error {
    fn from(err: zbus::fdo::Error) -> Self {
        Self::Dbus(err)
    }
}

impl From<zbus::Error> for Error {
    fn from(err: zbus::Error) -> Self {
        Self::Dbus(err.into())
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<BundleSubcommandError> for Error {
    fn from(err: BundleSubcommandError) -> Self {
        Self::BundleSubcommand(err)
    }
}

impl From<pty_process::Error> for Error {
    fn from(err: pty_process::Error) -> Self {
        Self::Pty(err)
    }
}

pub type Result<T, E = Error> = std::result::Result<T, E>;
