use std::{ cell::{RefCell, RefMut}, collections::{HashMap, hash_map::Entry}, }; 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 { shares: RefCell>, } #[derive(Debug, Default)] struct MockShare { files: HashMap, } #[derive(Debug)] struct MockFile { size: u64, offset: u64, } impl From<&file::Checked> for MockFile { fn from(value: &file::Checked) -> Self { Self { size: value.get_size(), offset: 0, } } } impl MockClient { fn insert_share(&self, share_id: &ShareID, share: MockShare) -> 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(share); Ok(()) } fn insert_file(&self, share_id: &ShareID, file_id: &FileID, file: MockFile) -> 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(file); 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 = true.into(); self.insert_share(&share_id, MockShare::default())?; 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 = true.into(); self.insert_file(share_id, &file_id, file.into())?; 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()?; 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(()) } }