Skip to content

Commit

Permalink
less boilerplate by using derive_more
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Dec 22, 2024
1 parent 95fc4dd commit a7ef71b
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 421 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ serde_json = "1.0.133"
diesel = { version = "2.2.6", features = ["chrono", "r2d2", "numeric"] }
diesel_migrations = "2.2.0"
diesel_logger = { version = "0.4.0", optional = true }

derive_more = { version = "1.0.0", features = ["from", "into", "as_ref", "deref", "display"] }
diesel-derive-newtype = "2.1.2"

# Bundled/Static SQLite
Expand Down
2 changes: 1 addition & 1 deletion src/api/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ async fn _user_api_key_login(
let Some(client_user_uuid) = client_id.strip_prefix("user.") else {
err!("Malformed client_id", format!("IP: {}.", ip.ip))
};
let client_user_uuid: UserId = client_user_uuid.to_string().into();
let client_user_uuid: UserId = client_user_uuid.into();
let Some(user) = User::find_by_uuid(&client_user_uuid, conn).await else {
err!("Invalid client_id", format!("IP: {}.", ip.ip))
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl WSEntryMapGuard {
impl Drop for WSEntryMapGuard {
fn drop(&mut self) {
info!("Closing WS connection from {}", self.addr);
if let Some(mut entry) = self.users.map.get_mut(&self.user_uuid.to_string()) {
if let Some(mut entry) = self.users.map.get_mut(self.user_uuid.as_ref()) {
entry.retain(|(uuid, _)| uuid != &self.entry_uuid);
}
}
Expand Down
40 changes: 2 additions & 38 deletions src/db/models/attachment.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::io::ErrorKind;

use bigdecimal::{BigDecimal, ToPrimitive};
use derive_more::{AsRef, Deref, Display};
use rocket::request::FromParam;
use serde_json::Value;
use std::{
borrow::Borrow,
fmt::{Display, Formatter},
ops::Deref,
};

use super::{CipherId, OrganizationId, UserId};
use crate::CONFIG;
Expand Down Expand Up @@ -234,41 +230,9 @@ impl Attachment {
}
}

#[derive(DieselNewType, FromForm, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, AsRef, Deref, DieselNewType, Display, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachmentId(pub String);

impl AsRef<str> for AttachmentId {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Deref for AttachmentId {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Borrow<str> for AttachmentId {
fn borrow(&self) -> &str {
&self.0
}
}

impl Display for AttachmentId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<String> for AttachmentId {
fn from(raw: String) -> Self {
Self(raw)
}
}

impl<'r> FromParam<'r> for AttachmentId {
type Error = ();

Expand Down
42 changes: 4 additions & 38 deletions src/db/models/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::util::LowerCase;
use crate::CONFIG;
use chrono::{NaiveDateTime, TimeDelta, Utc};
use derive_more::{AsRef, Deref, Display, From};
use rocket::request::FromParam;
use serde_json::Value;
use std::{
borrow::Borrow,
fmt::{Display, Formatter},
ops::Deref,
};

use super::{
Attachment, CollectionCipher, CollectionId, Favorite, FolderCipher, FolderId, Group, Membership, MembershipStatus,
Expand Down Expand Up @@ -1034,41 +1030,11 @@ impl Cipher {
}
}

#[derive(DieselNewType, FromForm, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
)]
pub struct CipherId(String);

impl AsRef<str> for CipherId {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Deref for CipherId {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Borrow<str> for CipherId {
fn borrow(&self) -> &str {
&self.0
}
}

impl Display for CipherId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<String> for CipherId {
fn from(raw: String) -> Self {
Self(raw)
}
}

impl<'r> FromParam<'r> for CipherId {
type Error = ();

Expand Down
42 changes: 4 additions & 38 deletions src/db/models/collection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use derive_more::{AsRef, Deref, Display, From};
use rocket::request::FromParam;
use serde_json::Value;
use std::{
borrow::Borrow,
fmt::{Display, Formatter},
ops::Deref,
};

use super::{
CipherId, CollectionGroup, GroupUser, Membership, MembershipStatus, MembershipType, OrganizationId, User, UserId,
Expand Down Expand Up @@ -790,41 +786,11 @@ impl CollectionUser {
}
}

#[derive(DieselNewType, FromForm, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(
Clone, Debug, AsRef, Deref, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize,
)]
pub struct CollectionId(String);

impl AsRef<str> for CollectionId {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Deref for CollectionId {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Borrow<str> for CollectionId {
fn borrow(&self) -> &str {
&self.0
}
}

impl Display for CollectionId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<String> for CollectionId {
fn from(raw: String) -> Self {
Self(raw)
}
}

impl<'r> FromParam<'r> for CollectionId {
type Error = ();

Expand Down
101 changes: 29 additions & 72 deletions src/db/models/device.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use chrono::{NaiveDateTime, Utc};
use derive_more::{Display, From};
use rocket::request::FromParam;
use std::{
borrow::Borrow,
fmt::{Display, Formatter},
ops::Deref,
};

use super::UserId;
use crate::{crypto, CONFIG};
use core::fmt;

db_object! {
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
Expand Down Expand Up @@ -250,68 +245,62 @@ impl Device {
}
}

#[derive(Display)]
pub enum DeviceType {
#[display("Android")]
Android = 0,
#[display("iOS")]
Ios = 1,
#[display("Chrome Extension")]
ChromeExtension = 2,
#[display("Firefox Extension")]
FirefoxExtension = 3,
#[display("Opera Extension")]
OperaExtension = 4,
#[display("Edge Extension")]
EdgeExtension = 5,
#[display("Windows")]
WindowsDesktop = 6,
#[display("macOS")]
MacOsDesktop = 7,
#[display("Linux")]
LinuxDesktop = 8,
#[display("Chrome")]
ChromeBrowser = 9,
#[display("Firefox")]
FirefoxBrowser = 10,
#[display("Opera")]
OperaBrowser = 11,
#[display("Edge")]
EdgeBrowser = 12,
#[display("Internet Explorer")]
IEBrowser = 13,
#[display("Unknown Browser")]
UnknownBrowser = 14,
#[display("Android")]
AndroidAmazon = 15,
#[display("UWP")]
Uwp = 16,
#[display("Safari")]
SafariBrowser = 17,
#[display("Vivaldi")]
VivaldiBrowser = 18,
#[display("Vivaldi Extension")]
VivaldiExtension = 19,
#[display("Safari Extension")]
SafariExtension = 20,
#[display("SDK")]
Sdk = 21,
#[display("Server")]
Server = 22,
#[display("Windows CLI")]
WindowsCLI = 23,
#[display("macOS CLI")]
MacOsCLI = 24,
#[display("Linux CLI")]
LinuxCLI = 25,
}

impl Display for DeviceType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
DeviceType::Android => write!(f, "Android"),
DeviceType::Ios => write!(f, "iOS"),
DeviceType::ChromeExtension => write!(f, "Chrome Extension"),
DeviceType::FirefoxExtension => write!(f, "Firefox Extension"),
DeviceType::OperaExtension => write!(f, "Opera Extension"),
DeviceType::EdgeExtension => write!(f, "Edge Extension"),
DeviceType::WindowsDesktop => write!(f, "Windows"),
DeviceType::MacOsDesktop => write!(f, "macOS"),
DeviceType::LinuxDesktop => write!(f, "Linux"),
DeviceType::ChromeBrowser => write!(f, "Chrome"),
DeviceType::FirefoxBrowser => write!(f, "Firefox"),
DeviceType::OperaBrowser => write!(f, "Opera"),
DeviceType::EdgeBrowser => write!(f, "Edge"),
DeviceType::IEBrowser => write!(f, "Internet Explorer"),
DeviceType::UnknownBrowser => write!(f, "Unknown Browser"),
DeviceType::AndroidAmazon => write!(f, "Android"),
DeviceType::Uwp => write!(f, "UWP"),
DeviceType::SafariBrowser => write!(f, "Safari"),
DeviceType::VivaldiBrowser => write!(f, "Vivaldi"),
DeviceType::VivaldiExtension => write!(f, "Vivaldi Extension"),
DeviceType::SafariExtension => write!(f, "Safari Extension"),
DeviceType::Sdk => write!(f, "SDK"),
DeviceType::Server => write!(f, "Server"),
DeviceType::WindowsCLI => write!(f, "Windows CLI"),
DeviceType::MacOsCLI => write!(f, "macOS CLI"),
DeviceType::LinuxCLI => write!(f, "Linux CLI"),
}
}
}

impl DeviceType {
pub fn from_i32(value: i32) -> DeviceType {
match value {
Expand Down Expand Up @@ -346,7 +335,7 @@ impl DeviceType {
}
}

#[derive(DieselNewType, FromForm, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, DieselNewType, Display, From, FromForm, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceId(String);

impl DeviceId {
Expand All @@ -355,38 +344,6 @@ impl DeviceId {
}
}

impl AsRef<str> for DeviceId {
fn as_ref(&self) -> &str {
&self.0
}
}

impl Deref for DeviceId {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Borrow<str> for DeviceId {
fn borrow(&self) -> &str {
&self.0
}
}

impl Display for DeviceId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<String> for DeviceId {
fn from(raw: String) -> Self {
Self(raw)
}
}

impl<'r> FromParam<'r> for DeviceId {
type Error = ();

Expand Down
Loading

0 comments on commit a7ef71b

Please sign in to comment.