remozipsy 0.0.2

zip implementation independent structs and helpers
Documentation
use core::{fmt::Debug, future::Future};
use futures_lite::stream::Stream;
use std::ops::RangeInclusive;

use bytes::Bytes;

use crate::model::FileInfo;

mod local_directory;
mod remote_file_info;
mod sync;

pub use local_directory::calculate_local_unix_path;
pub use sync::Statemachine;

//TODO: evaluating removing Clone here, so we can advantage of `mut` in
// evaluate phase

/// This trait is used to access the local file system.
/// You can implement it according to your needs or use
/// the [`crate::tokio::TokioLocalStorage`] struct.
/// Note: try to avoid parallel modifications to the Filesystem during
/// executiontime of the statemachine.
pub trait FileSystem {
    type Error: Debug + Send;
    type StorePrepare: Send;

    /// returns all files existing locally and thus might already be in sync/or
    /// out-of-sync with the remote files
    fn all_files(&mut self) -> impl Future<Output = Result<Vec<FileInfo>, Self::Error>> + Send;

    /// This function is always called before any actually data is storred. you
    /// can assume it to be called exactly once per storage, EXCEPT an error
    /// occurs in the meantime. Its useful for opening a file in the
    /// meantime, or do some checks early^
    fn prepare_store_file(
        &self,
        info: FileInfo,
    ) -> impl Future<Output = Result<Self::StorePrepare, Self::Error>> + Send;

    /// Write the data to disk
    fn store_file(
        &self,
        prepared: Self::StorePrepare,
        data: Bytes,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send;

    /// File should be removed from FileSystem
    fn delete_file(&self, info: FileInfo) -> impl Future<Output = Result<(), Self::Error>> + Send;
}

/// This trait is used to access the remote zip file.
/// You can implement it according to your needs or use
/// the [`crate::reqwest::ReqwestRemoteZip`] struct.
/// Note: the zip MUST NOT change during executiontime of the statemachine.
pub trait RemoteZip {
    type Error: Debug + Send;

    fn get_zip_size(&self) -> impl Future<Output = Result<usize, Self::Error>> + Send;
    fn fetch_bytes(&self, range: RangeInclusive<usize>) -> impl Future<Output = Result<Bytes, Self::Error>> + Send;
    fn fetch_bytes_stream(
        &self,
        range: RangeInclusive<usize>,
    ) -> impl Future<Output = Result<impl Stream<Item = Result<Bytes, Self::Error>> + Send, Self::Error>> + Send;
}