use crate::filesystem::*;
use melodium_core::*;
use melodium_macro::mel_treatment;
use std::sync::Arc;
#[mel_treatment(
default recursive true
input path Block<string>
input filesystem Block<FileSystem>
output success Block<void>
output failure Block<void>
output error Block<string>
)]
pub async fn create(recursive: bool) {
if let (Ok(filesystem), Ok(path)) = (
filesystem.recv_one().await.map(|val| {
GetData::<Arc<dyn Data>>::try_data(val)
.unwrap()
.downcast_arc::<FileSystem>()
.unwrap()
}),
path.recv_one()
.await
.map(|val| GetData::<string>::try_data(val).unwrap()),
) {
filesystem
.filesystem
.create_dir(
&path,
recursive,
Box::new(|| {
Box::pin(async {
let _ = success.send_one(().into()).await;
})
}),
Box::new(|| {
Box::pin(async {
let _ = failure.send_one(().into()).await;
})
}),
Box::new(|msg: String| {
Box::pin(async {
let _ = error.send_one(msg.into()).await;
})
}),
)
.await
}
}
#[mel_treatment(
default recursive false
default follow_links true
input path Block<string>
input filesystem Block<FileSystem>
output entries Stream<string>
output completed Block<void>
output failed Block<void>
output finished Block<void>
output errors Stream<string>
)]
pub async fn scan(recursive: bool, follow_links: bool) {
if let (Ok(filesystem), Ok(path)) = (
filesystem.recv_one().await.map(|val| {
GetData::<Arc<dyn Data>>::try_data(val)
.unwrap()
.downcast_arc::<FileSystem>()
.unwrap()
}),
path.recv_one()
.await
.map(|val| GetData::<string>::try_data(val).unwrap()),
) {
filesystem
.filesystem
.scan_dir(
&path,
recursive,
follow_links,
Box::new(|path: String| {
Box::pin(async { entries.send_one(path.into()).await.map_err(|_| ()) })
}),
Box::new(|| {
Box::pin(async {
let _ = completed.send_one(().into()).await;
})
}),
Box::new(|| {
Box::pin(async {
let _ = failed.send_one(().into()).await;
})
}),
Box::new(|| {
Box::pin(async {
let _ = finished.send_one(().into()).await;
})
}),
Box::new(|msg: String| {
Box::pin(async { errors.send_one(msg.into()).await.map_err(|_| ()) })
}),
)
.await
}
}