summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/location.rs
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2021-04-23 19:16:39 +0200
committerDavid Lönnhager <david.l@mullvad.net>2021-05-17 11:08:50 +0200
commit68b0e6fdbd21c5fb7152e66da88a7983a7e4a855 (patch)
tree16fe3523e1e110448ee07571089349898544043f /mullvad-cli/src/location.rs
parent03bea638ea4be5b1b1faf1a173f49866d5c50d52 (diff)
downloadmullvadvpn-68b0e6fdbd21c5fb7152e66da88a7983a7e4a855.tar.xz
mullvadvpn-68b0e6fdbd21c5fb7152e66da88a7983a7e4a855.zip
Add exit location CLI option
Diffstat (limited to 'mullvad-cli/src/location.rs')
-rw-r--r--mullvad-cli/src/location.rs27
1 files changed, 19 insertions, 8 deletions
diff --git a/mullvad-cli/src/location.rs b/mullvad-cli/src/location.rs
index 09f39720ba..cadeaa24fe 100644
--- a/mullvad-cli/src/location.rs
+++ b/mullvad-cli/src/location.rs
@@ -22,11 +22,22 @@ pub fn get_subcommand() -> clap::App<'static, 'static> {
)
}
-pub fn get_constraint(matches: &clap::ArgMatches<'_>) -> RelayLocation {
- let country_original = matches.value_of("country").unwrap();
+pub fn get_constraint_from_args(matches: &clap::ArgMatches<'_>) -> RelayLocation {
+ let country = matches.value_of("country").unwrap();
+ let city = matches.value_of("city");
+ let hostname = matches.value_of("hostname");
+ get_constraint(country, city, hostname)
+}
+
+pub fn get_constraint<T: AsRef<str>>(
+ country: T,
+ city: Option<T>,
+ hostname: Option<T>,
+) -> RelayLocation {
+ let country_original = country.as_ref();
let country = country_original.to_lowercase();
- let city = matches.value_of("city").map(str::to_lowercase);
- let hostname = matches.value_of("hostname").map(str::to_lowercase);
+ let city = city.map(|s| s.as_ref().to_lowercase());
+ let hostname = hostname.map(|s| s.as_ref().to_lowercase());
match (country_original, city, hostname) {
("any", None, None) => RelayLocation::default(),
@@ -81,16 +92,16 @@ pub fn format_providers(providers: &Vec<String>) -> String {
}
}
-fn country_code_validator(code: String) -> std::result::Result<(), String> {
- if code.len() == 2 || code == "any" {
+pub fn country_code_validator<T: AsRef<str>>(code: T) -> std::result::Result<(), String> {
+ if code.as_ref().len() == 2 || code.as_ref() == "any" {
Ok(())
} else {
Err(String::from("Country codes must be two letters, or 'any'."))
}
}
-fn city_code_validator(code: String) -> std::result::Result<(), String> {
- if code.len() == 3 {
+pub fn city_code_validator<T: AsRef<str>>(code: T) -> std::result::Result<(), String> {
+ if code.as_ref().len() == 3 {
Ok(())
} else {
Err(String::from("City codes must be three letters"))