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

use crate::{
    chunk::{Chunk, Label},
    id::Id,
};

/// Use a local or remote repository.
pub trait BackupRepository {
    /// Type of error from using this kind of repository.
    type Error: Into<RepositoryError>;

    /// Get a chunk from the repository.
    fn get(&self, id: &Id) -> Result<Chunk, Self::Error>;

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

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

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

/// Errors from using an implementation of [`BackupRepository`].
#[derive(Debug, thiserror::Error)]
pub enum RepositoryError {
    /// Error from implementation of [`BackupRepository`] trait.
    #[error("failed to use backup repository")]
    Underlying(#[source] Box<dyn std::error::Error>),
}
