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