1use bytes::Bytes;
8use futures::TryFutureExt;
9use reqwest::Response;
10
11use crate::pg_enums::{Architecture, OperationSystem};
12use crate::pg_errors::{PgEmbedError, PgEmbedErrorType};
13use crate::pg_types::PgResult;
14
15pub struct PostgresVersion(pub &'static str);
17pub const PG_V15: PostgresVersion = PostgresVersion("15.1.0");
19pub const PG_V14: PostgresVersion = PostgresVersion("14.6.0");
21pub const PG_V13: PostgresVersion = PostgresVersion("13.9.0");
23pub const PG_V12: PostgresVersion = PostgresVersion("12.13.0");
25pub const PG_V11: PostgresVersion = PostgresVersion("11.18.0");
27pub const PG_V10: PostgresVersion = PostgresVersion("10.23.0");
29pub const PG_V9: PostgresVersion = PostgresVersion("9.6.24");
31
32pub struct PgFetchSettings {
34 pub host: String,
36 pub operating_system: OperationSystem,
38 pub architecture: Architecture,
40 pub version: PostgresVersion,
42}
43
44impl Default for PgFetchSettings {
45 fn default() -> Self {
46 PgFetchSettings {
47 host: "https://repo1.maven.org".to_string(),
48 operating_system: OperationSystem::default(),
49 architecture: Architecture::default(),
50 version: PG_V13,
51 }
52 }
53}
54
55impl PgFetchSettings {
56 pub fn platform(&self) -> String {
58 let os = self.operating_system.to_string();
59 let arch = if self.operating_system == OperationSystem::AlpineLinux {
60 format!("{}-{}", self.architecture.to_string(), "alpine")
61 } else {
62 self.architecture.to_string()
63 };
64 format!("{}-{}", os, arch)
65 }
66
67 pub async fn fetch_postgres(&self) -> PgResult<Box<Bytes>> {
73 let platform = &self.platform();
74 let version = self.version.0;
75 let download_url = format!(
76 "{}/maven2/io/zonky/test/postgres/embedded-postgres-binaries-{}/{}/embedded-postgres-binaries-{}-{}.jar",
77 &self.host,
78 &platform,
79 version,
80 &platform,
81 version);
82 let response: Response = reqwest::get(download_url)
83 .map_err(|e| PgEmbedError {
84 error_type: PgEmbedErrorType::DownloadFailure,
85 source: Some(Box::new(e)),
86 message: None,
87 })
88 .await?;
89
90 let content: Bytes = response
91 .bytes()
92 .map_err(|e| PgEmbedError {
93 error_type: PgEmbedErrorType::ConversionFailure,
94 source: Some(Box::new(e)),
95 message: None,
96 })
97 .await?;
98
99 Ok(Box::new(content))
100 }
101}