Skip to content

Commit

Permalink
Remove deprecated uses of from_i32
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Sep 8, 2023
1 parent 4525c4b commit a74e21f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ impl TryFrom<proto::CustomListLocationUpdate>
.location
.ok_or(FromProtobufTypeError::InvalidArgument("missing location"))?,
);
match proto::custom_list_location_update::State::from_i32(custom_list.state) {
Some(proto::custom_list_location_update::State::Add) => Ok(Self::Add {
match proto::custom_list_location_update::State::try_from(custom_list.state) {
Ok(proto::custom_list_location_update::State::Add) => Ok(Self::Add {
name: custom_list.name,
location,
}),
Some(proto::custom_list_location_update::State::Remove) => Ok(Self::Remove {
Ok(proto::custom_list_location_update::State::Remove) => Ok(Self::Remove {
name: custom_list.name,
location,
}),
None => Err(FromProtobufTypeError::InvalidArgument("incorrect state")),
Err(_) => Err(FromProtobufTypeError::InvalidArgument("incorrect state")),
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions mullvad-management-interface/src/types/conversions/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ impl TryFrom<proto::DeviceState> for mullvad_types::device::DeviceState {
type Error = FromProtobufTypeError;

fn try_from(state: proto::DeviceState) -> Result<Self, FromProtobufTypeError> {
let state_type = proto::device_state::State::from_i32(state.state).ok_or(
FromProtobufTypeError::InvalidArgument("invalid device state"),
)?;
let state_type = proto::device_state::State::try_from(state.state)
.map_err(|_| FromProtobufTypeError::InvalidArgument("invalid device state"))?;

match state_type {
proto::device_state::State::LoggedIn => {
Expand Down Expand Up @@ -112,8 +111,8 @@ impl TryFrom<proto::DeviceEvent> for mullvad_types::device::DeviceEvent {
type Error = FromProtobufTypeError;

fn try_from(event: proto::DeviceEvent) -> Result<Self, Self::Error> {
let cause = proto::device_event::Cause::from_i32(event.cause)
.ok_or(FromProtobufTypeError::InvalidArgument("invalid event"))?;
let cause = proto::device_event::Cause::try_from(event.cause)
.map_err(|_| FromProtobufTypeError::InvalidArgument("invalid event"))?;
let cause = mullvad_types::device::DeviceEventCause::from(cause);

let new_state = mullvad_types::device::DeviceState::try_from(event.new_state.ok_or(
Expand Down
28 changes: 13 additions & 15 deletions mullvad-management-interface/src/types/conversions/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ impl TryFrom<proto::TunnelEndpoint> for talpid_types::net::TunnelEndpoint {
)?,
protocol: try_transport_protocol_from_i32(proxy_ep.protocol)?,
},
proxy_type: match proto::ProxyType::from_i32(proxy_ep.proxy_type) {
Some(proto::ProxyType::Shadowsocks) => {
proxy_type: match proto::ProxyType::try_from(proxy_ep.proxy_type) {
Ok(proto::ProxyType::Shadowsocks) => {
talpid_net::proxy::ProxyType::Shadowsocks
}
Some(proto::ProxyType::Custom) => talpid_net::proxy::ProxyType::Custom,
None => {
Ok(proto::ProxyType::Custom) => talpid_net::proxy::ProxyType::Custom,
Err(_) => {
return Err(FromProtobufTypeError::InvalidArgument(
"unknown proxy type",
))
Expand All @@ -97,13 +97,13 @@ impl TryFrom<proto::TunnelEndpoint> for talpid_types::net::TunnelEndpoint {
),
protocol: try_transport_protocol_from_i32(obfs_ep.protocol)?,
},
obfuscation_type: match proto::ObfuscationType::from_i32(
obfuscation_type: match proto::ObfuscationType::try_from(
obfs_ep.obfuscation_type,
) {
Some(proto::ObfuscationType::Udp2tcp) => {
Ok(proto::ObfuscationType::Udp2tcp) => {
talpid_net::ObfuscationType::Udp2Tcp
}
None => {
Err(_) => {
return Err(FromProtobufTypeError::InvalidArgument(
"unknown obfuscation type",
))
Expand Down Expand Up @@ -177,10 +177,10 @@ impl From<proto::IpVersion> for proto::IpVersionConstraint {
pub fn try_tunnel_type_from_i32(
tunnel_type: i32,
) -> Result<talpid_types::net::TunnelType, FromProtobufTypeError> {
match proto::TunnelType::from_i32(tunnel_type) {
Some(proto::TunnelType::Openvpn) => Ok(talpid_types::net::TunnelType::OpenVpn),
Some(proto::TunnelType::Wireguard) => Ok(talpid_types::net::TunnelType::Wireguard),
None => Err(FromProtobufTypeError::InvalidArgument(
match proto::TunnelType::try_from(tunnel_type) {
Ok(proto::TunnelType::Openvpn) => Ok(talpid_types::net::TunnelType::OpenVpn),
Ok(proto::TunnelType::Wireguard) => Ok(talpid_types::net::TunnelType::Wireguard),
Err(_) => Err(FromProtobufTypeError::InvalidArgument(
"invalid tunnel protocol",
)),
}
Expand All @@ -189,9 +189,7 @@ pub fn try_tunnel_type_from_i32(
pub fn try_transport_protocol_from_i32(
protocol: i32,
) -> Result<talpid_types::net::TransportProtocol, FromProtobufTypeError> {
Ok(proto::TransportProtocol::from_i32(protocol)
.ok_or(FromProtobufTypeError::InvalidArgument(
"invalid transport protocol",
))?
Ok(proto::TransportProtocol::try_from(protocol)
.map_err(|_| FromProtobufTypeError::InvalidArgument("invalid transport protocol"))?
.into())
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ impl TryFrom<&proto::WireguardConstraints>
use talpid_types::net;

let ip_version = match &constraints.ip_version {
Some(constraint) => match proto::IpVersion::from_i32(constraint.protocol) {
Some(proto::IpVersion::V4) => Some(net::IpVersion::V4),
Some(proto::IpVersion::V6) => Some(net::IpVersion::V6),
None => {
Some(constraint) => match proto::IpVersion::try_from(constraint.protocol) {
Ok(proto::IpVersion::V4) => Some(net::IpVersion::V4),
Ok(proto::IpVersion::V6) => Some(net::IpVersion::V6),
Err(_) => {
return Err(FromProtobufTypeError::InvalidArgument(
"invalid ip protocol version",
))
Expand Down Expand Up @@ -660,11 +660,11 @@ impl TryFrom<proto::ObfuscationSettings> for mullvad_types::relay_constraints::O
use mullvad_types::relay_constraints::SelectedObfuscation;
use proto::obfuscation_settings::SelectedObfuscation as IpcSelectedObfuscation;
let selected_obfuscation =
match IpcSelectedObfuscation::from_i32(settings.selected_obfuscation) {
Some(IpcSelectedObfuscation::Auto) => SelectedObfuscation::Auto,
Some(IpcSelectedObfuscation::Off) => SelectedObfuscation::Off,
Some(IpcSelectedObfuscation::Udp2tcp) => SelectedObfuscation::Udp2Tcp,
None => {
match IpcSelectedObfuscation::try_from(settings.selected_obfuscation) {
Ok(IpcSelectedObfuscation::Auto) => SelectedObfuscation::Auto,
Ok(IpcSelectedObfuscation::Off) => SelectedObfuscation::Off,
Ok(IpcSelectedObfuscation::Udp2tcp) => SelectedObfuscation::Udp2Tcp,
Err(_) => {
return Err(FromProtobufTypeError::InvalidArgument(
"invalid selected obfuscator",
));
Expand Down Expand Up @@ -709,17 +709,17 @@ impl TryFrom<proto::BridgeState> for mullvad_types::relay_constraints::BridgeSta
type Error = FromProtobufTypeError;

fn try_from(state: proto::BridgeState) -> Result<Self, Self::Error> {
match proto::bridge_state::State::from_i32(state.state) {
Some(proto::bridge_state::State::Auto) => {
match proto::bridge_state::State::try_from(state.state) {
Ok(proto::bridge_state::State::Auto) => {
Ok(mullvad_types::relay_constraints::BridgeState::Auto)
}
Some(proto::bridge_state::State::On) => {
Ok(proto::bridge_state::State::On) => {
Ok(mullvad_types::relay_constraints::BridgeState::On)
}
Some(proto::bridge_state::State::Off) => {
Ok(proto::bridge_state::State::Off) => {
Ok(mullvad_types::relay_constraints::BridgeState::Off)
}
None => Err(FromProtobufTypeError::InvalidArgument(
Err(_) => Err(FromProtobufTypeError::InvalidArgument(
"invalid bridge state",
)),
}
Expand Down Expand Up @@ -758,11 +758,9 @@ pub fn try_providers_constraint_from_proto(
pub fn try_ownership_constraint_from_i32(
ownership: i32,
) -> Result<Constraint<mullvad_types::relay_constraints::Ownership>, FromProtobufTypeError> {
proto::Ownership::from_i32(ownership)
proto::Ownership::try_from(ownership)
.map(ownership_constraint_from_proto)
.ok_or(FromProtobufTypeError::InvalidArgument(
"invalid ownership argument",
))
.map_err(|_| FromProtobufTypeError::InvalidArgument("invalid ownership argument"))
}

pub fn ownership_constraint_from_proto(
Expand Down
20 changes: 9 additions & 11 deletions mullvad-management-interface/src/types/conversions/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,15 @@ impl TryFrom<proto::Settings> for mullvad_types::settings::Settings {
pub fn try_bridge_state_from_i32(
bridge_state: i32,
) -> Result<mullvad_types::relay_constraints::BridgeState, FromProtobufTypeError> {
match proto::bridge_state::State::from_i32(bridge_state) {
Some(proto::bridge_state::State::Auto) => {
match proto::bridge_state::State::try_from(bridge_state) {
Ok(proto::bridge_state::State::Auto) => {
Ok(mullvad_types::relay_constraints::BridgeState::Auto)
}
Some(proto::bridge_state::State::On) => {
Ok(mullvad_types::relay_constraints::BridgeState::On)
}
Some(proto::bridge_state::State::Off) => {
Ok(proto::bridge_state::State::On) => Ok(mullvad_types::relay_constraints::BridgeState::On),
Ok(proto::bridge_state::State::Off) => {
Ok(mullvad_types::relay_constraints::BridgeState::Off)
}
None => Err(FromProtobufTypeError::InvalidArgument(
Err(_) => Err(FromProtobufTypeError::InvalidArgument(
"invalid bridge state",
)),
}
Expand Down Expand Up @@ -286,10 +284,10 @@ impl TryFrom<proto::DnsOptions> for mullvad_types::settings::DnsOptions {
DnsState as MullvadDnsState,
};

let state = match proto::dns_options::DnsState::from_i32(options.state) {
Some(proto::dns_options::DnsState::Default) => MullvadDnsState::Default,
Some(proto::dns_options::DnsState::Custom) => MullvadDnsState::Custom,
None => {
let state = match proto::dns_options::DnsState::try_from(options.state) {
Ok(proto::dns_options::DnsState::Default) => MullvadDnsState::Default,
Ok(proto::dns_options::DnsState::Custom) => MullvadDnsState::Custom,
Err(_) => {
return Err(FromProtobufTypeError::InvalidArgument(
"invalid DNS options state",
))
Expand Down
50 changes: 24 additions & 26 deletions mullvad-management-interface/src/types/conversions/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ impl From<mullvad_types::auth_failed::AuthFailed> for proto::error_state::AuthFa
fn try_auth_failed_from_i32(
auth_failed_error: i32,
) -> Result<mullvad_types::auth_failed::AuthFailed, FromProtobufTypeError> {
proto::error_state::AuthFailedError::from_i32(auth_failed_error)
proto::error_state::AuthFailedError::try_from(auth_failed_error)
.map(mullvad_types::auth_failed::AuthFailed::from)
.ok_or(FromProtobufTypeError::InvalidArgument(
"invalid auth failed error",
))
.map_err(|_| FromProtobufTypeError::InvalidArgument("invalid auth failed error"))
}

impl From<proto::error_state::AuthFailedError> for mullvad_types::auth_failed::AuthFailed {
Expand Down Expand Up @@ -221,14 +219,14 @@ impl TryFrom<proto::TunnelState> for mullvad_types::states::TunnelState {
Some(proto::tunnel_state::State::Disconnecting(
proto::tunnel_state::Disconnecting { after_disconnect },
)) => MullvadState::Disconnecting(
match proto::AfterDisconnect::from_i32(after_disconnect) {
Some(proto::AfterDisconnect::Nothing) => {
match proto::AfterDisconnect::try_from(after_disconnect) {
Ok(proto::AfterDisconnect::Nothing) => {
talpid_tunnel::ActionAfterDisconnect::Nothing
}
Some(proto::AfterDisconnect::Block) => {
Ok(proto::AfterDisconnect::Block) => {
talpid_tunnel::ActionAfterDisconnect::Block
}
Some(proto::AfterDisconnect::Reconnect) => {
Ok(proto::AfterDisconnect::Reconnect) => {
talpid_tunnel::ActionAfterDisconnect::Reconnect
}
_ => {
Expand All @@ -248,23 +246,23 @@ impl TryFrom<proto::TunnelState> for mullvad_types::states::TunnelState {
policy_error,
}),
})) => {
let cause = match proto::error_state::Cause::from_i32(cause) {
Some(proto::error_state::Cause::AuthFailed) => {
let cause = match proto::error_state::Cause::try_from(cause) {
Ok(proto::error_state::Cause::AuthFailed) => {
let auth_failed = try_auth_failed_from_i32(auth_failed_error)?;
talpid_tunnel::ErrorStateCause::AuthFailed(Some(
auth_failed.as_str().to_string(),
))
}
Some(proto::error_state::Cause::Ipv6Unavailable) => {
Ok(proto::error_state::Cause::Ipv6Unavailable) => {
talpid_tunnel::ErrorStateCause::Ipv6Unavailable
}
Some(proto::error_state::Cause::IsOffline) => {
Ok(proto::error_state::Cause::IsOffline) => {
talpid_tunnel::ErrorStateCause::IsOffline
}
Some(proto::error_state::Cause::SetDnsError) => {
Ok(proto::error_state::Cause::SetDnsError) => {
talpid_tunnel::ErrorStateCause::SetDnsError
}
Some(proto::error_state::Cause::SetFirewallPolicyError) => {
Ok(proto::error_state::Cause::SetFirewallPolicyError) => {
let policy_error = policy_error.ok_or(
FromProtobufTypeError::InvalidArgument("missing firewall policy error"),
)?;
Expand All @@ -275,27 +273,27 @@ impl TryFrom<proto::TunnelState> for mullvad_types::states::TunnelState {
)?;
talpid_tunnel::ErrorStateCause::SetFirewallPolicyError(policy_error)
}
Some(proto::error_state::Cause::StartTunnelError) => {
Ok(proto::error_state::Cause::StartTunnelError) => {
talpid_tunnel::ErrorStateCause::StartTunnelError
}
Some(proto::error_state::Cause::TunnelParameterError) => {
let parameter_error = match proto::error_state::GenerationError::from_i32(parameter_error) {
Some(proto::error_state::GenerationError::CustomTunnelHostResolutionError) => talpid_tunnel::ParameterGenerationError::CustomTunnelHostResultionError,
Some(proto::error_state::GenerationError::NoMatchingBridgeRelay) => talpid_tunnel::ParameterGenerationError::NoMatchingBridgeRelay,
Some(proto::error_state::GenerationError::NoMatchingRelay) => talpid_tunnel::ParameterGenerationError::NoMatchingRelay,
Some(proto::error_state::GenerationError::NoWireguardKey) => talpid_tunnel::ParameterGenerationError::NoWireguardKey,
Ok(proto::error_state::Cause::TunnelParameterError) => {
let parameter_error = match proto::error_state::GenerationError::try_from(parameter_error) {
Ok(proto::error_state::GenerationError::CustomTunnelHostResolutionError) => talpid_tunnel::ParameterGenerationError::CustomTunnelHostResultionError,
Ok(proto::error_state::GenerationError::NoMatchingBridgeRelay) => talpid_tunnel::ParameterGenerationError::NoMatchingBridgeRelay,
Ok(proto::error_state::GenerationError::NoMatchingRelay) => talpid_tunnel::ParameterGenerationError::NoMatchingRelay,
Ok(proto::error_state::GenerationError::NoWireguardKey) => talpid_tunnel::ParameterGenerationError::NoWireguardKey,
_ => return Err(FromProtobufTypeError::InvalidArgument(
"invalid parameter error",
)),
};
talpid_tunnel::ErrorStateCause::TunnelParameterError(parameter_error)
}
#[cfg(target_os = "android")]
Some(proto::error_state::Cause::VpnPermissionDenied) => {
Ok(proto::error_state::Cause::VpnPermissionDenied) => {
talpid_tunnel::ErrorStateCause::VpnPermissionDenied
}
#[cfg(target_os = "windows")]
Some(proto::error_state::Cause::SplitTunnelError) => {
Ok(proto::error_state::Cause::SplitTunnelError) => {
talpid_tunnel::ErrorStateCause::SplitTunnelError
}
_ => {
Expand Down Expand Up @@ -334,12 +332,12 @@ fn try_firewall_policy_error_from_i32(
lock_pid: u32,
lock_name: String,
) -> Result<talpid_types::tunnel::FirewallPolicyError, FromProtobufTypeError> {
match proto::error_state::firewall_policy_error::ErrorType::from_i32(policy_error) {
Some(proto::error_state::firewall_policy_error::ErrorType::Generic) => {
match proto::error_state::firewall_policy_error::ErrorType::try_from(policy_error) {
Ok(proto::error_state::firewall_policy_error::ErrorType::Generic) => {
Ok(talpid_types::tunnel::FirewallPolicyError::Generic)
}
#[cfg(windows)]
Some(proto::error_state::firewall_policy_error::ErrorType::Locked) => {
Ok(proto::error_state::firewall_policy_error::ErrorType::Locked) => {
let blocking_app = option_from_proto_string(lock_name).map(|name| {
talpid_types::tunnel::BlockingApplication {
pid: lock_pid,
Expand Down
Loading

0 comments on commit a74e21f

Please sign in to comment.