use std::{ cell::{RefCell, RefMut}, collections::{HashMap, hash_map::Entry}, }; use uuid::Uuid; use crate::{ Error, Result, error_response, file::{self, FileTrait}, sharry::{AliasID, Client, FileID, ShareID, Uri, json}, }; use super::mock_ids::CheckID; #[derive(Debug, Default)] pub struct MockClient { use_uuid: bool, shares: RefCell>, } #[derive(Debug, Default)] struct MockShare { files: HashMap, } #[derive(Debug)] struct MockFile { size: u64, offset: u64, } impl From for MockClient { fn from(value: bool) -> Self { Self { use_uuid: value, ..Default::default() } } } impl MockClient { fn insert_share(&self, share_id: &ShareID) -> Result<()> { let mut shares = self.shares.borrow_mut(); let Entry::Vacant(entry) = shares.entry(share_id.to_string()) else { return Err(error_response!("can't insert share {share_id:?}!")); }; entry.insert(MockShare::default()); Ok(()) } fn insert_file(&self, share_id: &ShareID, file_id: &FileID, size: u64) -> Result<()> { let mut share = self.get_share(share_id)?; let Entry::Vacant(entry) = share.files.entry(file_id.to_string()) else { return Err(error_response!("can't insert file {file_id:?}!")); }; entry.insert(MockFile { size, offset: 0 }); Ok(()) } fn get_share<'t>(&'t self, share_id: &ShareID) -> Result> { let share_id = &share_id.to_string(); let shares = self.shares.borrow_mut(); // check share exists shares .get(share_id) .ok_or_else(|| error_response!("can't find share {share_id:?}!"))?; Ok(RefMut::map(shares, |shares| { shares.get_mut(share_id).expect("checked but None!") })) } fn get_file<'t>( &'t self, share_id: &ShareID, file_id: &FileID, ) -> Result> { let file_id = &file_id.to_string(); let share = self.get_share(share_id)?; // check file exists share .files .get(file_id) .ok_or_else(|| error_response!("can't find file {file_id:?}!"))?; Ok(RefMut::map(share, move |share| { share.files.get_mut(file_id).expect("checked but None!") })) } } impl Client for MockClient { fn share_create( &self, uri: &Uri, alias_id: &AliasID, _: json::NewShareRequest, ) -> Result { (uri, alias_id).check()?; let share_id = if self.use_uuid { Uuid::now_v1(&[4, 8, 15, 16, 23, 42]).to_string().into() } else { true.into() }; self.insert_share(&share_id)?; Ok(share_id) } fn share_notify(&self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID) -> crate::Result<()> { (uri, alias_id).check()?; share_id.check()?; let _share = self.get_share(share_id)?; Ok(()) } fn file_create( &self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID, file: &file::Checked, ) -> Result { (uri, alias_id).check()?; share_id.check()?; let file_id = if self.use_uuid { let id = Uuid::now_v1(&[4, 8, 15, 16, 23, 42]); FileID::new_test(id) } else { true.into() }; self.insert_file(share_id, &file_id, file.get_size())?; Ok(file_id) } fn file_patch( &self, uri: &Uri, alias_id: &AliasID, share_id: &ShareID, chunk: &file::Chunk, ) -> Result<()> { (uri, alias_id).check()?; (share_id, chunk.get_file_id()).check()?; // TODO: `chunk` must align to a full MiB let file = self.get_file(share_id, chunk.get_file_id())?; if chunk.get_length() == 0 { return Err(error_response!("chunk {chunk:?} empty!")); } else if chunk.get_offset() % (1024 * 1024) != 0 { return Err(error_response!("chunk {chunk:?} not aligned to a MiB!")); } else if chunk.get_offset() != file.offset { return Error::mismatch(file.offset, chunk.get_offset()); } else if file.offset + chunk.get_length() > file.size { return Err(error_response!("chunk {chunk:?} too long!")); } let mut file = file; file.offset += chunk.get_length(); Ok(()) } }