diff --git a/.gitignore b/.gitignore index b50ab41..9b3ef28 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ *.rock *.nix +proto/*.rs venv/ build/ *.charm diff --git a/build.rs b/build.rs index 7cbb768..2bb6c96 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,10 @@ +use std::path::Path; + fn main() -> Result<(), Box> { + // Define the path to the output directory within the `src` folder + let out_dir = Path::new("proto"); + std::fs::create_dir_all(out_dir)?; + let descriptor_set_path = format!( "{}/ratings_descriptor.bin", std::env::var("OUT_DIR").unwrap() @@ -14,6 +20,7 @@ fn main() -> Result<(), Box> { tonic_build::configure() .build_server(true) .file_descriptor_set_path(descriptor_set_path) + .out_dir(out_dir) .compile(files, &["proto"])?; Ok(()) diff --git a/src/features/app/interface.rs b/src/features/app/interface.rs index 30f8526..cb4da56 100644 --- a/src/features/app/interface.rs +++ b/src/features/app/interface.rs @@ -1,16 +1,11 @@ use crate::app::AppContext; -use self::protobuf::{App, GetRatingRequest, GetRatingResponse}; -pub use protobuf::app_server; +use crate::features::pb::app::{GetRatingRequest, GetRatingResponse}; use tonic::{Request, Response, Status}; -use super::{service::AppService, use_cases}; +use crate::features::pb::app::app_server::App; -pub mod protobuf { - pub use self::app_server::App; - tonic::include_proto!("ratings.features.common"); - tonic::include_proto!("ratings.features.app"); -} +use super::{service::AppService, use_cases}; #[tonic::async_trait] impl App for AppService { diff --git a/src/features/app/service.rs b/src/features/app/service.rs index 7806756..ef22390 100644 --- a/src/features/app/service.rs +++ b/src/features/app/service.rs @@ -1,4 +1,4 @@ -use super::interface::app_server::AppServer; +use crate::features::pb::app::app_server::AppServer; #[derive(Debug, Default)] pub struct AppService; diff --git a/src/features/common/entities.rs b/src/features/common/entities.rs index 9801597..b83b1d0 100644 --- a/src/features/common/entities.rs +++ b/src/features/common/entities.rs @@ -1,8 +1,6 @@ use sqlx::FromRow; -pub mod protobuf { - tonic::include_proto!("ratings.features.common"); -} +use crate::features::pb::common as pb; const INSUFFICIENT_VOTES_QUANTITY: usize = 25; @@ -55,8 +53,8 @@ impl Rating { } } - pub(crate) fn into_dto(self) -> protobuf::Rating { - protobuf::Rating { + pub(crate) fn into_dto(self) -> pb::Rating { + pb::Rating { snap_id: self.snap_id, total_votes: self.total_votes, ratings_band: self.ratings_band as i32, diff --git a/src/features/mod.rs b/src/features/mod.rs index 8663180..cd7ef29 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -1,3 +1,4 @@ pub mod app; mod common; +mod pb; pub mod user; diff --git a/src/features/pb.rs b/src/features/pb.rs new file mode 100644 index 0000000..380ddcd --- /dev/null +++ b/src/features/pb.rs @@ -0,0 +1,11 @@ +pub mod app { + include!("../../proto/ratings.features.app.rs"); +} + +pub mod common { + include!("../../proto/ratings.features.common.rs"); +} + +pub mod user { + include!("../../proto/ratings.features.user.rs"); +} diff --git a/src/features/user/entities.rs b/src/features/user/entities.rs index 221b742..3f9ee13 100644 --- a/src/features/user/entities.rs +++ b/src/features/user/entities.rs @@ -1,7 +1,7 @@ use sqlx::FromRow; use time::OffsetDateTime; -use super::interface::protobuf; +use crate::features::pb::user; pub type ClientHash = String; @@ -35,13 +35,13 @@ pub struct Vote { } impl Vote { - pub(crate) fn into_dto(self) -> protobuf::Vote { + pub(crate) fn into_dto(self) -> user::Vote { let timestamp = Some(prost_types::Timestamp { seconds: self.timestamp.unix_timestamp(), nanos: 0, }); - protobuf::Vote { + user::Vote { snap_id: self.snap_id, snap_revision: self.snap_revision as i32, vote_up: self.vote_up, diff --git a/src/features/user/interface.rs b/src/features/user/interface.rs index ab11711..b2962fd 100644 --- a/src/features/user/interface.rs +++ b/src/features/user/interface.rs @@ -2,8 +2,6 @@ use time::OffsetDateTime; use tonic::{Request, Response, Status}; -pub use protobuf::user_server; - use crate::app::AppContext; use crate::utils::jwt::Claims; @@ -11,16 +9,12 @@ use super::entities::Vote; use super::service::UserService; use super::use_cases; -use self::protobuf::{ +use crate::features::pb::user::{ AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse, - ListMyVotesRequest, ListMyVotesResponse, User, VoteRequest, + ListMyVotesRequest, ListMyVotesResponse, VoteRequest, }; -pub mod protobuf { - pub use self::user_server::User; - - tonic::include_proto!("ratings.features.user"); -} +use crate::features::pb::user::user_server::User; #[tonic::async_trait] impl User for UserService { diff --git a/src/features/user/service.rs b/src/features/user/service.rs index f594dd8..0362e7b 100644 --- a/src/features/user/service.rs +++ b/src/features/user/service.rs @@ -1,4 +1,4 @@ -use super::interface::user_server::UserServer; +use crate::features::pb::user::user_server::UserServer; #[derive(Debug, Default)] pub struct UserService; diff --git a/tests/app_tests/lifecycle_test.rs b/tests/app_tests/lifecycle_test.rs index 80e7d99..98f627a 100644 --- a/tests/app_tests/lifecycle_test.rs +++ b/tests/app_tests/lifecycle_test.rs @@ -7,11 +7,10 @@ use ratings::{ use super::super::helpers::with_lifecycle::with_lifecycle; use crate::helpers::test_data::TestData; use crate::helpers::vote_generator::generate_votes; -use crate::helpers::{self, client_app::pb::RatingsBand, client_app::AppClient}; -use crate::helpers::{ - client_user::{pb::AuthenticateResponse, UserClient}, - data_faker, -}; +use crate::helpers::{self, client_app::AppClient}; +use crate::helpers::{client_user::UserClient, data_faker}; +use crate::pb::common::RatingsBand; +use crate::pb::user::AuthenticateResponse; #[tokio::test] async fn app_lifecycle_test() -> Result<(), Box> { diff --git a/tests/helpers/client_app.rs b/tests/helpers/client_app.rs index 155f337..09133c1 100644 --- a/tests/helpers/client_app.rs +++ b/tests/helpers/client_app.rs @@ -1,10 +1,7 @@ use tonic::{metadata::MetadataValue, transport::Endpoint, Request, Response, Status}; -pub mod pb { - pub use self::app_client::AppClient; - - tonic::include_proto!("ratings.features.app"); -} +use crate::pb::app::app_client as pb; +use crate::pb::app::{GetRatingRequest, GetRatingResponse}; #[derive(Debug, Clone)] pub struct AppClient { @@ -22,7 +19,7 @@ impl AppClient { &self, id: &str, token: &str, - ) -> Result, Status> { + ) -> Result, Status> { let channel = Endpoint::from_shared(self.url.clone()) .unwrap() .connect() @@ -34,7 +31,7 @@ impl AppClient { Ok(req) }); client - .get_rating(pb::GetRatingRequest { + .get_rating(GetRatingRequest { snap_id: id.to_string(), }) .await diff --git a/tests/helpers/client_user.rs b/tests/helpers/client_user.rs index 25a9999..c967f2a 100644 --- a/tests/helpers/client_user.rs +++ b/tests/helpers/client_user.rs @@ -2,12 +2,11 @@ use tonic::metadata::MetadataValue; use tonic::transport::Endpoint; use tonic::{Request, Response, Status}; -use self::pb::GetSnapVotesResponse; -pub mod pb { - pub use self::user_client::UserClient; - - tonic::include_proto!("ratings.features.user"); -} +use crate::pb::user::user_client as pb; +use crate::pb::user::{ + AuthenticateRequest, AuthenticateResponse, GetSnapVotesRequest, GetSnapVotesResponse, + VoteRequest, +}; #[derive(Debug, Clone)] pub struct UserClient { @@ -22,18 +21,15 @@ impl UserClient { } #[allow(dead_code)] - pub async fn authenticate( - &self, - id: &str, - ) -> Result, Status> { + pub async fn authenticate(&self, id: &str) -> Result, Status> { let mut client = pb::UserClient::connect(self.url.clone()).await.unwrap(); client - .authenticate(pb::AuthenticateRequest { id: id.to_string() }) + .authenticate(AuthenticateRequest { id: id.to_string() }) .await } #[allow(dead_code)] - pub async fn vote(&self, token: &str, ballet: pb::VoteRequest) -> Result, Status> { + pub async fn vote(&self, token: &str, ballet: VoteRequest) -> Result, Status> { let channel = Endpoint::from_shared(self.url.clone()) .unwrap() .connect() @@ -51,7 +47,7 @@ impl UserClient { pub async fn get_snap_votes( &self, token: &str, - request: pb::GetSnapVotesRequest, + request: GetSnapVotesRequest, ) -> Result, Status> { let channel = Endpoint::from_shared(self.url.clone()) .unwrap() diff --git a/tests/helpers/vote_generator.rs b/tests/helpers/vote_generator.rs index 509843d..0ad012c 100644 --- a/tests/helpers/vote_generator.rs +++ b/tests/helpers/vote_generator.rs @@ -1,6 +1,6 @@ -use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest}; use super::test_data::TestData; use crate::helpers; +use crate::pb::user::{AuthenticateResponse, VoteRequest}; pub async fn generate_votes( snap_id: &str, diff --git a/tests/mod.rs b/tests/mod.rs index e741ab7..6136c19 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -10,3 +10,5 @@ mod app_tests { } mod helpers; + +mod pb; diff --git a/tests/pb.rs b/tests/pb.rs new file mode 100644 index 0000000..e19cc5b --- /dev/null +++ b/tests/pb.rs @@ -0,0 +1,11 @@ +pub mod app { + include!("../proto/ratings.features.app.rs"); +} + +pub mod common { + include!("../proto/ratings.features.common.rs"); +} + +pub mod user { + include!("../proto/ratings.features.user.rs"); +} diff --git a/tests/user_tests/double_authenticate_test.rs b/tests/user_tests/double_authenticate_test.rs index d413215..0012eaf 100644 --- a/tests/user_tests/double_authenticate_test.rs +++ b/tests/user_tests/double_authenticate_test.rs @@ -1,9 +1,9 @@ use crate::helpers; use crate::helpers::test_data::TestData; -use super::super::helpers::client_user::pb::AuthenticateResponse; use super::super::helpers::client_user::UserClient; use super::super::helpers::with_lifecycle::with_lifecycle; +use crate::pb::user::AuthenticateResponse; use ratings::app::AppContext; use ratings::utils::{self, Infrastructure}; use sqlx::Row; diff --git a/tests/user_tests/get_votes_lifecycle_test.rs b/tests/user_tests/get_votes_lifecycle_test.rs index 421beb3..4e8ff54 100644 --- a/tests/user_tests/get_votes_lifecycle_test.rs +++ b/tests/user_tests/get_votes_lifecycle_test.rs @@ -1,8 +1,8 @@ use crate::helpers; -use crate::helpers::client_user::pb::GetSnapVotesRequest; use crate::helpers::test_data::TestData; -use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest}; +use crate::pb::user::{AuthenticateResponse, GetSnapVotesRequest, VoteRequest}; + use super::super::helpers::client_user::UserClient; use super::super::helpers::with_lifecycle::with_lifecycle; use futures::FutureExt; diff --git a/tests/user_tests/simple_lifecycle_test.rs b/tests/user_tests/simple_lifecycle_test.rs index dde156c..4cf8ba0 100644 --- a/tests/user_tests/simple_lifecycle_test.rs +++ b/tests/user_tests/simple_lifecycle_test.rs @@ -1,9 +1,9 @@ use crate::helpers; use crate::helpers::test_data::TestData; -use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest}; use super::super::helpers::client_user::UserClient; use super::super::helpers::with_lifecycle::with_lifecycle; +use crate::pb::user::{AuthenticateResponse, VoteRequest}; use futures::FutureExt; use ratings::app::AppContext; use ratings::utils::{self, Infrastructure};