mz_cloud_provider/
lib.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::fmt;
11use std::str::FromStr;
12
13/// Identifies a supported cloud provider.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum CloudProvider {
16    /// A pseudo-provider value used by local development environments.
17    Local,
18    /// A pseudo-provider value used by Docker.
19    Docker,
20    /// A deprecated psuedo-provider value used by mzcompose.
21    // TODO(benesch): remove once v0.39 ships.
22    MzCompose,
23    /// A pseudo-provider value used by cloudtest.
24    Cloudtest,
25    /// Amazon Web Services.
26    Aws,
27    /// Google Cloud Platform
28    Gcp,
29    /// Microsoft Azure
30    Azure,
31    /// Other generic cloud provider
32    Generic,
33}
34
35impl CloudProvider {
36    /// Returns true if this provider actually runs in the cloud
37    pub fn is_cloud(&self) -> bool {
38        matches!(self, Self::Aws | Self::Gcp | Self::Azure | Self::Generic)
39    }
40}
41
42impl fmt::Display for CloudProvider {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            CloudProvider::Local => f.write_str("local"),
46            CloudProvider::Docker => f.write_str("docker"),
47            CloudProvider::MzCompose => f.write_str("mzcompose"),
48            CloudProvider::Cloudtest => f.write_str("cloudtest"),
49            CloudProvider::Aws => f.write_str("aws"),
50            CloudProvider::Gcp => f.write_str("gcp"),
51            CloudProvider::Azure => f.write_str("azure"),
52            CloudProvider::Generic => f.write_str("generic"),
53        }
54    }
55}
56
57impl FromStr for CloudProvider {
58    type Err = InvalidCloudProviderError;
59
60    fn from_str(s: &str) -> Result<CloudProvider, InvalidCloudProviderError> {
61        match s.to_lowercase().as_ref() {
62            "local" => Ok(CloudProvider::Local),
63            "docker" => Ok(CloudProvider::Docker),
64            "mzcompose" => Ok(CloudProvider::MzCompose),
65            "cloudtest" => Ok(CloudProvider::Cloudtest),
66            "aws" => Ok(CloudProvider::Aws),
67            "gcp" => Ok(CloudProvider::Gcp),
68            "azure" => Ok(CloudProvider::Azure),
69            "generic" => Ok(CloudProvider::Generic),
70            _ => Err(InvalidCloudProviderError),
71        }
72    }
73}
74
75/// The error type for [`CloudProvider::from_str`].
76#[derive(Debug, Clone, PartialEq)]
77pub struct InvalidCloudProviderError;
78
79impl fmt::Display for InvalidCloudProviderError {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        f.write_str("invalid cloud provider")
82    }
83}
84
85impl std::error::Error for InvalidCloudProviderError {}