Skip to content

Commit

Permalink
chores
Browse files Browse the repository at this point in the history
  • Loading branch information
MordechaiHadad committed Sep 10, 2024
1 parent 291e4e0 commit 435f3de
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions src/handlers/use_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,60 @@ async fn copy_nvim_proxy(config: &Config) -> Result<()> {
Ok(())
}

/// Asynchronously copies a file from `old_path` to `new_path`, handling specific OS errors.
///
/// This function attempts to copy a file from the specified `old_path` to the specified `new_path`.
/// If the file is being used by another process (OS error 26 or 32), it prints an error message
/// and returns an error indicating that the file is busy. For any other errors, it returns a
/// generic error with additional context.
///
/// # Arguments
///
/// * `old_path` - A reference to the source `Path` of the file to be copied.
/// * `new_path` - A reference to the destination `Path` where the file should be copied.
///
/// # Returns
///
/// This function returns a `Result<()>`. If the file is successfully copied, it returns `Ok(())`.
/// If an error occurs, it returns an `Err` with a detailed error message.
///
/// # Errors
///
/// This function will return an error in the following cases:
/// - If the file is being used by another process (OS error 26 or 32), it returns an error
/// indicating that the file is busy.
/// - For any other errors, it returns a generic error with additional context.
///
/// # Examples
///
/// ```rust
/// use std::path::Path;
/// use anyhow::Result;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
/// let old_path = Path::new("path/to/source/file");
/// let new_path = Path::new("path/to/destination/file");
///
/// copy_file_with_error_handling(&old_path, &new_path).await?;
/// Ok(())
/// }
/// ```
async fn copy_file_with_error_handling(old_path: &Path, new_path: &Path) -> Result<()> {
match fs::copy(&old_path, &new_path).await {
Ok(_) => Ok(()),
Err(e) => match e.raw_os_error() {
Some(26) | Some(32) => {
eprintln!("Error: The file is busy. Please make sure to close any processes using it.");
Err(anyhow::anyhow!("The file {} is busy. Please make sure to close any processes using it.", old_path.display()))
Err(e) => {
match e.raw_os_error() {
Some(26) | Some(32) => {
eprintln!("Error: The file is busy. Please make sure to close any processes using it.");
Err(anyhow::anyhow!(
"The file {} is busy. Please make sure to close any processes using it.",
old_path.display()
))
}
_ => Err(anyhow::anyhow!(e).context("Failed to copy file")),
}
_ => Err(anyhow::anyhow!(e).context("Failed to copy file")),
},
}
}
}

Expand Down

0 comments on commit 435f3de

Please sign in to comment.