// Copyright (c) 2025-2026 Zensical and contributors

// SPDX-License-Identifier: MIT
// Third-party contributions licensed under DCO

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// ----------------------------------------------------------------------------

//! Module error conversions.

use std::error;

use super::Error;

// ----------------------------------------------------------------------------
// Traits
// ----------------------------------------------------------------------------

/// Conversion into [`Error`].
pub trait IntoError {
    /// Converts into a module error.
    ///
    /// This method is intended to be used within [`Module::setup`][], to make
    /// it convenient to convert an arbitrary error into a module error.
    ///
    /// [`Module::setup`]: crate::Module::setup
    fn into_error(self) -> Error;
}

// ----------------------------------------------------------------------------
// Blanket implementations
// ----------------------------------------------------------------------------

impl<E> IntoError for E
where
    E: error::Error + Send + 'static,
{
    /// Converts any error into a module error.
    ///
    /// This implementation attempts to downcast the error into one of the known
    /// variants, and falls back to the [`Error::Other`] variant in other cases.
    /// It also ensures that the error is not double-boxed, returning unknown
    /// errors captured by the [`Error::Other`] variant unchanged.
    #[inline]
    fn into_error(self) -> Error {
        let mut err: Box<dyn error::Error + Send> = Box::new(self);
        err = match err.downcast() {
            Ok(err) => return *err,
            Err(err) => err,
        };

        // Implements downcast attempt of error
        macro_rules! downcast {
            ($variant:path) => {
                err = match err.downcast() {
                    Ok(err) => return $variant(*err),
                    Err(err) => err,
                }
            };
        }

        // Downcast known error variants
        downcast!(Error::Id);
        downcast!(Error::Expression);

        // Handle unknown error variants
        Error::Other(err)
    }
}
