Add request, response and event types
Browse files at this commit0dfb213a5278bbb6889df63a125de9efd6ef2c09Parent: f5d5359- feat
- add request, response and event types
10 changed files +394 -0
- crates/common/src/event.rs +14 -0
- crates/common/src/request.rs +41 -0
- crates/common/src/request/community.rs +82 -0
- crates/common/src/request/message.rs +98 -0
- crates/common/src/request/user.rs +79 -0
- crates/common/src/request/user_community.rs +53 -0
- crates/common/src/response.rs +27 -0
- crates/common/src/response/community.rs +0 -0
- crates/common/src/response/message.rs +0 -0
- crates/common/src/response/user.rs +0 -0
crates/common/src/event.rsadded+14 -0
| @@ -0,0 +1,14 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::{community::Community, message::Message, user::User}; | ||
| 4 | + | ||
| 5 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 6 | +pub enum Event { | ||
| 7 | + MessageCreate(Message), | ||
| 8 | + MessageUpdate(Message), | ||
| 9 | + MessageDelete(Message), | ||
| 10 | + UserJoin(Community, User), | ||
| 11 | + UserLeave(Community, User), | ||
| 12 | + CommunityUpdate(Community), | ||
| 13 | + CommunityDelete(Community), | ||
| 14 | +} | ||
crates/common/src/request.rsadded+41 -0
| @@ -0,0 +1,41 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::request::{ | ||
| 4 | + community::{CreateCommunity, DeleteCommunity, ReadCommunity, UpdateCommunity}, | ||
| 5 | + message::{CreateMessage, DeleteMessage, ReadMessage, UpdateMessage}, | ||
| 6 | + user::{CreateUser, DeleteUser, ReadUser, UpdateUser}, | ||
| 7 | + user_community::{JoinCommunity, LeaveCommunity}, | ||
| 8 | +}; | ||
| 9 | + | ||
| 10 | +pub mod community; | ||
| 11 | +pub mod message; | ||
| 12 | +pub mod user; | ||
| 13 | +pub mod user_community; | ||
| 14 | + | ||
| 15 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 16 | +pub struct Authentication(String); | ||
| 17 | + | ||
| 18 | +impl Authentication { | ||
| 19 | + pub fn get_authentication_token(&self) -> &str { | ||
| 20 | + &self.0 | ||
| 21 | + } | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 25 | +pub enum Request { | ||
| 26 | + Authentication(Authentication), | ||
| 27 | + CreateUser(CreateUser), | ||
| 28 | + ReadUser(ReadUser), | ||
| 29 | + UpdateUser(UpdateUser), | ||
| 30 | + DeleteUser(DeleteUser), | ||
| 31 | + CreateCommunity(CreateCommunity), | ||
| 32 | + ReadCommunity(ReadCommunity), | ||
| 33 | + UpdateCommunity(UpdateCommunity), | ||
| 34 | + DeleteCommunity(DeleteCommunity), | ||
| 35 | + CreateMessage(CreateMessage), | ||
| 36 | + ReadMessage(ReadMessage), | ||
| 37 | + UpdateMessage(UpdateMessage), | ||
| 38 | + DeleteMessage(DeleteMessage), | ||
| 39 | + JoinCommunity(JoinCommunity), | ||
| 40 | + LeaveCommunity(LeaveCommunity), | ||
| 41 | +} | ||
crates/common/src/request/community.rsadded+82 -0
| @@ -0,0 +1,82 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::{error::Error, validate::validate_community_name}; | ||
| 4 | + | ||
| 5 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 6 | +pub struct CreateCommunity { | ||
| 7 | + community_name: String, | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +impl CreateCommunity { | ||
| 11 | + pub fn new(community_name: &str) -> Result<Self, Error> { | ||
| 12 | + let community_name = community_name.to_string(); | ||
| 13 | + validate_community_name(&community_name)?; | ||
| 14 | + | ||
| 15 | + Ok(Self { community_name }) | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + pub fn get_community_name(&self) -> &str { | ||
| 19 | + &self.community_name | ||
| 20 | + } | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 24 | +pub struct ReadCommunity { | ||
| 25 | + community_id: String, | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +impl ReadCommunity { | ||
| 29 | + pub fn new(community_id: &str) -> Self { | ||
| 30 | + let community_id = community_id.to_string(); | ||
| 31 | + | ||
| 32 | + Self { community_id } | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + pub fn get_community_id(&self) -> &str { | ||
| 36 | + &self.community_id | ||
| 37 | + } | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 41 | +pub struct UpdateCommunity { | ||
| 42 | + community_id: String, | ||
| 43 | + community_name: String, | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +impl UpdateCommunity { | ||
| 47 | + pub fn new(community_id: &str, community_name: &str) -> Result<Self, Error> { | ||
| 48 | + let community_id = community_id.to_string(); | ||
| 49 | + let community_name = community_name.to_string(); | ||
| 50 | + validate_community_name(&community_name)?; | ||
| 51 | + | ||
| 52 | + Ok(Self { | ||
| 53 | + community_id, | ||
| 54 | + community_name, | ||
| 55 | + }) | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + pub fn get_community_id(&self) -> &str { | ||
| 59 | + &self.community_id | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + pub fn get_community_name(&self) -> &str { | ||
| 63 | + &self.community_name | ||
| 64 | + } | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 68 | +pub struct DeleteCommunity { | ||
| 69 | + community_id: String, | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | +impl DeleteCommunity { | ||
| 73 | + pub fn new(community_id: &str) -> Self { | ||
| 74 | + let community_id = community_id.to_string(); | ||
| 75 | + | ||
| 76 | + Self { community_id } | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + pub fn get_community_id(&self) -> &str { | ||
| 80 | + &self.community_id | ||
| 81 | + } | ||
| 82 | +} | ||
crates/common/src/request/message.rsadded+98 -0
| @@ -0,0 +1,98 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::{error::Error, validate::validate_message_body}; | ||
| 4 | + | ||
| 5 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 6 | +pub struct CreateMessage { | ||
| 7 | + community_id: String, | ||
| 8 | + user_id: String, | ||
| 9 | + message_body: String, | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +impl CreateMessage { | ||
| 13 | + pub fn new(community_id: &str, user_id: &str, message_body: &str) -> Result<Self, Error> { | ||
| 14 | + let community_id = community_id.to_string(); | ||
| 15 | + let user_id = user_id.to_string(); | ||
| 16 | + let message_body = message_body.to_string(); | ||
| 17 | + validate_message_body(&message_body)?; | ||
| 18 | + | ||
| 19 | + Ok(Self { | ||
| 20 | + community_id, | ||
| 21 | + user_id, | ||
| 22 | + message_body, | ||
| 23 | + }) | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + pub fn get_community_id(&self) -> &str { | ||
| 27 | + &self.community_id | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + pub fn get_user_id(&self) -> &str { | ||
| 31 | + &self.user_id | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + pub fn get_message_body(&self) -> &str { | ||
| 35 | + &self.message_body | ||
| 36 | + } | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 40 | +pub struct ReadMessage { | ||
| 41 | + message_id: String, | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +impl ReadMessage { | ||
| 45 | + pub fn new(message_id: &str) -> Self { | ||
| 46 | + let message_id = message_id.to_string(); | ||
| 47 | + | ||
| 48 | + Self { message_id } | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + pub fn get_message_id(&self) -> &str { | ||
| 52 | + &self.message_id | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 57 | +pub struct UpdateMessage { | ||
| 58 | + message_id: String, | ||
| 59 | + message_body: String, | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +impl UpdateMessage { | ||
| 63 | + pub fn new(message_id: &str, message_body: &str) -> Result<Self, Error> { | ||
| 64 | + let message_id = message_id.to_string(); | ||
| 65 | + let message_body = message_body.to_string(); | ||
| 66 | + validate_message_body(&message_body)?; | ||
| 67 | + | ||
| 68 | + Ok(Self { | ||
| 69 | + message_id, | ||
| 70 | + message_body, | ||
| 71 | + }) | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + pub fn get_message_id(&self) -> &str { | ||
| 75 | + &self.message_id | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + pub fn get_message_body(&self) -> &str { | ||
| 79 | + &self.message_body | ||
| 80 | + } | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 84 | +pub struct DeleteMessage { | ||
| 85 | + message_id: String, | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +impl DeleteMessage { | ||
| 89 | + pub fn new(message_id: &str) -> Self { | ||
| 90 | + let message_id = message_id.to_string(); | ||
| 91 | + | ||
| 92 | + Self { message_id } | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + pub fn get_message_id(&self) -> &str { | ||
| 96 | + &self.message_id | ||
| 97 | + } | ||
| 98 | +} | ||
crates/common/src/request/user.rsadded+79 -0
| @@ -0,0 +1,79 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::{error::Error, validate::validate_username}; | ||
| 4 | + | ||
| 5 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 6 | +pub struct CreateUser { | ||
| 7 | + username: String, | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +impl CreateUser { | ||
| 11 | + pub fn new(username: &str) -> Result<Self, Error> { | ||
| 12 | + let username = username.to_string(); | ||
| 13 | + validate_username(&username)?; | ||
| 14 | + | ||
| 15 | + Ok(Self { username }) | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + pub fn get_username(&self) -> &str { | ||
| 19 | + &self.username | ||
| 20 | + } | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 24 | +pub struct ReadUser { | ||
| 25 | + user_id: String, | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +impl ReadUser { | ||
| 29 | + pub fn new(user_id: &str) -> Self { | ||
| 30 | + let user_id = user_id.to_string(); | ||
| 31 | + | ||
| 32 | + Self { user_id } | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + pub fn get_user_id(&self) -> &str { | ||
| 36 | + &self.user_id | ||
| 37 | + } | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 41 | +pub struct UpdateUser { | ||
| 42 | + user_id: String, | ||
| 43 | + username: String, | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +impl UpdateUser { | ||
| 47 | + pub fn new(user_id: &str, username: &str) -> Result<Self, Error> { | ||
| 48 | + let user_id = user_id.to_string(); | ||
| 49 | + let username = username.to_string(); | ||
| 50 | + validate_username(&username)?; | ||
| 51 | + | ||
| 52 | + Ok(Self { user_id, username }) | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + pub fn get_user_id(&self) -> &str { | ||
| 56 | + &self.user_id | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + pub fn get_username(&self) -> &str { | ||
| 60 | + &self.username | ||
| 61 | + } | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 65 | +pub struct DeleteUser { | ||
| 66 | + user_id: String, | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +impl DeleteUser { | ||
| 70 | + pub fn new(user_id: &str) -> Self { | ||
| 71 | + let user_id = user_id.to_string(); | ||
| 72 | + | ||
| 73 | + Self { user_id } | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + pub fn get_user_id(&self) -> &str { | ||
| 77 | + &self.user_id | ||
| 78 | + } | ||
| 79 | +} | ||
crates/common/src/request/user_community.rsadded+53 -0
| @@ -0,0 +1,53 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 4 | +pub struct JoinCommunity { | ||
| 5 | + user_id: String, | ||
| 6 | + community_id: String, | ||
| 7 | +} | ||
| 8 | + | ||
| 9 | +impl JoinCommunity { | ||
| 10 | + pub fn new(user_id: &str, community_id: &str) -> Self { | ||
| 11 | + let user_id = user_id.to_string(); | ||
| 12 | + let community_id = community_id.to_string(); | ||
| 13 | + | ||
| 14 | + Self { | ||
| 15 | + user_id, | ||
| 16 | + community_id, | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + pub fn get_user_id(&self) -> &str { | ||
| 21 | + &self.user_id | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + pub fn get_community_id(&self) -> &str { | ||
| 25 | + &self.community_id | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 30 | +pub struct LeaveCommunity { | ||
| 31 | + user_id: String, | ||
| 32 | + community_id: String, | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +impl LeaveCommunity { | ||
| 36 | + pub fn new(user_id: &str, community_id: &str) -> Self { | ||
| 37 | + let user_id = user_id.to_string(); | ||
| 38 | + let community_id = community_id.to_string(); | ||
| 39 | + | ||
| 40 | + Self { | ||
| 41 | + user_id, | ||
| 42 | + community_id, | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + pub fn get_user_id(&self) -> &str { | ||
| 47 | + &self.user_id | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + pub fn get_community_id(&self) -> &str { | ||
| 51 | + &self.community_id | ||
| 52 | + } | ||
| 53 | +} | ||
crates/common/src/response.rsadded+27 -0
| @@ -0,0 +1,27 @@ | |||
| 1 | +use bitcode::{Decode, Encode}; | ||
| 2 | + | ||
| 3 | +use crate::{community::Community, error::Error, message::Message, user::User}; | ||
| 4 | + | ||
| 5 | +pub mod community; | ||
| 6 | +pub mod message; | ||
| 7 | +pub mod user; | ||
| 8 | + | ||
| 9 | +#[derive(Debug, Clone, Encode, Decode)] | ||
| 10 | +pub enum Response { | ||
| 11 | + Authentication(User), | ||
| 12 | + CreateUser(User), | ||
| 13 | + ReadUser(User), | ||
| 14 | + UpdateUser(User), | ||
| 15 | + DeleteUser(User), | ||
| 16 | + CreateCommunity(Community), | ||
| 17 | + ReadCommunity(Community), | ||
| 18 | + UpdateCommunity(Community), | ||
| 19 | + DeleteCommunity(Community), | ||
| 20 | + CreateMessage(Message), | ||
| 21 | + ReadMessage(Message), | ||
| 22 | + UpdateMessage(Message), | ||
| 23 | + DeleteMessage(Message), | ||
| 24 | + JoinCommunity, | ||
| 25 | + LeaveCommunity, | ||
| 26 | + Error(Error), | ||
| 27 | +} | ||
crates/common/src/response/community.rsadded+0 -0
crates/common/src/response/message.rsadded+0 -0
crates/common/src/response/user.rsadded+0 -0