1use std::io;
2
3pub type Result<T> = std::result::Result<T, GitError>;
4
5#[derive(Debug, Clone)]
6pub enum GitError {
7 IoError(String),
8 CommandFailed(String),
9}
10
11impl From<io::Error> for GitError {
12 fn from(error: io::Error) -> Self {
13 GitError::IoError(error.to_string())
14 }
15}
16
17#[cfg(test)]
18mod tests {
19 use super::*;
20 use std::io;
21
22 #[test]
23 fn test_git_error_io_error_variant() {
24 let error = GitError::IoError("test io error".to_string());
25 match error {
26 GitError::IoError(msg) => assert_eq!(msg, "test io error"),
27 _ => panic!("Expected IoError variant"),
28 }
29 }
30
31 #[test]
32 fn test_git_error_command_failed_variant() {
33 let error = GitError::CommandFailed("test command failed".to_string());
34 match error {
35 GitError::CommandFailed(msg) => assert_eq!(msg, "test command failed"),
36 _ => panic!("Expected CommandFailed variant"),
37 }
38 }
39
40 #[test]
41 fn test_git_error_clone() {
42 let error1 = GitError::IoError("test error".to_string());
43 let error2 = error1.clone();
44
45 match (error1, error2) {
46 (GitError::IoError(msg1), GitError::IoError(msg2)) => assert_eq!(msg1, msg2),
47 _ => panic!("Clone failed or wrong variant"),
48 }
49 }
50
51 #[test]
52 fn test_git_error_debug() {
53 let io_error = GitError::IoError("io test".to_string());
54 let cmd_error = GitError::CommandFailed("cmd test".to_string());
55
56 let io_debug = format!("{:?}", io_error);
57 let cmd_debug = format!("{:?}", cmd_error);
58
59 assert!(io_debug.contains("IoError"));
60 assert!(io_debug.contains("io test"));
61 assert!(cmd_debug.contains("CommandFailed"));
62 assert!(cmd_debug.contains("cmd test"));
63 }
64
65 #[test]
66 fn test_from_io_error() {
67 let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
68 let git_err: GitError = io_err.into();
69
70 match git_err {
71 GitError::IoError(msg) => assert!(msg.contains("file not found")),
72 _ => panic!("Expected IoError variant from io::Error conversion"),
73 }
74 }
75
76 #[test]
77 fn test_from_io_error_different_kinds() {
78 let permission_err = io::Error::new(io::ErrorKind::PermissionDenied, "access denied");
79 let git_err: GitError = permission_err.into();
80
81 match git_err {
82 GitError::IoError(msg) => assert!(msg.contains("access denied")),
83 _ => panic!("Expected IoError variant"),
84 }
85 }
86
87 #[test]
88 fn test_result_type_alias() {
89 fn test_function() -> Result<String> {
90 Ok("success".to_string())
91 }
92
93 let result = test_function();
94 assert!(result.is_ok());
95 assert_eq!(result.unwrap(), "success");
96 }
97
98 #[test]
99 fn test_result_type_alias_error() {
100 fn test_function() -> Result<String> {
101 Err(GitError::CommandFailed("test error".to_string()))
102 }
103
104 let result = test_function();
105 assert!(result.is_err());
106 match result.unwrap_err() {
107 GitError::CommandFailed(msg) => assert_eq!(msg, "test error"),
108 _ => panic!("Expected CommandFailed variant"),
109 }
110 }
111}