use thiserror::Error;
#[derive(Error, Debug)]
pub enum PinnerError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("API error: {0}")]
Api(String),
#[error("Parse error: {0}")]
Parse(String),
#[error("Path not found: {0}")]
PathNotFound(String),
#[error("Ignore error: {0}")]
Ignore(#[from] ignore::Error),
#[error("Config error: {0}")]
Config(String),
}
impl PinnerError {
pub fn is_path_not_found(&self) -> bool {
matches!(self, PinnerError::PathNotFound(_))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn test_error_display() {
let err = PinnerError::Api("failed".to_string());
assert_eq!(format!("{}", err), "API error: failed");
assert!(!err.is_path_not_found());
let err = PinnerError::Parse("yaml".to_string());
assert_eq!(format!("{}", err), "Parse error: yaml");
let err = PinnerError::PathNotFound("path/to/wf".to_string());
assert_eq!(format!("{}", err), "Path not found: path/to/wf");
assert!(err.is_path_not_found());
let err = PinnerError::Config("invalid".to_string());
assert_eq!(format!("{}", err), "Config error: invalid");
}
#[test]
fn test_error_from_io() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "not found");
let err = PinnerError::from(io_err);
assert!(matches!(err, PinnerError::Io(_)));
assert!(format!("{}", err).contains("IO error: not found"));
}
#[test]
fn test_error_from_ignore() {
let io_err = io::Error::other("ignore err");
let err = PinnerError::Ignore(ignore::Error::Io(io_err));
assert!(format!("{}", err).contains("Ignore error"));
}
}