Skip to main content

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

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