//! Error types for peer operations.

/// Custom error types for peer operations.
#[derive(Debug)]
pub enum PeerError {
    /// Failed to determine the size of a file.
    FileSizeDetermination {
        path: String,
        source: std::io::Error,
    },
    /// Game directory has not been configured.
    GameDirNotSet,
    /// General error wrapper.
    Other(eyre::Report),
}

impl std::fmt::Display for PeerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PeerError::FileSizeDetermination { path, source } => {
                write!(f, "Failed to determine file size for {path}: {source}")
            }
            PeerError::GameDirNotSet => write!(f, "Game directory not set"),
            PeerError::Other(err) => write!(f, "General error: {err}"),
        }
    }
}

impl std::error::Error for PeerError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            PeerError::FileSizeDetermination { source, .. } => Some(source),
            PeerError::Other(err) => Some(err.root_cause()),
            PeerError::GameDirNotSet => None,
        }
    }
}

impl From<eyre::Report> for PeerError {
    fn from(err: eyre::Report) -> Self {
        PeerError::Other(err)
    }
}
