Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/FileSystem/
RenameFile.rs

1//! Rename a file or directory, creating any missing target parents first.
2
3use tonic::{Response, Status};
4
5use crate::{
6	RPC::CocoonService::CocoonServiceImpl,
7	Vine::Generated::{Empty, RenameFileRequest},
8	dev_log,
9};
10
11pub async fn Fn(_Service:&CocoonServiceImpl, Request:RenameFileRequest) -> Result<Response<Empty>, Status> {
12	let OldPath = CocoonServiceImpl::UriToPath(Request.source.as_ref())
13		.ok_or_else(|| Status::invalid_argument("rename_file: missing source URI"))?;
14
15	let NewPath = CocoonServiceImpl::UriToPath(Request.target.as_ref())
16		.ok_or_else(|| Status::invalid_argument("rename_file: missing target URI"))?;
17
18	dev_log!("cocoon", "[CocoonService] rename_file: {:?} → {:?}", OldPath, NewPath);
19
20	if let Some(Parent) = NewPath.parent() {
21		if !Parent.as_os_str().is_empty() {
22			tokio::fs::create_dir_all(Parent)
23				.await
24				.map_err(|Error| Status::internal(format!("rename_file: create_dir_all failed: {}", Error)))?;
25		}
26	}
27
28	tokio::fs::rename(&OldPath, &NewPath)
29		.await
30		.map_err(|Error| Status::internal(format!("rename_file: {}: {}", OldPath.display(), Error)))?;
31
32	Ok(Response::new(Empty {}))
33}