use thiserror::Error;
#[derive(Error, Debug)]
pub enum DepsError {
#[error("failed to parse {file_type}: {source}")]
ParseError {
file_type: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("registry request failed for {package}: {source}")]
RegistryError {
package: String,
#[source]
source: reqwest::Error,
},
#[error("cache error: {0}")]
CacheError(String),
#[error("invalid version requirement: {0}")]
InvalidVersionReq(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("unsupported ecosystem: {0}")]
UnsupportedEcosystem(String),
#[error("ambiguous ecosystem detection for file: {0}")]
AmbiguousEcosystem(String),
#[error("invalid URI: {0}")]
InvalidUri(String),
}
pub type Result<T> = std::result::Result<T, DepsError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let error = DepsError::CacheError("test error".into());
assert_eq!(error.to_string(), "cache error: test error");
}
#[test]
fn test_invalid_version_req() {
let error = DepsError::InvalidVersionReq("invalid".into());
assert_eq!(error.to_string(), "invalid version requirement: invalid");
}
#[test]
fn test_parse_error() {
let io_err = std::io::Error::new(std::io::ErrorKind::InvalidData, "bad data");
let error = DepsError::ParseError {
file_type: "Cargo.toml".into(),
source: Box::new(io_err),
};
assert!(error.to_string().contains("failed to parse Cargo.toml"));
}
#[test]
fn test_io_error_conversion() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let error: DepsError = io_err.into();
assert!(error.to_string().contains("I/O error"));
}
#[test]
fn test_unsupported_ecosystem() {
let error = DepsError::UnsupportedEcosystem("unknown".into());
assert_eq!(error.to_string(), "unsupported ecosystem: unknown");
}
#[test]
fn test_ambiguous_ecosystem() {
let error = DepsError::AmbiguousEcosystem("file.txt".into());
assert_eq!(
error.to_string(),
"ambiguous ecosystem detection for file: file.txt"
);
}
#[test]
fn test_invalid_uri() {
let error = DepsError::InvalidUri("http://example.com".into());
assert_eq!(error.to_string(), "invalid URI: http://example.com");
}
}