Skip to main content

Mountain/RPC/CocoonService/GenericRequest/FileSystem/
Readdir.rs

1use serde_json::{Value, json};
2use tonic::Response;
3use ::Vine::Generated::GenericResponse;
4
5pub async fn Fn(RequestId:u64, Params:Value) -> Response<GenericResponse> {
6	let Path = Params
7		.as_str()
8		.or_else(|| Params.get("path").and_then(|V| V.as_str()))
9		.unwrap_or("");
10
11	match tokio::fs::read_dir(Path).await {
12		Ok(mut Entries) => {
13			let mut Items:Vec<Value> = Vec::new();
14
15			while let Ok(Some(Entry)) = Entries.next_entry().await {
16				if let Some(Name) = Entry.file_name().to_str() {
17					let IsDir = Entry.file_type().await.map(|T| T.is_dir()).unwrap_or(false);
18
19					Items.push(json!({ "name": Name, "type": if IsDir { 2u32 } else { 1u32 } }));
20				}
21			}
22
23			super::OkResponse(RequestId, &Items)
24		},
25
26		Err(Error) => super::ErrResponse(RequestId, -32000, format!("fs.listDir: {}", Error)),
27	}
28}