zesty_api/
location.rs

1//
2// Copyright 2023 Zesty Tech Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use std::fmt;
7use std::str::FromStr;
8
9use serde_with::{DeserializeFromStr, SerializeDisplay};
10use thiserror::Error;
11
12pub use aws::AwsRegion;
13pub use azure::AzureRegion;
14pub use gcp::GcpRegion;
15
16mod aws;
17mod azure;
18mod gcp;
19mod impls;
20
21#[derive(Debug, Error)]
22#[error("Invalid region specified: {0}")]
23pub struct InvalidRegion(String);
24
25impl InvalidRegion {
26    fn new(text: impl Into<String>) -> Self {
27        Self(text.into())
28    }
29}
30
31#[derive(Clone, Copy, Debug, SerializeDisplay, DeserializeFromStr)]
32#[non_exhaustive]
33pub enum Location {
34    Aws(AwsRegion),
35    Azure(AzureRegion),
36    Gcp(GcpRegion),
37}
38
39trait CloudLocation {
40    const CLOUD_VENDOR: &'static str;
41    const CLOUD_VENDOR_PREFIX: &'static str;
42
43    fn normal_region(text: &str) -> String {
44        text.strip_prefix(Self::CLOUD_VENDOR_PREFIX)
45            .unwrap_or(text)
46            .to_lowercase()
47    }
48}
49
50#[derive(Debug, Error)]
51pub enum ParseError {
52    #[error("Ambiguous region, use either {0}")]
53    AmbiguousLocation(String),
54    #[error("Unknown region: {0}")]
55    UnknownLocation(String),
56}
57
58impl ParseError {
59    fn ambiguous(candidates: &[&str]) -> Self {
60        let text = candidates.join(" or ");
61        Self::AmbiguousLocation(text)
62    }
63
64    fn unknown(text: &str) -> Self {
65        Self::UnknownLocation(text.to_string())
66    }
67}