Skip to content

Commit

Permalink
[lib] Add full feature for lib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
krendelhoff2 committed Apr 23, 2024
1 parent c445c4d commit c552734
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
6 changes: 1 addition & 5 deletions src/Cargo.lock

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

28 changes: 19 additions & 9 deletions src/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,41 @@ 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"
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"]
11 changes: 8 additions & 3 deletions src/lib/src/error.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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,
Expand All @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions src/lib/src/model/user.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/lib/src/utils/token.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/web-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c552734

Please sign in to comment.