Skip to main content

pg_embed/
pg_enums.rs

1//!
2//! Enums
3//!
4
5use std::fmt;
6
7use crate::command_executor::ProcessStatus;
8use crate::pg_errors::Error;
9
10///
11/// Postgresql authentication method
12///
13/// Choose between plain password, md5 or scram_sha_256 authentication.
14/// Scram_sha_256 authentication is only available on postgresql versions >= 11
15///
16pub enum PgAuthMethod {
17    /// plain-text
18    Plain,
19    /// md5
20    MD5,
21    /// scram_sha_256
22    ScramSha256,
23}
24
25impl fmt::Display for PgAuthMethod {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            PgAuthMethod::Plain => write!(f, "password"),
29            PgAuthMethod::MD5 => write!(f, "md5"),
30            PgAuthMethod::ScramSha256 => write!(f, "scram-sha-256"),
31        }
32    }
33}
34
35///
36/// Postgresql server status
37///
38#[derive(Debug, Clone, Copy, PartialEq)]
39pub enum PgServerStatus {
40    /// Postgres uninitialized
41    Uninitialized,
42    /// Initialization process running
43    Initializing,
44    /// Initialization process finished
45    Initialized,
46    /// Postgres server process starting
47    Starting,
48    /// Postgres server process started
49    Started,
50    /// Postgres server process stopping
51    Stopping,
52    /// Postgres server process stopped
53    Stopped,
54    /// Postgres failure
55    Failure,
56}
57
58///
59/// Postgesql process type
60///
61/// Used internally for distinguishing processes being executed
62///
63pub enum PgProcessType {
64    /// initdb process
65    InitDb,
66    /// pg_ctl start process
67    StartDb,
68    /// pg_ctl stop process
69    StopDb,
70}
71
72impl ProcessStatus<PgServerStatus, Error> for PgProcessType {
73    fn status_entry(&self) -> PgServerStatus {
74        match self {
75            PgProcessType::InitDb => PgServerStatus::Initializing,
76            PgProcessType::StartDb => PgServerStatus::Starting,
77            PgProcessType::StopDb => PgServerStatus::Stopping,
78        }
79    }
80
81    fn status_exit(&self) -> PgServerStatus {
82        match self {
83            PgProcessType::InitDb => PgServerStatus::Initialized,
84            PgProcessType::StartDb => PgServerStatus::Started,
85            PgProcessType::StopDb => PgServerStatus::Stopped,
86        }
87    }
88
89    fn error_type(&self) -> Error {
90        match self {
91            PgProcessType::InitDb => Error::PgInitFailure,
92            PgProcessType::StartDb => Error::PgStartFailure,
93            PgProcessType::StopDb => Error::PgStopFailure,
94        }
95    }
96
97    fn timeout_error(&self) -> Error {
98        Error::PgTimedOutError
99    }
100
101    fn wrap_error<E: std::error::Error + Sync + Send + 'static>(
102        &self,
103        error: E,
104        message: Option<String>,
105    ) -> Error {
106        Error::PgError(error.to_string(), message.unwrap_or_default())
107    }
108}
109
110impl fmt::Display for PgProcessType {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            PgProcessType::InitDb => write!(f, "initdb"),
114            PgProcessType::StartDb => write!(f, "start"),
115            PgProcessType::StopDb => write!(f, "stop"),
116        }
117    }
118}
119
120/// The operation systems enum
121#[derive(Debug, PartialEq, Copy, Clone)]
122pub enum OperationSystem {
123    /// macOS
124    Darwin,
125    /// Windows
126    Windows,
127    /// Linux (glibc)
128    Linux,
129    /// Alpine Linux (musl)
130    AlpineLinux,
131}
132
133impl fmt::Display for OperationSystem {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        match self {
136            OperationSystem::Darwin => write!(f, "darwin"),
137            OperationSystem::Windows => write!(f, "windows"),
138            OperationSystem::Linux => write!(f, "linux"),
139            OperationSystem::AlpineLinux => write!(f, "linux"),
140        }
141    }
142}
143
144#[allow(clippy::derivable_impls)]
145impl Default for OperationSystem {
146    fn default() -> Self {
147        #[cfg(not(any(target_os = "linux", target_os = "windows")))]
148        {
149            OperationSystem::Darwin
150        }
151
152        #[cfg(target_os = "linux")]
153        {
154            OperationSystem::Linux
155        }
156
157        #[cfg(target_os = "windows")]
158        {
159            OperationSystem::Windows
160        }
161    }
162}
163
164/// The cpu architectures enum
165#[derive(Debug, PartialEq, Copy, Clone)]
166pub enum Architecture {
167    /// x86_64
168    Amd64,
169    /// 32-bit x86
170    I386,
171    /// ARMv6 (32-bit)
172    Arm32v6,
173    /// ARMv7 (32-bit)
174    Arm32v7,
175    /// AArch64 / ARMv8 (64-bit)
176    Arm64v8,
177    /// POWER little-endian 64-bit
178    Ppc64le,
179}
180
181impl fmt::Display for Architecture {
182    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183        match self {
184            Architecture::Amd64 => write!(f, "amd64"),
185            Architecture::I386 => write!(f, "i386"),
186            Architecture::Arm32v6 => write!(f, "arm32v6"),
187            Architecture::Arm32v7 => write!(f, "arm32v7"),
188            Architecture::Arm64v8 => write!(f, "arm64v8"),
189            Architecture::Ppc64le => write!(f, "ppc64le"),
190        }
191    }
192}
193
194#[allow(clippy::derivable_impls)]
195impl Default for Architecture {
196    fn default() -> Self {
197        #[cfg(not(any(
198            target_arch = "x86",
199            target_arch = "arm",
200            target_arch = "aarch64",
201            target_arch = "powerpc64"
202        )))]
203        {
204            Architecture::Amd64
205        }
206
207        #[cfg(target_arch = "x86")]
208        {
209            Architecture::I386
210        }
211
212        #[cfg(target_arch = "arm")]
213        {
214            Architecture::Arm32v7
215        }
216
217        #[cfg(target_arch = "aarch64")]
218        {
219            Architecture::Arm64v8
220        }
221
222        #[cfg(target_arch = "powerpc64")]
223        {
224            Architecture::Ppc64le
225        }
226    }
227}
228
229/// The postgresql binaries acquisition status
230#[derive(Copy, Clone, PartialEq)]
231pub enum PgAcquisitionStatus {
232    /// Acquiring postgresql binaries
233    InProgress,
234    /// Finished acquiring postgresql binaries
235    Finished,
236    /// No acquisition
237    Undefined,
238}
239
240#[cfg(test)]
241mod tests {
242    use super::*;
243
244    #[test]
245    fn test_operation_system_display() {
246        assert_eq!(OperationSystem::Darwin.to_string(), "darwin");
247        assert_eq!(OperationSystem::Windows.to_string(), "windows");
248        assert_eq!(OperationSystem::Linux.to_string(), "linux");
249        assert_eq!(OperationSystem::AlpineLinux.to_string(), "linux");
250    }
251
252    #[test]
253    fn test_architecture_display() {
254        assert_eq!(Architecture::Amd64.to_string(), "amd64");
255        assert_eq!(Architecture::I386.to_string(), "i386");
256        assert_eq!(Architecture::Arm32v6.to_string(), "arm32v6");
257        assert_eq!(Architecture::Arm32v7.to_string(), "arm32v7");
258        assert_eq!(Architecture::Arm64v8.to_string(), "arm64v8");
259        assert_eq!(Architecture::Ppc64le.to_string(), "ppc64le");
260    }
261}