pg_embed_alternative/
pg_enums.rs

1//!
2//! Enums
3//!
4
5use std::error::Error;
6
7use crate::command_executor::ProcessStatus;
8use crate::pg_errors::{PgEmbedError, PgEmbedErrorType};
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
25///
26/// Postgresql server status
27///
28#[derive(Debug, Clone, Copy, PartialEq)]
29pub enum PgServerStatus {
30    /// Postgres uninitialized
31    Uninitialized,
32    /// Initialization process running
33    Initializing,
34    /// Initialization process finished
35    Initialized,
36    /// Postgres server process starting
37    Starting,
38    /// Postgres server process started
39    Started,
40    /// Postgres server process stopping
41    Stopping,
42    /// Postgres server process stopped
43    Stopped,
44    /// Postgres failure
45    Failure,
46}
47
48///
49/// Postgesql process type
50///
51/// Used internally for distinguishing processes being executed
52///
53pub enum PgProcessType {
54    /// initdb process
55    InitDb,
56    /// pg_ctl start process
57    StartDb,
58    /// pg_ctl stop process
59    StopDb,
60}
61
62impl ProcessStatus<PgServerStatus, PgEmbedError> for PgProcessType {
63    fn status_entry(&self) -> PgServerStatus {
64        match self {
65            PgProcessType::InitDb => PgServerStatus::Initializing,
66            PgProcessType::StartDb => PgServerStatus::Starting,
67            PgProcessType::StopDb => PgServerStatus::Stopping,
68        }
69    }
70
71    fn status_exit(&self) -> PgServerStatus {
72        match self {
73            PgProcessType::InitDb => PgServerStatus::Initialized,
74            PgProcessType::StartDb => PgServerStatus::Started,
75            PgProcessType::StopDb => PgServerStatus::Stopped,
76        }
77    }
78
79    fn error_type(&self) -> PgEmbedError {
80        match self {
81            PgProcessType::InitDb => PgEmbedError {
82                error_type: PgEmbedErrorType::PgInitFailure,
83                source: None,
84                message: None,
85            },
86            PgProcessType::StartDb => PgEmbedError {
87                error_type: PgEmbedErrorType::PgStartFailure,
88                source: None,
89                message: None,
90            },
91            PgProcessType::StopDb => PgEmbedError {
92                error_type: PgEmbedErrorType::PgStopFailure,
93                source: None,
94                message: None,
95            },
96        }
97    }
98
99    fn wrap_error<E: Error + Sync + Send + 'static>(
100        &self,
101        error: E,
102        message: Option<String>,
103    ) -> PgEmbedError {
104        PgEmbedError {
105            error_type: PgEmbedErrorType::PgError,
106            source: Some(Box::new(error)),
107            message,
108        }
109    }
110}
111
112impl ToString for PgProcessType {
113    fn to_string(&self) -> String {
114        match self {
115            PgProcessType::InitDb => "initdb".to_string(),
116            PgProcessType::StartDb => "start".to_string(),
117            PgProcessType::StopDb => "stop".to_string(),
118        }
119    }
120}
121
122/// The operation systems enum
123#[derive(Debug, PartialEq, Copy, Clone)]
124pub enum OperationSystem {
125    Darwin,
126    Windows,
127    Linux,
128    AlpineLinux,
129}
130
131impl ToString for OperationSystem {
132    fn to_string(&self) -> String {
133        match &self {
134            OperationSystem::Darwin => "darwin".to_string(),
135            OperationSystem::Windows => "windows".to_string(),
136            OperationSystem::Linux => "linux".to_string(),
137            OperationSystem::AlpineLinux => "linux".to_string(),
138        }
139    }
140}
141
142impl Default for OperationSystem {
143    fn default() -> Self {
144        #[cfg(not(any(target_os = "linux", target_os = "windows")))]
145        {
146            OperationSystem::Darwin
147        }
148
149        #[cfg(target_os = "linux")]
150        {
151            OperationSystem::Linux
152        }
153
154        #[cfg(target_os = "windows")]
155        {
156            OperationSystem::Windows
157        }
158    }
159}
160
161/// The cpu architectures enum
162#[derive(Debug, PartialEq, Copy, Clone)]
163pub enum Architecture {
164    Amd64,
165    I386,
166    Arm32v6,
167    Arm32v7,
168    Arm64v8,
169    Ppc64le,
170}
171
172impl ToString for Architecture {
173    fn to_string(&self) -> String {
174        match &self {
175            Architecture::Amd64 => "amd64".to_string(),
176            Architecture::I386 => "i386".to_string(),
177            Architecture::Arm32v6 => "arm32v6".to_string(),
178            Architecture::Arm32v7 => "arm32v7".to_string(),
179            Architecture::Arm64v8 => "arm64v8".to_string(),
180            Architecture::Ppc64le => "ppc64le".to_string(),
181        }
182    }
183}
184
185impl Default for Architecture {
186    fn default() -> Self {
187        #[cfg(not(any(
188            target_arch = "x86",
189            target_arch = "arm",
190            target_arch = "aarch64",
191            target_arch = "powerpc64"
192        )))]
193        {
194            Architecture::Amd64
195        }
196
197        #[cfg(target_arch = "x86")]
198        {
199            Architecture::I386
200        }
201
202        #[cfg(target_arch = "arm")]
203        {
204            Architecture::Arm32v7
205        }
206
207        #[cfg(target_arch = "aarch64")]
208        {
209            Architecture::Arm64v8
210        }
211
212        #[cfg(target_arch = "powerpc64")]
213        {
214            Architecture::Ppc64le
215        }
216    }
217}
218
219/// The postgresql binaries acquisition status
220#[derive(Copy, Clone, PartialEq)]
221pub enum PgAcquisitionStatus {
222    /// Acquiring postgresql binaries
223    InProgress,
224    /// Finished acquiring postgresql binaries
225    Finished,
226    /// No acquisition
227    Undefined,
228}