diff --git a/build.rs b/build.rs index 7cbb7683..3f4a6442 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 30f85263..61208eec 100644 --- a/src/features/app/interface.rs +++ b/src/features/app/interface.rs @@ -1,16 +1,12 @@ 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 crate::features::pb::app::app_server::App; + use super::{service::AppService, use_cases}; -pub mod protobuf { - pub use self::app_server::App; - tonic::include_proto!("ratings.features.common"); - tonic::include_proto!("ratings.features.app"); -} #[tonic::async_trait] impl App for AppService { diff --git a/src/features/app/service.rs b/src/features/app/service.rs index 78067566..ef223907 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 98015974..b83b1d0f 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 86631807..cd7ef294 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 00000000..380ddcd8 --- /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 221b7427..3f9ee13f 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 ab117117..b2962fd4 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 f594dd81..0362e7b5 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 80e7d991..ea5ec0b8 100644 --- a/tests/app_tests/lifecycle_test.rs +++ b/tests/app_tests/lifecycle_test.rs @@ -7,9 +7,11 @@ 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::pb::common::RatingsBand; +use crate::pb::user::AuthenticateResponse; +use crate::helpers::{self, client_app::AppClient}; use crate::helpers::{ - client_user::{pb::AuthenticateResponse, UserClient}, + client_user::UserClient, data_faker, }; diff --git a/tests/helpers/client_app.rs b/tests/helpers/client_app.rs index 155f3373..c5f91bab 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::{GetRatingRequest, GetRatingResponse}; +use crate::pb::app::app_client as pb; #[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 25a99995..957a1e9a 100644 --- a/tests/helpers/client_user.rs +++ b/tests/helpers/client_user.rs @@ -2,12 +2,8 @@ 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, VoteRequest, GetSnapVotesResponse, GetSnapVotesRequest}; #[derive(Debug, Clone)] pub struct UserClient { @@ -25,15 +21,15 @@ impl UserClient { pub async fn authenticate( &self, id: &str, - ) -> Result, Status> { + ) -> 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 509843dd..a3771b46 100644 --- a/tests/helpers/vote_generator.rs +++ b/tests/helpers/vote_generator.rs @@ -1,4 +1,4 @@ -use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest}; +use crate::pb::user::{AuthenticateResponse, VoteRequest}; use super::test_data::TestData; use crate::helpers; diff --git a/tests/mod.rs b/tests/mod.rs index e741ab7c..6136c19d 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 00000000..e19cc5bc --- /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 d413215f..efe89c99 100644 --- a/tests/user_tests/double_authenticate_test.rs +++ b/tests/user_tests/double_authenticate_test.rs @@ -1,7 +1,7 @@ use crate::helpers; use crate::helpers::test_data::TestData; -use super::super::helpers::client_user::pb::AuthenticateResponse; +use crate::pb::user::AuthenticateResponse; use super::super::helpers::client_user::UserClient; use super::super::helpers::with_lifecycle::with_lifecycle; use ratings::app::AppContext; diff --git a/tests/user_tests/get_votes_lifecycle_test.rs b/tests/user_tests/get_votes_lifecycle_test.rs index 421beb3d..000f6812 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, VoteRequest, GetSnapVotesRequest}; + 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 dde156ce..c3d0ab54 100644 --- a/tests/user_tests/simple_lifecycle_test.rs +++ b/tests/user_tests/simple_lifecycle_test.rs @@ -1,7 +1,7 @@ use crate::helpers; use crate::helpers::test_data::TestData; -use super::super::helpers::client_user::pb::{AuthenticateResponse, VoteRequest}; +use crate::pb::user::{AuthenticateResponse, VoteRequest}; use super::super::helpers::client_user::UserClient; use super::super::helpers::with_lifecycle::with_lifecycle; use futures::FutureExt;