-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
region.rs
135 lines (118 loc) · 3.53 KB
/
region.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use http::{uri::InvalidUri, Uri};
use rusoto_core::{region::ParseRegionError, Region};
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use std::convert::TryFrom;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct RegionOrEndpoint {
region: Option<String>,
endpoint: Option<String>,
}
impl RegionOrEndpoint {
pub fn with_region(region: String) -> Self {
Self {
region: Some(region),
endpoint: None,
}
}
pub fn with_endpoint(endpoint: String) -> Self {
Self {
region: None,
endpoint: Some(endpoint),
}
}
}
#[derive(Debug, Snafu)]
pub enum ParseError {
#[snafu(display("Failed to parse custom endpoint as URI: {}", source))]
EndpointParseError { source: InvalidUri },
#[snafu(display("{}", source))]
RegionParseError { source: ParseRegionError },
#[snafu(display("Only one of 'region' or 'endpoint' can be specified"))]
BothRegionAndEndpoint,
#[snafu(display("Must set either 'region' or 'endpoint'"))]
MissingRegionAndEndpoint,
}
impl TryFrom<&RegionOrEndpoint> for Region {
type Error = ParseError;
fn try_from(r: &RegionOrEndpoint) -> Result<Self, Self::Error> {
match (&r.region, &r.endpoint) {
(Some(region), None) => region.parse().context(RegionParseError),
(None, Some(endpoint)) => endpoint
.parse::<Uri>()
.map(|_| Region::Custom {
name: "custom".into(),
endpoint: endpoint.into(),
})
.context(EndpointParseError),
(Some(_), Some(_)) => Err(ParseError::BothRegionAndEndpoint),
(None, None) => Err(ParseError::MissingRegionAndEndpoint),
}
}
}
impl TryFrom<RegionOrEndpoint> for Region {
type Error = ParseError;
fn try_from(r: RegionOrEndpoint) -> Result<Self, Self::Error> {
Region::try_from(&r)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rusoto_core::Region;
use serde::Deserialize;
use std::convert::TryInto;
#[derive(Deserialize)]
struct Config {
inner: Inner,
}
#[derive(Deserialize)]
struct Inner {
#[serde(flatten)]
region: RegionOrEndpoint,
}
#[test]
fn region_es_east_1() {
let config: Config = toml::from_str(
r#"
[inner]
region = "us-east-1"
"#,
)
.unwrap();
let region: Region = config.inner.region.try_into().unwrap();
assert_eq!(region, Region::UsEast1);
}
#[test]
fn region_custom_name_endpoint() {
let config: Config = toml::from_str(
r#"
[inner]
endpoint = "http://localhost:9000"
"#,
)
.unwrap();
let expected_region = Region::Custom {
name: "custom".into(),
endpoint: "http://localhost:9000".into(),
};
let region: Region = config.inner.region.try_into().unwrap();
assert_eq!(region, expected_region);
}
#[test]
fn region_not_provided() {
let config: Config = toml::from_str(
r#"
[inner]
endpoint_is_spelled_wrong = "http://localhost:9000"
"#,
)
.unwrap();
let region: Result<Region, ParseError> = config.inner.region.try_into();
match region {
Err(ParseError::MissingRegionAndEndpoint) => {}
other => panic!("assertion failed, wrong result {:?}", other),
}
}
}