diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-10-10 12:52:39 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-10-17 10:47:33 +0200 |
| commit | 5a2c3dca12def7db39383c2143a542ce2cdf49e5 (patch) | |
| tree | 3818a8254512d795be82b61111163a46f16cc777 | |
| parent | 86777eb87505855851462817f6672194ecbc2e54 (diff) | |
| download | mullvadvpn-5a2c3dca12def7db39383c2143a542ce2cdf49e5.tar.xz mullvadvpn-5a2c3dca12def7db39383c2143a542ce2cdf49e5.zip | |
Add conversion from gRPC type to GeoIpLocation
| -rw-r--r-- | mullvad-management-interface/src/types.rs | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/mullvad-management-interface/src/types.rs b/mullvad-management-interface/src/types.rs index 0f70d11a91..8b259e0722 100644 --- a/mullvad-management-interface/src/types.rs +++ b/mullvad-management-interface/src/types.rs @@ -1,7 +1,11 @@ pub use prost_types::{Duration, Timestamp}; use mullvad_types::relay_constraints::Constraint; -use std::convert::TryFrom; +use std::{ + convert::TryFrom, + net::{Ipv4Addr, Ipv6Addr}, + str::FromStr, +}; use talpid_types::{net::wireguard, ErrorExt}; #[allow(clippy::derive_partial_eq_without_eq)] @@ -29,6 +33,51 @@ impl From<mullvad_types::location::GeoIpLocation> for GeoIpLocation { } } +impl TryFrom<GeoIpLocation> for mullvad_types::location::GeoIpLocation { + type Error = FromProtobufTypeError; + + fn try_from(geoip: GeoIpLocation) -> Result<Self, Self::Error> { + Ok(mullvad_types::location::GeoIpLocation { + ipv4: match geoip.ipv4 { + addr if addr.is_empty() => None, + addr => Some(Ipv4Addr::from_str(&addr).map_err(|_err| { + FromProtobufTypeError::InvalidArgument("invalid IPv4 address") + })?), + }, + ipv6: match geoip.ipv6 { + addr if addr.is_empty() => None, + addr => Some(Ipv6Addr::from_str(&addr).map_err(|_err| { + FromProtobufTypeError::InvalidArgument("invalid IPv6 address") + })?), + }, + country: geoip.country, + city: match geoip.city { + city if city.is_empty() => None, + city => Some(city), + }, + latitude: geoip.latitude, + longitude: geoip.longitude, + mullvad_exit_ip: geoip.mullvad_exit_ip, + hostname: match geoip.hostname { + host if host.is_empty() => None, + host => Some(host), + }, + bridge_hostname: match geoip.bridge_hostname { + host if host.is_empty() => None, + host => Some(host), + }, + entry_hostname: match geoip.entry_hostname { + host if host.is_empty() => None, + host => Some(host), + }, + obfuscator_hostname: match geoip.obfuscator_hostname { + host if host.is_empty() => None, + host => Some(host), + }, + }) + } +} + impl From<talpid_types::net::TunnelEndpoint> for TunnelEndpoint { fn from(endpoint: talpid_types::net::TunnelEndpoint) -> Self { use talpid_types::net; |
