use alloc::{
collections::{BTreeMap, BTreeSet},
vec::Vec,
};
use crate::path::VdirPath;
#[derive(Debug)]
pub enum VdirCoroutineState<Y, R> {
Yielded(Y),
Complete(R),
}
pub trait VdirCoroutine {
type Yield;
type Return;
fn resume(&mut self, arg: Option<VdirReply>) -> VdirCoroutineState<Self::Yield, Self::Return>;
}
#[derive(Debug)]
pub enum VdirYield {
WantsRandom {
len: usize,
},
WantsFileExists(BTreeSet<VdirPath>),
WantsDirExists(BTreeSet<VdirPath>),
WantsDirRead(BTreeSet<VdirPath>),
WantsFileRead(BTreeSet<VdirPath>),
WantsFileCreate(BTreeMap<VdirPath, Vec<u8>>),
WantsDirCreate(BTreeSet<VdirPath>),
WantsDirRemove(BTreeSet<VdirPath>),
WantsFileRemove(BTreeSet<VdirPath>),
WantsRename(Vec<(VdirPath, VdirPath)>),
WantsCopy(Vec<(VdirPath, VdirPath)>),
}
#[derive(Clone, Debug)]
pub enum VdirReply {
Random(Vec<u8>),
FileExists(BTreeMap<VdirPath, bool>),
DirExists(BTreeMap<VdirPath, bool>),
DirRead(BTreeMap<VdirPath, BTreeSet<VdirPath>>),
FileRead(BTreeMap<VdirPath, Vec<u8>>),
FileCreate,
DirCreate,
DirRemove,
FileRemove,
Rename,
Copy,
}
#[macro_export]
macro_rules! vdir_try {
($coroutine:expr, $arg:expr $(,)?) => {
match $crate::coroutine::VdirCoroutine::resume($coroutine, $arg) {
$crate::coroutine::VdirCoroutineState::Yielded(y) => {
return $crate::coroutine::VdirCoroutineState::Yielded(y.into());
}
$crate::coroutine::VdirCoroutineState::Complete(Err(err)) => {
return $crate::coroutine::VdirCoroutineState::Complete(Err(err.into()));
}
$crate::coroutine::VdirCoroutineState::Complete(Ok(value)) => value,
}
};
}