use core::fmt::Display;
use std::error::Error as StdError;

use itertools::Itertools as _;

pub trait ApiError {
    // `anyhow::Error` prints the chain of sources if the alternate flag is specified.
    // Impls generated by `thiserror::Error` ignore the alternate flag. See:
    // - <https://github.com/dtolnay/thiserror/issues/78>
    // - <https://github.com/dtolnay/thiserror/issues/98>
    // - <https://github.com/dtolnay/thiserror/issues/214>
    fn format_sources(&self) -> impl Display + '_ {
        self.sources().format(": ")
    }

    // `StdError::sources` is not stable as of Rust 1.80.1.
    fn sources(&self) -> impl Iterator<Item = &dyn StdError>;
}
