Skip to content

Commit

Permalink
Use Uuid instead of String for ApiAccessMethodId
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Sep 26, 2023
1 parent e101291 commit 80ff123
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ mod data {
type Error = FromProtobufTypeError;

fn try_from(value: proto::ApiAccessMethod) -> Result<Self, Self::Error> {
// 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 =
Expand Down Expand Up @@ -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))
}
}

Expand Down
52 changes: 30 additions & 22 deletions mullvad-types/src/api_access.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 80ff123

Please sign in to comment.