| 1 | use std::path::PathBuf; |
| 2 | |
| 3 | #[derive(Debug, thiserror::Error)] |
| 4 | pub enum Error { |
| 5 | #[error("repository not found: {0}")] |
| 6 | RepoNotFound(String), |
| 7 | |
| 8 | #[error("path traversal rejected: {0}")] |
| 9 | PathTraversal(PathBuf), |
| 10 | |
| 11 | #[error("invalid repository at {0}: {1}")] |
| 12 | InvalidRepo(PathBuf, String), |
| 13 | |
| 14 | #[error("protocol error: {0}")] |
| 15 | Protocol(String), |
| 16 | |
| 17 | #[error("git operation failed: {0}")] |
| 18 | Git(Box<gix::open::Error>), |
| 19 | |
| 20 | #[error("io error: {0}")] |
| 21 | Io(#[from] std::io::Error), |
| 22 | } |
| 23 | |
| 24 | impl From<gix::open::Error> for Error { |
| 25 | fn from(e: gix::open::Error) -> Self { |
| 26 | Error::Git(Box::new(e)) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | pub type Result<T> = std::result::Result<T, Error>; |
| 31 | |
| 32 | #[cfg(test)] |
| 33 | mod tests { |
| 34 | use super::*; |
| 35 | |
| 36 | #[test] |
| 37 | fn error_display_repo_not_found() { |
| 38 | let err = Error::RepoNotFound("foo/bar.git".into()); |
| 39 | assert_eq!(err.to_string(), "repository not found: foo/bar.git"); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn error_display_path_traversal() { |
| 44 | let err = Error::PathTraversal(PathBuf::from("../etc/passwd")); |
| 45 | assert_eq!(err.to_string(), "path traversal rejected: ../etc/passwd"); |
| 46 | } |
| 47 | } |