use crate::sdk::{
events::FileEvent,
storage::files::{ExternalFile, FileMutationEvent},
};
use indexmap::IndexSet;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FileSet(pub IndexSet<ExternalFile>);
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FileTransfersSet {
pub uploads: FileSet,
pub downloads: FileSet,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TransferOperation {
Upload,
Download,
Delete,
Move(ExternalFile),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct FileOperation(pub ExternalFile, pub TransferOperation);
impl From<&FileMutationEvent> for FileOperation {
fn from(value: &FileMutationEvent) -> Self {
match value {
FileMutationEvent::Create { event, .. } => event.into(),
FileMutationEvent::Move(event) => event.into(),
FileMutationEvent::Delete(event) => event.into(),
}
}
}
impl From<&FileEvent> for FileOperation {
fn from(value: &FileEvent) -> Self {
match value {
FileEvent::CreateFile(vault_id, secret_id, file_name) => {
FileOperation(
ExternalFile::new(*vault_id, *secret_id, *file_name),
TransferOperation::Upload,
)
}
FileEvent::DeleteFile(vault_id, secret_id, file_name) => {
FileOperation(
ExternalFile::new(*vault_id, *secret_id, *file_name),
TransferOperation::Delete,
)
}
FileEvent::MoveFile { name, from, dest } => FileOperation(
ExternalFile::new(from.0, from.1, *name),
TransferOperation::Move(ExternalFile::new(
dest.0, dest.1, *name,
)),
),
_ => panic!("attempt to convert noop file event"),
}
}
}