summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/relay_constraints.rs
blob: 97555997fcd1c8cf8a65c554c6598687a71b5659 (plain)
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
use clap::Args;
use mullvad_types::{
    constraints::Constraint,
    location::{CityCode, CountryCode, Hostname},
    relay_constraints::{GeographicLocationConstraint, LocationConstraint},
};

#[derive(Args, Debug, Clone)]
pub struct LocationArgs {
    /// A two-letter country code, or 'any'.
    pub country: CountryCode,
    /// A three-letter city code.
    pub city: Option<CityCode>,
    /// A host name, such as "se-got-wg-101".
    pub hostname: Option<Hostname>,
}

impl From<LocationArgs> for Constraint<GeographicLocationConstraint> {
    fn from(value: LocationArgs) -> Self {
        if value.country.eq_ignore_ascii_case("any") {
            return Constraint::Any;
        }

        Constraint::Only(match (value.country, value.city, value.hostname) {
            (country, None, None) => GeographicLocationConstraint::Country(country),
            (country, Some(city), None) => GeographicLocationConstraint::City(country, city),
            (country, Some(city), Some(hostname)) => {
                GeographicLocationConstraint::Hostname(country, city, hostname)
            }

            _ => unreachable!("invalid location arguments"),
        })
    }
}

impl From<LocationArgs> for Constraint<LocationConstraint> {
    fn from(value: LocationArgs) -> Self {
        if value.country.eq_ignore_ascii_case("any") {
            return Constraint::Any;
        }

        let location = match (value.country, value.city, value.hostname) {
            (country, None, None) => GeographicLocationConstraint::Country(country),
            (country, Some(city), None) => GeographicLocationConstraint::City(country, city),
            (country, Some(city), Some(hostname)) => {
                GeographicLocationConstraint::Hostname(country, city, hostname)
            }
            _ => unreachable!("invalid location arguments"),
        };
        Constraint::Only(LocationConstraint::Location(location))
    }
}