recursive_copy 2.0.0

A minimal, safe, and portable recursive copy library for Unix systems.
Documentation
//! Error types for recursive copy operations.

use std::fmt;
use std::io;
use std::path::PathBuf;
use walkdir_minimal::WalkError;

/// Enumeration of possible errors during a copy operation.
#[derive(Debug)]
pub enum CopyError {
    /// Standard input/output error.
    Io(io::Error),
    /// Error encountered while traversing the directory tree.
    Walk(WalkError),
    /// A circular reference was detected through symbolic links.
    SymlinkLoop(PathBuf),
}

impl fmt::Display for CopyError {
    /// Formats the error for user-facing display.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CopyError::Io(e) => write!(f, "IO error: {}", e),
            CopyError::Walk(e) => write!(f, "Walk error: {:?}", e),
            CopyError::SymlinkLoop(p) => write!(f, "Symlink loop detected: {}", p.display()),
        }
    }
}

impl std::error::Error for CopyError {
    /// Returns the underlying cause of the error if it exists.
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CopyError::Io(e) => Some(e),
            CopyError::Walk(e) => Some(e),
            CopyError::SymlinkLoop(_) => None,
        }
    }
}

impl From<io::Error> for CopyError {
    /// Automatically converts `std::io::Error` into `CopyError::Io`.
    fn from(e: io::Error) -> Self {
        CopyError::Io(e)
    }
}

impl From<WalkError> for CopyError {
    /// Automatically converts `WalkError` into `CopyError::Walk`.
    fn from(e: WalkError) -> Self {
        CopyError::Walk(e)
    }
}