Skip to main content

clevercloud_sdk/v4/addon_provider/
postgresql.rs

1//! # Postgresql addon provider module
2//!
3//! This module provide helpers and structures to interact with the postgresql
4//! addon provider
5#![allow(deprecated)]
6
7use std::{
8    convert::TryFrom,
9    fmt::{self, Debug, Display, Formatter},
10    str::FromStr,
11};
12
13use crate::oauth10a::{ClientError, RestClient};
14#[cfg(feature = "logging")]
15use log::{Level, debug, log_enabled};
16#[cfg(feature = "jsonschemas")]
17use schemars::JsonSchema_repr as JsonSchemaRepr;
18use serde_repr::{Deserialize_repr as DeserializeRepr, Serialize_repr as SerializeRepr};
19
20use crate::{
21    Client,
22    v4::addon_provider::{AddonProvider, AddonProviderId},
23};
24
25// -----------------------------------------------------------------------------
26// Error enumeration
27
28#[derive(thiserror::Error, Debug)]
29pub enum Error {
30    #[error(
31        "failed to parse version from '{0}', available versions are 18, 17, 16, 15, 14, 13, 12 and 11"
32    )]
33    ParseVersion(String),
34    #[error("failed to get information about addon provider '{0}', {1}")]
35    Get(AddonProviderId, ClientError),
36}
37
38// -----------------------------------------------------------------------------
39// Version enum
40
41#[cfg_attr(feature = "jsonschemas", derive(JsonSchemaRepr))]
42#[derive(SerializeRepr, DeserializeRepr, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
43#[serde(untagged)]
44#[repr(i32)]
45pub enum Version {
46    V11 = 11,
47    V12 = 12,
48    V13 = 13,
49    V14 = 14,
50    V15 = 15,
51    V16 = 16,
52    V17 = 17,
53    V18 = 18,
54}
55
56impl FromStr for Version {
57    type Err = Error;
58
59    fn from_str(s: &str) -> Result<Self, Self::Err> {
60        Ok(match s {
61            "18" => Self::V18,
62            "17" => Self::V17,
63            "16" => Self::V16,
64            "15" => Self::V15,
65            "14" => Self::V14,
66            "13" => Self::V13,
67            "12" => Self::V12,
68            "11" => Self::V11,
69            _ => {
70                return Err(Error::ParseVersion(s.to_owned()));
71            }
72        })
73    }
74}
75
76impl TryFrom<String> for Version {
77    type Error = Error;
78
79    fn try_from(s: String) -> Result<Self, Self::Error> {
80        Self::from_str(&s)
81    }
82}
83
84#[allow(clippy::from_over_into)]
85impl Into<String> for Version {
86    fn into(self) -> String {
87        self.to_string()
88    }
89}
90
91impl Display for Version {
92    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
93        match self {
94            Self::V18 => write!(f, "18"),
95            Self::V17 => write!(f, "17"),
96            Self::V16 => write!(f, "16"),
97            Self::V15 => write!(f, "15"),
98            Self::V14 => write!(f, "14"),
99            Self::V13 => write!(f, "13"),
100            Self::V12 => write!(f, "12"),
101            Self::V11 => write!(f, "11"),
102        }
103    }
104}
105
106// -----------------------------------------------------------------------------
107// Helpers functions
108
109#[cfg_attr(feature = "tracing", tracing::instrument)]
110/// returns information about the postgresql addon provider
111pub async fn get(client: &Client) -> Result<AddonProvider<Version>, Error> {
112    let path = format!(
113        "{}/v4/addon-providers/{}",
114        client.endpoint,
115        AddonProviderId::PostgreSql
116    );
117
118    #[cfg(feature = "logging")]
119    if log_enabled!(Level::Debug) {
120        debug!(
121            "execute a request to get information about the postgresql addon-provider, path: '{}', name: '{}'",
122            &path,
123            AddonProviderId::PostgreSql
124        );
125    }
126
127    client
128        .get(&path)
129        .await
130        .map_err(|err| Error::Get(AddonProviderId::PostgreSql, err))
131}
132
133// -----------------------------------------------------------------------------
134// Tests
135
136#[cfg(test)]
137mod tests {
138    use std::str::FromStr;
139
140    use super::Version;
141
142    #[test]
143    fn version_string_round_trip() {
144        for (s, version) in [
145            ("18", Version::V18),
146            ("17", Version::V17),
147            ("16", Version::V16),
148            ("15", Version::V15),
149            ("14", Version::V14),
150            ("13", Version::V13),
151            ("12", Version::V12),
152            ("11", Version::V11),
153        ] {
154            assert_eq!(Version::from_str(s).unwrap(), version);
155            assert_eq!(version.to_string(), s);
156        }
157    }
158
159    #[test]
160    fn version_rejects_unknown() {
161        assert!(Version::from_str("42").is_err());
162    }
163}