//! Interface for using a backup repository wherever it is.

use crate::{
    api_client::ApiClientError, chunk::Chunk, id::Id, label::Label,
    local_repo::LocalRepositoryError,
};

/// Use a local or remote repository.
pub trait BackupRepository {
    /// Get a chunk from the repository.
    fn get(&self, id: &Id) -> Result<Chunk, RepositoryError>;

    /// Put a chunk into the repository.
    fn put(&self, chunk: &Chunk) -> Result<(), RepositoryError>;

    /// Delete a chunk from the repository.
    fn delete(&self, id: &Id) -> Result<(), RepositoryError>;

    /// Find chunks with a label in the repository.
    fn find(&self, label: &Label) -> Result<Vec<Id>, RepositoryError>;
}

/// Errors from using an implementation of [`BackupRepository`].
#[derive(Debug, thiserror::Error)]
pub enum RepositoryError {
    /// Error from using a local repository.
    #[error("failed to use backup repository")]
    Local(#[source] LocalRepositoryError),

    /// Error from using a remote Obnam API instance.
    #[error("failed to use remote backup repository")]
    Remote(#[source] ApiClientError),
}

impl From<LocalRepositoryError> for RepositoryError {
    fn from(value: LocalRepositoryError) -> Self {
        Self::Local(value)
    }
}

impl From<ApiClientError> for RepositoryError {
    fn from(value: ApiClientError) -> Self {
        Self::Remote(value)
    }
}
