use std::path::PathBuf;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Error, Debug, Diagnostic)]
pub enum HoldError {
#[error("Git repository not found in '{0}' or any parent directories")]
#[diagnostic(
code(cargo_hold::git::repo_not_found),
help("Ensure 'cargo hold' is run from within a Git repository.")
)]
RepoNotFound(
PathBuf,
),
#[error("Failed to access Git index")]
#[diagnostic(code(cargo_hold::git::index_error))]
IndexError(#[from] git2::Error),
#[error("I/O error accessing '{path}'")]
#[diagnostic(code(cargo_hold::io_error))]
IoError {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("Failed to serialize metadata")]
#[diagnostic(
code(cargo_hold::metadata::serialization_error),
help(
"An internal error occurred while trying to save the metadata. Try running 'cargo \
hold bilge' to reset."
)
)]
SerializationError(#[source] Box<dyn std::error::Error + Send + Sync>),
#[error("Failed to deserialize metadata: {0}")]
#[diagnostic(
code(cargo_hold::metadata::deserialization_error),
help("The metadata file may be corrupted. Run 'cargo hold bilge' to reset it.")
)]
DeserializationError(
#[source]
rkyv::rancor::BoxedError,
),
#[error("Invalid path: {message}")]
#[diagnostic(code(cargo_hold::path::invalid))]
InvalidPath {
message: String,
},
#[error("Invalid file type for '{0}': {1}")]
#[diagnostic(
code(cargo_hold::file::invalid_type),
help("cargo-hold only processes regular files tracked by Git.")
)]
InvalidFileType(
PathBuf,
String,
),
#[error("Failed to set file modification time for '{0}'")]
#[diagnostic(
code(cargo_hold::timestamp::set_error),
help("Ensure you have write permissions for the file.")
)]
SetTimestampError(
PathBuf,
#[source]
std::io::Error,
),
#[error("Failed to create metadata directory '{0}'")]
#[diagnostic(
code(cargo_hold::metadata::create_dir_error),
help("Ensure you have write permissions for the parent directory.")
)]
CreateMetadataDirError(
PathBuf,
#[source]
std::io::Error,
),
#[error("Invalid metadata size: '{0}' - {1}")]
#[diagnostic(
code(cargo_hold::gc::invalid_metadata_size),
help(
"Specify metadata size as a number with optional suffix (e.g., '5G', '500M', '1024K', \
or raw bytes)"
)
)]
InvalidMetadataSize(
String,
String,
),
#[error("Garbage collection error: {0}")]
#[diagnostic(
code(cargo_hold::gc::error),
help("Check permissions and disk space, then try again.")
)]
GcError(
String,
),
#[error("Configuration error: {0}")]
#[diagnostic(
code(cargo_hold::config::error),
help("Check the required configuration parameters.")
)]
ConfigError(
String,
),
#[error("failed to process {failed} of {total} tracked file(s); run with -v for details")]
#[diagnostic(
code(cargo_hold::files::partial_failure),
help("Fix file permissions or paths, then re-run the command.")
)]
PartialFileProcessing {
failed: usize,
total: usize,
},
#[error("Invalid UTF-8 in path: {0}")]
#[diagnostic(
code(cargo_hold::path::invalid_utf8),
help("File paths must be valid UTF-8. This is a requirement for Git-tracked files.")
)]
InvalidUtf8Path(
PathBuf,
),
}
pub type Result<T> = std::result::Result<T, HoldError>;