From c55273430f7b4c87f5f0b21b4a2bd24d96204d35 Mon Sep 17 00:00:00 2001 From: Savely Krendelhoff Date: Tue, 23 Apr 2024 20:32:21 +0530 Subject: [PATCH] [lib] Add `full` feature for lib Problem: `lib` crate is intended to be shared across all other crates, thou some deps and trait impls make sense only for specific use cases. For example, websocket client crate wouldn't ever need sqlx trait impls. Solution: Add `full` feature to be able to opt out unnecessary deps if they are not needed. --- src/Cargo.lock | 6 +----- src/lib/Cargo.toml | 28 +++++++++++++++++++--------- src/lib/src/error.rs | 11 ++++++++--- src/lib/src/model/user.rs | 16 +++++++++------- src/lib/src/utils/token.rs | 5 ++++- src/web-api/Cargo.toml | 2 +- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index 9f3a434..1afc414 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -1793,16 +1793,12 @@ dependencies = [ name = "lib" version = "0.1.0" dependencies = [ - "actix-web", - "anyhow", "argon2", "chrono", "derive_more", - "env_logger", - "fake", - "futures-util", "jsonwebtoken", "proptest", + "rand_core 0.6.4", "serde", "serde_json", "sqlx", diff --git a/src/lib/Cargo.toml b/src/lib/Cargo.toml index f555a73..12ab1de 100644 --- a/src/lib/Cargo.toml +++ b/src/lib/Cargo.toml @@ -5,18 +5,18 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dev-dependencies] +proptest = "1.4.0" + [dependencies] -actix-web = "4.4.1" -anyhow = "1.0.79" argon2 = "0.5.2" derive_more = "0.99.17" -env_logger = "0.10.1" -fake = "2.9.2" -futures-util = "0.3.30" jsonwebtoken = "9.2.0" -proptest = "1.4.0" serde_json = "1.0.111" -utoipa = { version = "4.2.0", features = ["chrono", "uuid", "actix_extras"] } + +[dependencies.rand_core] +version = "0.6.4" +features = ["getrandom"] [dependencies.uuid] version = "1.6.1" @@ -24,12 +24,22 @@ features = ["serde", "v4"] [dependencies.chrono] version = "0.4.31" -features = [ "serde" ] +features = ["serde"] [dependencies.serde] version = "1.0.194" -features = [ "derive" ] +features = ["derive"] + +[dependencies.utoipa] +version = "4.2.0" +features = ["chrono", "uuid", "actix_extras"] +optional = true [dependencies.sqlx] version = "0.7.3" features = ["tls-native-tls", "runtime-async-std", "postgres", "chrono", "uuid"] +optional = true + +[features] +default = [] +full = ["dep:utoipa", "dep:sqlx"] diff --git a/src/lib/src/error.rs b/src/lib/src/error.rs index 92cf8f3..076fa54 100644 --- a/src/lib/src/error.rs +++ b/src/lib/src/error.rs @@ -1,9 +1,12 @@ use derive_more::Display; use serde::Serialize; use std::fmt; + +#[cfg(feature = "full")] use utoipa::ToSchema; -#[derive(Debug, Display, Copy, Clone, Serialize, ToSchema)] +#[derive(Debug, Display, Copy, Clone, Serialize)] +#[cfg_attr(feature = "full", derive(ToSchema))] #[serde(rename_all = "snake_case")] pub enum Status { #[display(fmt = "success")] @@ -22,7 +25,8 @@ where serializer.collect_str(value) } -#[derive(Debug, Serialize, Clone, ToSchema)] +#[derive(Debug, Serialize, Clone)] +#[cfg_attr(feature = "full", derive(ToSchema))] pub struct Response { #[serde(serialize_with = "use_display")] pub status: Status, @@ -36,7 +40,8 @@ impl fmt::Display for Response { } } -#[derive(Debug, PartialEq, Display, Clone, ToSchema)] +#[derive(Debug, PartialEq, Display, Clone)] +#[cfg_attr(feature = "full", derive(ToSchema))] pub enum Error { #[display(fmt = "Server Error. Please try again later")] ServerError, diff --git a/src/lib/src/model/user.rs b/src/lib/src/model/user.rs index 8b2d58e..81be140 100644 --- a/src/lib/src/model/user.rs +++ b/src/lib/src/model/user.rs @@ -1,16 +1,17 @@ - use chrono::prelude::*; use derive_more::Display; use serde::{Deserialize, Serialize}; -use utoipa::ToSchema; - +#[cfg(feature = "full")] +use utoipa::ToSchema; -#[derive( - Debug, Deserialize, Serialize, Clone, Copy, sqlx::Type, PartialEq, Display, Eq, Hash, ToSchema, +#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Display, Eq, Hash)] +#[cfg_attr(feature = "full", derive(sqlx::Type, ToSchema))] +#[cfg_attr( + feature = "full", + sqlx(type_name = "user_role", rename_all = "lowercase") )] -#[sqlx(type_name = "user_role", rename_all = "lowercase")] #[serde(rename_all = "snake_case")] pub enum UserRole { #[display(fmt = "admin")] @@ -35,7 +36,8 @@ impl UserRole { } } -#[derive(Debug, Deserialize, sqlx::FromRow, sqlx::Type, Serialize, Clone, ToSchema)] +#[derive(Debug, Deserialize, Serialize, Clone)] +#[cfg_attr(feature = "full", derive(ToSchema, sqlx::FromRow, sqlx::Type))] pub struct User { pub id: uuid::Uuid, pub username: String, diff --git a/src/lib/src/utils/token.rs b/src/lib/src/utils/token.rs index f8cebb1..1a85b9d 100644 --- a/src/lib/src/utils/token.rs +++ b/src/lib/src/utils/token.rs @@ -1,9 +1,12 @@ use chrono::{Duration, Utc}; use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation}; use serde::{Deserialize, Serialize}; + +#[cfg(feature = "full")] use utoipa::ToSchema; -#[derive(Debug, Serialize, Deserialize, ToSchema)] +#[derive(Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "full", derive(ToSchema))] pub struct Claims { pub sub: String, pub iat: usize, diff --git a/src/web-api/Cargo.toml b/src/web-api/Cargo.toml index bbef5e5..7ef118d 100644 --- a/src/web-api/Cargo.toml +++ b/src/web-api/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lib = { path = "../lib" } +lib = { path = "../lib", features = ["full"] } actix-web = "4.4.1" anyhow = "1.0.79" argon2 = "0.5.2"