1use thiserror::Error;
6
7pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Error, Debug)]
12pub enum Error {
13 #[error("SSH error: {0}")]
15 Ssh(#[from] lmrc_ssh::Error),
16
17 #[error("SSH command execution failed: {message}")]
19 SshExecution {
20 message: String,
22 command: String,
24 },
25
26 #[error("PostgreSQL installation failed: {0}")]
28 Installation(String),
29
30 #[error("PostgreSQL configuration failed: {0}")]
32 Configuration(String),
33
34 #[error("PostgreSQL is not installed on the server")]
36 NotInstalled,
37
38 #[error("PostgreSQL version {0} is already installed")]
40 AlreadyInstalled(String),
41
42 #[error("Invalid PostgreSQL version: {0}")]
44 InvalidVersion(String),
45
46 #[error("Invalid configuration parameter: {parameter} = {value}")]
48 InvalidConfig {
49 parameter: String,
51 value: String,
53 },
54
55 #[error("Missing required configuration: {0}")]
57 MissingConfig(String),
58
59 #[error("PostgreSQL service error: {0}")]
61 ServiceError(String),
62
63 #[error("Database connection test failed: {0}")]
65 ConnectionTest(String),
66
67 #[error("PostgreSQL uninstallation failed: {0}")]
69 Uninstallation(String),
70
71 #[error("IO error: {0}")]
73 Io(#[from] std::io::Error),
74
75 #[error("Serialization error: {0}")]
77 Serialization(#[from] serde_json::Error),
78
79 #[error("{0}")]
81 Other(String),
82}
83
84impl Error {
85 pub fn ssh_execution(message: impl Into<String>, command: impl Into<String>) -> Self {
87 Self::SshExecution {
88 message: message.into(),
89 command: command.into(),
90 }
91 }
92
93 pub fn invalid_config(parameter: impl Into<String>, value: impl Into<String>) -> Self {
95 Self::InvalidConfig {
96 parameter: parameter.into(),
97 value: value.into(),
98 }
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn test_error_display() {
108 let err = Error::NotInstalled;
109 assert_eq!(err.to_string(), "PostgreSQL is not installed on the server");
110
111 let err = Error::AlreadyInstalled("15".to_string());
112 assert_eq!(
113 err.to_string(),
114 "PostgreSQL version 15 is already installed"
115 );
116
117 let err = Error::invalid_config("max_connections", "invalid");
118 assert_eq!(
119 err.to_string(),
120 "Invalid configuration parameter: max_connections = invalid"
121 );
122 }
123
124 #[test]
125 fn test_ssh_execution_error() {
126 let err = Error::ssh_execution("command failed", "apt-get install");
127 match err {
128 Error::SshExecution { message, command } => {
129 assert_eq!(message, "command failed");
130 assert_eq!(command, "apt-get install");
131 }
132 _ => panic!("Wrong error type"),
133 }
134 }
135}