diff --git a/crates/common/src/bastion.rs b/crates/common/src/bastion.rs deleted file mode 100644 index 35d685b..0000000 --- a/crates/common/src/bastion.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::sync::Arc; - -use crate::{error::Error, user::User, validate::validate_bastion_name}; - -#[derive(Debug)] -struct BastionInner { - id: String, - name: String, -} - -impl BastionInner { - pub fn new(name: impl ToString) -> Result { - let id = "replace".to_string(); - let name = name.to_string(); - - validate_bastion_name(&name)?; - - Ok(Self { id, name }) - } - - pub fn get_id(&self) -> &String { - &self.id - } - - pub fn get_name(&self) -> &String { - &self.name - } -} - -#[derive(Debug, Clone)] -pub struct Bastion { - bastion: Arc, -} - -impl Bastion { - pub fn new(name: impl ToString) -> Result { - Ok(Self { - bastion: BastionInner::new(name)?.into(), - }) - } - - pub fn get_id(&self) -> &String { - self.bastion.get_id() - } - - pub fn get_name(&self) -> &String { - self.bastion.get_name() - } -} - -#[derive(Debug)] -pub enum BastionEvent { - JoinBastion(JoinBastion), - LeftBastion(LeftBastion), -} - -#[derive(Debug)] -pub struct JoinBastion { - user: User, - bastion: Bastion, -} - -impl JoinBastion { - pub fn new(user: User, bastion: Bastion) -> Self { - Self { user, bastion } - } - - pub fn gather(self) -> (User, Bastion) { - (self.user, self.bastion) - } -} - -#[derive(Debug)] -pub struct LeftBastion { - user: User, - bastion: Bastion, -} - -impl LeftBastion { - pub fn new(user: User, bastion: Bastion) -> Self { - Self { user, bastion } - } - - pub fn gather(self) -> (User, Bastion) { - (self.user, self.bastion) - } -} diff --git a/crates/common/src/community.rs b/crates/common/src/community.rs new file mode 100644 index 0000000..a8eb893 --- /dev/null +++ b/crates/common/src/community.rs @@ -0,0 +1,28 @@ +use bitcode::{Decode, Encode}; + +#[derive(Debug, Clone, Encode, Decode)] +pub struct Community { + community_id: String, + community_name: String, +} + +impl Community { + #[cfg(feature = "server")] + pub fn new(community_id: &str, community_name: &str) -> Self { + let community_id = community_id.to_string(); + let community_name = community_name.to_string(); + + Self { + community_id, + community_name, + } + } + + pub fn get_community_id(&self) -> &str { + &self.community_id + } + + pub fn get_community_name(&self) -> &str { + &self.community_name + } +} diff --git a/crates/common/src/constant.rs b/crates/common/src/constant.rs index 79a0dec..5d36cc6 100644 --- a/crates/common/src/constant.rs +++ b/crates/common/src/constant.rs @@ -2,5 +2,5 @@ pub const MAXIMUM_USERNAME_LENGTH: usize = 16; pub const MINIMUM_USERNAME_LENGTH: usize = 3; pub const MAXIMUM_MESSAGE_LENGTH: usize = 1024; pub const MINIMUM_MESSAGE_LENGTH: usize = 1; -pub const MAXIMUM_BASTION_NAME_LENGTH: usize = 16; -pub const MINIMUM_BASTION_NAME_LENGTH: usize = 3; +pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16; +pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3; diff --git a/crates/common/src/error.rs b/crates/common/src/error.rs index eba94ea..ed1cd6f 100644 --- a/crates/common/src/error.rs +++ b/crates/common/src/error.rs @@ -1,14 +1,24 @@ use std::fmt::Display; -pub mod bastion; +use bitcode::{Decode, Encode}; + +pub mod community; +pub mod decode; +pub mod file_operation; pub mod message; +pub mod network; pub mod user; -#[derive(Debug)] +#[derive(Debug, Clone, Encode, Decode)] pub enum Error { + Authenticate, User(user::Error), Message(message::Error), - Bastion(bastion::Error), + Community(community::Error), + FileOperation(file_operation::Error), + Network(network::Error), + Decode(decode::Error), + Server, } impl std::error::Error for Error {} @@ -16,9 +26,14 @@ impl std::error::Error for Error {} impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + Error::Authenticate => write!(f, "Authenticate"), Error::User(error_value) => error_value.fmt(f), Error::Message(error_value) => error_value.fmt(f), - Error::Bastion(error_value) => error_value.fmt(f), + Error::Community(error_value) => error_value.fmt(f), + Error::FileOperation(error_value) => error_value.fmt(f), + Error::Network(error_value) => error_value.fmt(f), + Error::Decode(error_value) => error_value.fmt(f), + Error::Server => write!(f, "Server"), } } } @@ -35,8 +50,56 @@ impl From for Error { } } -impl From for Error { - fn from(value: bastion::Error) -> Self { - Error::Bastion(value) +impl From for Error { + fn from(value: community::Error) -> Self { + Error::Community(value) + } +} + +impl From for Error { + fn from(value: file_operation::Error) -> Self { + Error::FileOperation(value) + } +} + +impl From for Error { + fn from(value: network::Error) -> Self { + Error::Network(value) + } +} + +impl From for Error { + fn from(value: decode::Error) -> Self { + Error::Decode(value) + } +} + +impl From for Error { + fn from(value: quinn::ConnectionError) -> Self { + Self::Network(value.into()) + } +} + +impl From for Error { + fn from(value: quinn::ReadError) -> Self { + Self::Network(value.into()) + } +} + +impl From for Error { + fn from(value: quinn::ReadToEndError) -> Self { + Self::Network(value.into()) + } +} + +impl From for Error { + fn from(value: quinn::WriteError) -> Self { + Self::Network(value.into()) + } +} + +impl From for Error { + fn from(value: bitcode::Error) -> Self { + Self::Decode(value.into()) } } diff --git a/crates/common/src/error/bastion.rs b/crates/common/src/error/community.rs similarity index 71% rename from crates/common/src/error/bastion.rs rename to crates/common/src/error/community.rs index a812f80..af37050 100644 --- a/crates/common/src/error/bastion.rs +++ b/crates/common/src/error/community.rs @@ -1,8 +1,10 @@ use std::fmt::Display; -use crate::constant::{MAXIMUM_BASTION_NAME_LENGTH, MINIMUM_BASTION_NAME_LENGTH}; +use bitcode::{Decode, Encode}; -#[derive(Debug)] +use crate::constant::{MAXIMUM_COMMUNITY_NAME_LENGTH, MINIMUM_COMMUNITY_NAME_LENGTH}; + +#[derive(Debug, Clone, Encode, Decode)] pub enum Error { MaximumLength(usize), MinimumLength(usize), @@ -17,12 +19,12 @@ impl Display for Error { Error::MaximumLength(length) => write!( f, "More than maximum length({}) is provided({})", - MAXIMUM_BASTION_NAME_LENGTH, length + MAXIMUM_COMMUNITY_NAME_LENGTH, length ), Error::MinimumLength(length) => write!( f, "Less than minimum length({}) is provided({})", - MINIMUM_BASTION_NAME_LENGTH, length + MINIMUM_COMMUNITY_NAME_LENGTH, length ), Error::ASCII => write!(f, "Given input is not ASCII compatible"), } diff --git a/crates/common/src/error/decode.rs b/crates/common/src/error/decode.rs new file mode 100644 index 0000000..14ecf05 --- /dev/null +++ b/crates/common/src/error/decode.rs @@ -0,0 +1,24 @@ +use std::fmt::Display; + +use bitcode::{Decode, Encode}; + +#[derive(Debug, Clone, Encode, Decode)] +pub enum Error { + Inner(String), +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Inner(error_value) => error_value.fmt(f), + } + } +} + +impl From for Error { + fn from(value: bitcode::Error) -> Self { + Self::Inner(value.to_string()) + } +} diff --git a/crates/common/src/error/message.rs b/crates/common/src/error/message.rs index 9da8baa..0776644 100644 --- a/crates/common/src/error/message.rs +++ b/crates/common/src/error/message.rs @@ -1,8 +1,10 @@ use std::fmt::Display; +use bitcode::{Decode, Encode}; + use crate::constant::{MAXIMUM_MESSAGE_LENGTH, MINIMUM_MESSAGE_LENGTH}; -#[derive(Debug)] +#[derive(Debug, Clone, Encode, Decode)] pub enum Error { MaximumLength(usize), MinimumLength(usize), diff --git a/crates/common/src/error/network.rs b/crates/common/src/error/network.rs new file mode 100644 index 0000000..d389c66 --- /dev/null +++ b/crates/common/src/error/network.rs @@ -0,0 +1,48 @@ +use std::fmt::Display; + +use bitcode::{Decode, Encode}; + +#[derive(Debug, Clone, Encode, Decode)] +pub enum Error { + Connection(String), + Read(String), + ReadToEnd(String), + Write(String), +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Connection(error_value) => error_value.fmt(f), + Error::Read(error_value) => error_value.fmt(f), + Error::ReadToEnd(error_value) => error_value.fmt(f), + Error::Write(error_value) => error_value.fmt(f), + } + } +} + +impl From for Error { + fn from(value: quinn::ConnectionError) -> Self { + Self::Connection(value.to_string()) + } +} + +impl From for Error { + fn from(value: quinn::ReadError) -> Self { + Self::Read(value.to_string()) + } +} + +impl From for Error { + fn from(value: quinn::ReadToEndError) -> Self { + Self::Read(value.to_string()) + } +} + +impl From for Error { + fn from(value: quinn::WriteError) -> Self { + Self::Write(value.to_string()) + } +} diff --git a/crates/common/src/error/user.rs b/crates/common/src/error/user.rs index b42ec7b..b3a8832 100644 --- a/crates/common/src/error/user.rs +++ b/crates/common/src/error/user.rs @@ -1,8 +1,10 @@ use std::fmt::Display; +use bitcode::{Decode, Encode}; + use crate::constant::{MAXIMUM_USERNAME_LENGTH, MINIMUM_USERNAME_LENGTH}; -#[derive(Debug)] +#[derive(Debug, Clone, Encode, Decode)] pub enum Error { MaximumLength(usize), MinimumLength(usize), diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 74649df..4f15998 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -1,7 +1,11 @@ -pub mod bastion; +pub mod community; pub mod constant; pub mod error; +pub mod event; pub mod message; pub mod network; +pub mod request; +pub mod response; pub mod user; +pub mod utils; pub mod validate; diff --git a/crates/common/src/message.rs b/crates/common/src/message.rs index 9d1347f..f79000f 100644 --- a/crates/common/src/message.rs +++ b/crates/common/src/message.rs @@ -1,54 +1,42 @@ -use std::sync::Arc; +use bitcode::{Decode, Encode}; -use crate::{bastion::Bastion, error::Error, user::User, validate::validate_message}; - -#[derive(Debug)] -struct MessageInner { - id: String, - user: User, - bastion: Bastion, - message: String, +#[derive(Debug, Clone, Encode, Decode)] +pub struct Message { + message_id: String, + user_id: String, + community_id: String, + message_body: String, } -impl MessageInner { - pub fn new(user: User, bastion: Bastion, message: impl ToString) -> Result { - let message = message.to_string(); - validate_message(&message)?; - - Ok(Self { - id: "replace".to_string(), - user, - bastion, - message, - }) +impl Message { + #[cfg(feature = "server")] + pub fn new(message_id: &str, user_id: &str, community_id: &str, message_body: &str) -> Self { + let message_id = message_id.to_string(); + let user_id = user_id.to_string(); + let community_id = community_id.to_string(); + let message_body = message_body.to_string(); + + Self { + message_id, + user_id, + community_id, + message_body, + } } - pub fn get_user(&self) -> &User { - &self.user + pub fn get_id(&self) -> &str { + &self.message_id } - pub fn get_message(&self) -> &String { - &self.message - } -} - -#[derive(Debug, Clone)] -pub struct Message { - message: Arc, -} - -impl Message { - pub fn new(user: User, bastion: Bastion, message: impl ToString) -> Result { - Ok(Self { - message: MessageInner::new(user, bastion, message)?.into(), - }) + pub fn get_community_id(&self) -> &str { + &self.community_id } - pub fn get_user(&self) -> &User { - self.message.get_user() + pub fn get_user_id(&self) -> &str { + &self.user_id } - pub fn get_message(&self) -> &String { - self.message.get_message() + pub fn get_message_body(&self) -> &str { + &self.message_body } } diff --git a/crates/common/src/network.rs b/crates/common/src/network.rs index 8b13789..088908b 100644 --- a/crates/common/src/network.rs +++ b/crates/common/src/network.rs @@ -1 +1,41 @@ +use quinn::{RecvStream, SendStream}; +use crate::{constant::MAXIMUM_MESSAGE_LENGTH, error::Error, request::Request, response::Response}; + +pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64; +pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; +pub const RESPONCE_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2; + +pub struct Network; + +impl Network { + pub async fn send_response( + response: &Response, + mut send_stream: SendStream, + ) -> Result<(), Error> { + Ok(send_stream.write_all(&bitcode::encode(response)).await?) + } + + pub async fn receive_request( + mut receive_stream: RecvStream, + size_limit: Option, + ) -> Result { + let request = match size_limit { + Some(size_limit) => receive_stream.read_to_end(size_limit).await?, + None => receive_stream.read_to_end(REQUEST_MAX_READ_LENGHT).await?, + }; + + Ok(bitcode::decode::(&request)?) + } + + pub async fn send_request_and_receive_response( + request: &Request, + mut send_stream: SendStream, + mut receive_stream: RecvStream, + ) -> Result { + send_stream.write_all(&bitcode::encode(request)).await?; + let response = receive_stream.read_to_end(RESPONCE_MAX_READ_LENGHT).await?; + + Ok(bitcode::decode::(&response)?) + } +} diff --git a/crates/common/src/user.rs b/crates/common/src/user.rs index 4359017..034fd44 100644 --- a/crates/common/src/user.rs +++ b/crates/common/src/user.rs @@ -1,50 +1,25 @@ -use std::sync::Arc; +use bitcode::{Decode, Encode}; -use crate::{error::Error, validate::validate_username}; - -#[derive(Debug)] -struct UserInner { - id: String, +#[derive(Debug, Clone, Encode, Decode)] +pub struct User { + user_id: String, username: String, } -impl UserInner { - pub fn new(username: impl ToString) -> Result { +impl User { + #[cfg(feature = "server")] + pub fn new(user_id: &str, username: &str) -> Self { + let user_id = user_id.to_string(); let username = username.to_string(); - validate_username(&username)?; - Ok(Self { - id: "Tahinli".into(), - username, - }) + Self { user_id, username } } - pub fn get_id(&self) -> &String { - &self.id + pub fn get_user_id(&self) -> &str { + &self.user_id } - pub fn get_username(&self) -> &String { + pub fn get_username(&self) -> &str { &self.username } } - -#[derive(Debug, Clone)] -pub struct User { - user: Arc, -} - -impl User { - pub fn new(username: impl ToString) -> Result { - Ok(Self { - user: UserInner::new(username)?.into(), - }) - } - - pub fn get_id(&self) -> &String { - self.user.get_id() - } - - pub fn get_username(&self) -> &String { - self.user.get_username() - } -} diff --git a/crates/common/src/validate.rs b/crates/common/src/validate.rs index a3209fb..37101cb 100644 --- a/crates/common/src/validate.rs +++ b/crates/common/src/validate.rs @@ -1,11 +1,11 @@ use crate::constant::{ - MAXIMUM_BASTION_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, - MINIMUM_BASTION_NAME_LENGTH, MINIMUM_MESSAGE_LENGTH, MINIMUM_USERNAME_LENGTH, + MAXIMUM_COMMUNITY_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, + MINIMUM_COMMUNITY_NAME_LENGTH, MINIMUM_MESSAGE_LENGTH, MINIMUM_USERNAME_LENGTH, }; type UserError = crate::error::user::Error; type MessageError = crate::error::message::Error; -type BastionError = crate::error::bastion::Error; +type CommunityError = crate::error::community::Error; pub fn validate_username(username: &str) -> Result<(), UserError> { if !username.is_ascii() { @@ -23,7 +23,7 @@ pub fn validate_username(username: &str) -> Result<(), UserError> { Ok(()) } -pub fn validate_message(message: &str) -> Result<(), MessageError> { +pub fn validate_message_body(message: &str) -> Result<(), MessageError> { if !message.is_ascii() { return Err(MessageError::ASCII); } @@ -39,17 +39,17 @@ pub fn validate_message(message: &str) -> Result<(), MessageError> { Ok(()) } -pub fn validate_bastion_name(name: &str) -> Result<(), BastionError> { - if !name.is_ascii() { - return Err(BastionError::ASCII); +pub fn validate_community_name(community_name: &str) -> Result<(), CommunityError> { + if !community_name.is_ascii() { + return Err(CommunityError::ASCII); } - if name.len() < MINIMUM_BASTION_NAME_LENGTH { - return Err(BastionError::MinimumLength(name.len())); + if community_name.len() < MINIMUM_COMMUNITY_NAME_LENGTH { + return Err(CommunityError::MinimumLength(community_name.len())); } - if name.len() > MAXIMUM_BASTION_NAME_LENGTH { - return Err(BastionError::MaximumLength(name.len())); + if community_name.len() > MAXIMUM_COMMUNITY_NAME_LENGTH { + return Err(CommunityError::MaximumLength(community_name.len())); } Ok(())