Skip to main content

Mountain/IPC/WindServiceHandlers/FileSystem/Native/
FileReadNative.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3//! Wire method `file:read` / `file:readFile`.
4//!
5//! Returns `{ buffer: number[] }`. VS Code's `DiskFileSystemProviderClient`
6//! wraps the payload with `VSBuffer.wrap()`. The explicit byte array is
7//! required because `FileProtocolShim` used to return a struct with a
8//! `number[]` field and any change to that shape breaks the Blob worker
9//! round-trip - see `feedback_ipc_binary_fetch.md`.
10
11use serde_json::{Value, json};
12
13use crate::{IPC::WindServiceHandlers::Utilities::PathExtraction::extract_path_from_arg, dev_log};
14
15pub async fn FileReadNative(Arguments:Vec<Value>) -> Result<Value, String> {
16	let Path = extract_path_from_arg(Arguments.get(0).ok_or("Missing file path")?)?;
17
18	dev_log!("vfs-verbose", "readFile: {}", Path);
19
20	let Bytes = tokio::fs::read(&Path)
21		.await
22		.map_err(|E| format!("Failed to read file: {} (path: {})", E, Path))?;
23
24	dev_log!("vfs-verbose", "readFile OK: {} ({} bytes)", Path, Bytes.len());
25
26	let ByteArray:Vec<Value> = Bytes.iter().map(|B| json!(*B)).collect();
27	Ok(json!({ "buffer": ByteArray }))
28}