bunny_api/edge/
storage_zone.rs

1use std::str::FromStr;
2
3/// Storage Zones with their own API endpoints.
4#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5#[allow(missing_docs)]
6pub enum StorageZone {
7    Falkenstein,
8    LosAngeles,
9    NewYork,
10    Singapore,
11    Sydney,
12}
13
14impl StorageZone {
15    /// The root API URL for this Storage Zone.
16    #[must_use]
17    pub fn url(self) -> &'static str {
18        match self {
19            Self::Falkenstein => "storage.bunnycdn.com",
20            Self::LosAngeles => "la.storage.bunnycdn.com",
21            Self::NewYork => "ny.storage.bunnycdn.com",
22            Self::Singapore => "sg.storage.bunnycdn.com",
23            Self::Sydney => "syd.storage.bunnycdn.com",
24        }
25    }
26
27    fn is_falkenstein(s: &str) -> bool {
28         s.eq_ignore_ascii_case("falkenstein") ||
29            s.eq_ignore_ascii_case(Self::Falkenstein.url())
30    }
31
32    fn is_los_angeles(s: &str) -> bool {
33        s.eq_ignore_ascii_case("los angeles") ||
34            s.eq_ignore_ascii_case("losangeles") ||
35            s.eq_ignore_ascii_case("los-angeles") ||
36            s.eq_ignore_ascii_case("los_angeles") ||
37            s.eq_ignore_ascii_case("la") ||
38            s.eq_ignore_ascii_case(Self::LosAngeles.url())
39    }
40
41    fn is_new_york(s: &str) -> bool {
42        s.eq_ignore_ascii_case("new york") ||
43            s.eq_ignore_ascii_case("newyork") ||
44            s.eq_ignore_ascii_case("new-york") ||
45            s.eq_ignore_ascii_case("new_york") ||
46            s.eq_ignore_ascii_case("ny") ||
47            s.eq_ignore_ascii_case(Self::NewYork.url())
48    }
49
50    fn is_singapore(s: &str) -> bool {
51         s.eq_ignore_ascii_case("singapore") ||
52            s.eq_ignore_ascii_case("sg") ||
53            s.eq_ignore_ascii_case(Self::Singapore.url())
54    }
55
56    fn is_sydney(s: &str) -> bool {
57         s.eq_ignore_ascii_case("sydney") ||
58            s.eq_ignore_ascii_case("syd") ||
59            s.eq_ignore_ascii_case(Self::Sydney.url())
60    }
61}
62
63/// Could not convert a [`String`] into a [`StorageZone`].
64#[derive(Debug, thiserror::Error)]
65#[allow(clippy::module_name_repetitions)]
66#[error("unknown storage zone: {0}")]
67pub struct UnknownStorageZone(String);
68
69impl FromStr for StorageZone {
70    type Err = UnknownStorageZone;
71
72    fn from_str(s: &str) -> Result<Self, Self::Err> {
73        if Self::is_falkenstein(s) {
74            Ok(Self::Falkenstein)
75        } else if Self::is_los_angeles(s) {
76            Ok(Self::LosAngeles)
77        } else if Self::is_new_york(s) {
78            Ok(Self::NewYork)
79        } else if Self::is_singapore(s) {
80            Ok(Self::Singapore)
81        } else if Self::is_sydney(s) {
82            Ok(Self::Sydney)
83        } else {
84            Err(UnknownStorageZone(s.to_string()))
85        }
86    }
87}