Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/FileSystem/
ReadFile.rs

1//! Read a file from disk and return its bytes (always tagged `utf-8` -
2//! the encoding negotiation lives in Cocoon).
3
4use tonic::{Response, Status};
5
6use crate::{
7	RPC::CocoonService::CocoonServiceImpl,
8	Vine::Generated::{ReadFileRequest, ReadFileResponse},
9	dev_log,
10};
11
12pub async fn Fn(_Service:&CocoonServiceImpl, Request:ReadFileRequest) -> Result<Response<ReadFileResponse>, Status> {
13	let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
14		.ok_or_else(|| Status::invalid_argument("read_file: missing or empty URI"))?;
15
16	dev_log!("cocoon", "[CocoonService] Reading file: {:?}", Path);
17
18	let Content = tokio::fs::read(&Path).await.map_err(|Error| {
19		dev_log!("cocoon", "warn: [CocoonService] read_file failed for {:?}: {}", Path, Error);
20		Status::not_found(format!("read_file: {}: {}", Path.display(), Error))
21	})?;
22
23	Ok(Response::new(ReadFileResponse {
24		content:Content,
25		encoding:"utf-8".to_string(),
26	}))
27}