Skip to content

Commit 577a353

Browse files
committed
Simplify stuff
1 parent a0613ab commit 577a353

File tree

6 files changed

+68
-41
lines changed

6 files changed

+68
-41
lines changed

mullvad-types/src/constraints/constraint.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use jnix::{FromJava, IntoJava};
55
use serde::{Deserialize, Serialize};
66
use std::{fmt, str::FromStr};
77

8-
use crate::Intersection;
8+
use crate::{Intersection, IntoProto, TryFromProto};
99

1010
/// Limits the set of [`crate::relay_list::Relay`]s that a `RelaySelector` may select.
1111
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
@@ -18,6 +18,23 @@ pub enum Constraint<T> {
1818
Only(T),
1919
}
2020

21+
impl<T: IntoProto<S>, S> IntoProto<Option<S>> for Constraint<T> {
22+
fn into_proto(self) -> Option<S> {
23+
self.map(|v| v.into_proto()).option()
24+
}
25+
}
26+
27+
impl<T: TryFromProto<S>, S> TryFromProto<Option<S>> for Constraint<T> {
28+
fn try_from_proto(
29+
other: Option<S>,
30+
) -> Result<Self, crate::proto::types::FromProtobufTypeError> {
31+
other
32+
.map(|v| TryFromProto::try_from_proto(v))
33+
.transpose()
34+
.map(Constraint::from)
35+
}
36+
}
37+
2138
impl<T: Intersection> Intersection for Constraint<T> {
2239
/// Define the intersection between two arbitrary [`Constraint`]s.
2340
///
@@ -46,6 +63,16 @@ impl<T: fmt::Display> fmt::Display for Constraint<T> {
4663
}
4764
}
4865

66+
impl<T, E> Constraint<Result<T, E>> {
67+
pub fn transpose(self) -> Result<Constraint<T>, E> {
68+
match self {
69+
Constraint::Any => Ok(Constraint::Any),
70+
Constraint::Only(Ok(value)) => Ok(Constraint::Only(value)),
71+
Constraint::Only(Err(err)) => Err(err),
72+
}
73+
}
74+
}
75+
4976
impl<T> Constraint<T> {
5077
pub fn unwrap(self) -> T {
5178
match self {

mullvad-types/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,18 @@ impl<T: TryFromProto<S> + Ord, S> TryFromProto<Vec<S>> for std::collections::BTr
169169
.collect()
170170
}
171171
}
172+
173+
// There's no u16 type, so try to truncate a u32
174+
impl TryFromProto<u32> for u16 {
175+
fn try_from_proto(other: u32) -> Result<Self, crate::proto::types::FromProtobufTypeError> {
176+
u16::try_from(other).map_err(|_| {
177+
crate::proto::types::FromProtobufTypeError::InvalidArgument("integer too large")
178+
})
179+
}
180+
}
181+
182+
impl IntoProto<u32> for u16 {
183+
fn into_proto(self) -> u32 {
184+
u32::from(self)
185+
}
186+
}

mullvad-types/src/proto/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl MullvadProxyClient {
268268
}
269269

270270
pub async fn set_obfuscation_settings(&mut self, settings: ObfuscationSettings) -> Result<()> {
271-
let settings = types::ObfuscationSettings::from(&settings);
271+
let settings = types::ObfuscationSettings::from(settings);
272272
self.0
273273
.set_obfuscation_settings(settings)
274274
.await

mullvad-types/src/proto/types/conversions/relay_constraints.rs

+7-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::proto::types::{
44
use crate::{
55
constraints::Constraint, custom_list::Id, relay_constraints::GeographicLocationConstraint,
66
};
7+
use crate::{IntoProto, TryFromProto};
78
use std::str::FromStr;
89
use talpid_types::net::proxy::CustomProxy;
910

@@ -26,7 +27,7 @@ impl TryFrom<&types::WireguardConstraints> for crate::relay_constraints::Wiregua
2627
};
2728

2829
Ok(mullvad_constraints::WireguardConstraints {
29-
port: Constraint::from(constraints.port.map(|port| port as u16)),
30+
port: TryFromProto::try_from_proto(constraints.port)?,
3031
ip_version: Constraint::from(ip_version),
3132
use_multihop: constraints.use_multihop,
3233
entry_location: constraints
@@ -143,8 +144,8 @@ impl From<crate::relay_constraints::BridgeState> for types::BridgeState {
143144
}
144145
}
145146

146-
impl From<&crate::relay_constraints::ObfuscationSettings> for types::ObfuscationSettings {
147-
fn from(settings: &crate::relay_constraints::ObfuscationSettings) -> Self {
147+
impl From<crate::relay_constraints::ObfuscationSettings> for types::ObfuscationSettings {
148+
fn from(settings: crate::relay_constraints::ObfuscationSettings) -> Self {
148149
use crate::relay_constraints::SelectedObfuscation;
149150
let selected_obfuscation = i32::from(match settings.selected_obfuscation {
150151
SelectedObfuscation::Auto => types::obfuscation_settings::SelectedObfuscation::Auto,
@@ -155,23 +156,7 @@ impl From<&crate::relay_constraints::ObfuscationSettings> for types::Obfuscation
155156
});
156157
Self {
157158
selected_obfuscation,
158-
udp2tcp: Some(types::Udp2TcpObfuscationSettings::from(&settings.udp2tcp)),
159-
}
160-
}
161-
}
162-
163-
impl From<crate::relay_constraints::ObfuscationSettings> for types::ObfuscationSettings {
164-
fn from(settings: crate::relay_constraints::ObfuscationSettings) -> Self {
165-
types::ObfuscationSettings::from(&settings)
166-
}
167-
}
168-
169-
impl From<&crate::relay_constraints::Udp2TcpObfuscationSettings>
170-
for types::Udp2TcpObfuscationSettings
171-
{
172-
fn from(settings: &crate::relay_constraints::Udp2TcpObfuscationSettings) -> Self {
173-
Self {
174-
port: settings.port.map(u32::from).option(),
159+
udp2tcp: Some(settings.udp2tcp.into_proto()),
175160
}
176161
}
177162
}
@@ -441,7 +426,7 @@ impl TryFrom<types::ObfuscationSettings> for crate::relay_constraints::Obfuscati
441426

442427
let udp2tcp = match settings.udp2tcp {
443428
Some(settings) => {
444-
crate::relay_constraints::Udp2TcpObfuscationSettings::try_from(&settings)?
429+
crate::relay_constraints::Udp2TcpObfuscationSettings::try_from(settings)?
445430
}
446431
None => {
447432
return Err(FromProtobufTypeError::InvalidArgument(
@@ -457,18 +442,6 @@ impl TryFrom<types::ObfuscationSettings> for crate::relay_constraints::Obfuscati
457442
}
458443
}
459444

460-
impl TryFrom<&types::Udp2TcpObfuscationSettings>
461-
for crate::relay_constraints::Udp2TcpObfuscationSettings
462-
{
463-
type Error = FromProtobufTypeError;
464-
465-
fn try_from(settings: &types::Udp2TcpObfuscationSettings) -> Result<Self, Self::Error> {
466-
Ok(Self {
467-
port: Constraint::from(settings.port.map(|port| port as u16)),
468-
})
469-
}
470-
}
471-
472445
impl TryFrom<types::BridgeState> for crate::relay_constraints::BridgeState {
473446
type Error = FromProtobufTypeError;
474447

@@ -490,7 +463,7 @@ impl TryFrom<types::TransportPort> for crate::relay_constraints::TransportPort {
490463
fn try_from(port: types::TransportPort) -> Result<Self, Self::Error> {
491464
Ok(crate::relay_constraints::TransportPort {
492465
protocol: super::net::try_transport_protocol_from_i32(port.protocol)?,
493-
port: Constraint::from(port.port.map(|port| port as u16)),
466+
port: TryFromProto::try_from_proto(port.port)?,
494467
})
495468
}
496469
}

mullvad-types/src/proto/types/conversions/settings.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::proto::types::{self, FromProtobufTypeError};
22
use crate::settings::CURRENT_SETTINGS_VERSION;
3-
use crate::IntoProto;
3+
use crate::{IntoProto, TryFromProto};
44
use talpid_types::ErrorExt;
55

66
impl From<&crate::settings::Settings> for types::Settings {
@@ -37,7 +37,7 @@ impl From<&crate::settings::Settings> for types::Settings {
3737
tunnel_options: Some(types::TunnelOptions::from(&settings.tunnel_options)),
3838
show_beta_releases: settings.show_beta_releases,
3939
obfuscation_settings: Some(types::ObfuscationSettings::from(
40-
&settings.obfuscation_settings,
40+
settings.obfuscation_settings.clone(),
4141
)),
4242
split_tunnel,
4343
custom_lists: Some(settings.custom_lists.to_owned().into_proto()),
@@ -247,10 +247,10 @@ impl TryFrom<types::TunnelOptions> for crate::settings::TunnelOptions {
247247

248248
Ok(Self {
249249
openvpn: net::openvpn::TunnelOptions {
250-
mssfix: openvpn_options.mssfix.map(|mssfix| mssfix as u16),
250+
mssfix: TryFromProto::try_from_proto(openvpn_options.mssfix)?,
251251
},
252252
wireguard: crate::wireguard::TunnelOptions {
253-
mtu: wireguard_options.mtu.map(|mtu| mtu as u16),
253+
mtu: TryFromProto::try_from_proto(wireguard_options.mtu)?,
254254
rotation_interval: wireguard_options
255255
.rotation_interval
256256
.map(std::time::Duration::try_from)

mullvad-types/src/relay_constraints.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
relay_list::Relay,
99
CustomTunnelEndpoint, Intersection,
1010
};
11+
use crate::{IntoProto, TryFromProto};
1112
#[cfg(target_os = "android")]
1213
use jnix::{jni::objects::JObject, FromJava, IntoJava, JnixEnv};
1314
use serde::{Deserialize, Serialize};
@@ -626,7 +627,18 @@ impl fmt::Display for SelectedObfuscation {
626627
}
627628
}
628629

629-
#[derive(Default, Debug, Clone, Eq, PartialEq, Deserialize, Serialize, Intersection)]
630+
#[derive(
631+
Default,
632+
Debug,
633+
Clone,
634+
Eq,
635+
PartialEq,
636+
Deserialize,
637+
Serialize,
638+
Intersection,
639+
TryFromProto,
640+
IntoProto,
641+
)]
630642
#[cfg_attr(target_os = "android", derive(IntoJava))]
631643
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
632644
#[serde(rename_all = "snake_case")]

0 commit comments

Comments
 (0)