dirgrab_lib/
errors.rs

1// --- FILE: dirgrab-lib/src/errors.rs ---
2
3use std::io;
4use std::path::PathBuf;
5use thiserror::Error;
6
7/// Errors that can occur during the `dirgrab` library operations.
8///
9/// These errors cover issues ranging from file system access problems
10/// to Git command failures and configuration errors.
11#[derive(Error, Debug)]
12pub enum GrabError {
13    // Make enum public
14    /// The initial `target_path` provided in the `GrabConfig` was not found
15    /// on the filesystem or was inaccessible due to permissions.
16    #[error("Target path not found or not accessible: {0}")]
17    TargetPathNotFound(PathBuf),
18
19    /// An I/O error occurred while accessing a path during the operation
20    /// (e.g., reading a file, canonicalizing a path).
21    #[error("IO error accessing path '{path}': {source}")]
22    IoError {
23        path: PathBuf,
24        #[source]
25        source: io::Error,
26    },
27
28    /// A `git` command (like `git ls-files` or `git rev-parse`) failed to execute
29    /// successfully, indicated by a non-zero exit status.
30    /// Contains the command string, stderr, and stdout output for debugging.
31    #[error("Failed to execute git command: {command:?}\n  stderr: {stderr}\n  stdout: {stdout}")]
32    GitCommandError {
33        command: String,
34        stderr: String,
35        stdout: String,
36    },
37
38    /// An error occurred while trying to spawn or run the `git` process itself.
39    /// This commonly happens if `git` is not installed or not found in the system's PATH,
40    /// but can also indicate permission errors preventing execution.
41    #[error("Failed to run git command '{command}': {source}")]
42    GitExecutionError {
43        command: String,
44        #[source]
45        source: io::Error,
46    },
47
48    /// Failed to build the glob pattern matcher from the patterns provided
49    /// in `GrabConfig::exclude_patterns`. This might happen if a pattern has
50    /// invalid syntax according to the `ignore` crate.
51    #[error("Failed to build glob pattern matcher: {0}")]
52    GlobMatcherBuildError(#[source] ignore::Error),
53
54    /// Error specifically for path stripping issues during tree generation.
55    #[error("Failed to strip prefix '{prefix}' from path '{path}' during tree generation")]
56    PathStripError { prefix: PathBuf, path: PathBuf },
57}
58
59/// A convenience type alias for `Result<T, GrabError>`.
60pub type GrabResult<T> = Result<T, GrabError>; // Make type alias public