diff --git a/mullvad-management-interface/src/types/conversions/api_access_method.rs b/mullvad-management-interface/src/types/conversions/api_access_method.rs index a34faf63dd9d..7e6490504884 100644 --- a/mullvad-management-interface/src/types/conversions/api_access_method.rs +++ b/mullvad-management-interface/src/types/conversions/api_access_method.rs @@ -100,8 +100,12 @@ mod data { type Error = FromProtobufTypeError; fn try_from(value: proto::ApiAccessMethod) -> Result { - // TODO: Should this be used or genertaed anew? - // let id = value.id; + let id = value + .id + .ok_or(FromProtobufTypeError::InvalidArgument( + "Could not deserialize Access Method from protobuf", + ))? + .into(); let name = value.name; let enabled = value.enabled; let access_method = @@ -149,8 +153,7 @@ mod data { } }; - // TODO: Should the `id` be used or generated a new? - Ok(ApiAccessMethod::new(name, enabled, access_method)) + Ok(ApiAccessMethod::with_id(id, name, enabled, access_method)) } } diff --git a/mullvad-types/src/api_access.rs b/mullvad-types/src/api_access.rs index aa15396bcacc..a2643a71507d 100644 --- a/mullvad-types/src/api_access.rs +++ b/mullvad-types/src/api_access.rs @@ -1,8 +1,6 @@ -use std::collections::hash_map::DefaultHasher; use std::str::FromStr; use serde::{Deserialize, Serialize}; -use std::hash::{Hash, Hasher}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4}; /// Daemon settings for API access methods. @@ -85,13 +83,17 @@ pub struct ApiAccessMethod { } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] -pub struct ApiAccessMethodId(String); +pub struct ApiAccessMethodId(uuid::Uuid); impl ApiAccessMethodId { + pub fn new() -> Self { + Self(uuid::Uuid::new_v4()) + } /// It is up to the caller to make sure that the supplied String is actually /// a valid UUID in the context of [`ApiAccessMethod`]s. pub fn from_string(id: String) -> Self { - Self(id) + // TODO: Remove unwrap + Self(uuid::Uuid::from_str(&id).unwrap()) } } @@ -101,23 +103,6 @@ impl std::fmt::Display for ApiAccessMethodId { } } -impl std::ops::Deref for ApiAccessMethodId { - type Target = String; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl From<&AccessMethod> for ApiAccessMethodId { - fn from(value: &AccessMethod) -> Self { - // Generate unqiue ID for this `AccessMethod`. - let mut hasher = DefaultHasher::new(); - value.hash(&mut hasher); - ApiAccessMethodId(hasher.finish().to_string()) - } -} - /// Access Method datastructure. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Hash)] pub enum AccessMethod { @@ -128,7 +113,30 @@ pub enum AccessMethod { impl ApiAccessMethod { pub fn new(name: String, enabled: bool, access_method: AccessMethod) -> Self { Self { - id: ApiAccessMethodId::from(&access_method), + id: ApiAccessMethodId::new(), + name, + enabled, + access_method, + } + } + + /// Just like [`new`], [`with_id`] will create a new [`ApiAccessMethod`]. + /// But instead of automatically generating a new UUID, the id is instead + /// passed as an argument. + /// + /// This is useful when converting to [`ApiAccessMethod`] from other data + /// representations, such as protobuf. + /// + /// [`new`]: ApiAccessMethod::new + /// [`with_id`]: ApiAccessMethod::with_id + pub fn with_id( + id: ApiAccessMethodId, + name: String, + enabled: bool, + access_method: AccessMethod, + ) -> Self { + Self { + id, name, enabled, access_method,