diff --git a/crates/common/src/bastion.rs b/crates/common/src/bastion.rs new file mode 100644 index 0000000..35d685b --- /dev/null +++ b/crates/common/src/bastion.rs @@ -0,0 +1,87 @@ +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/constant.rs b/crates/common/src/constant.rs index d23f3d5..79a0dec 100644 --- a/crates/common/src/constant.rs +++ b/crates/common/src/constant.rs @@ -2,3 +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; diff --git a/crates/common/src/error.rs b/crates/common/src/error.rs index d5b4dcb..eba94ea 100644 --- a/crates/common/src/error.rs +++ b/crates/common/src/error.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +pub mod bastion; pub mod message; pub mod user; @@ -7,6 +8,7 @@ pub mod user; pub enum Error { User(user::Error), Message(message::Error), + Bastion(bastion::Error), } impl std::error::Error for Error {} @@ -16,6 +18,7 @@ impl Display for Error { match self { Error::User(error_value) => error_value.fmt(f), Error::Message(error_value) => error_value.fmt(f), + Error::Bastion(error_value) => error_value.fmt(f), } } } @@ -31,3 +34,9 @@ impl From for Error { Error::Message(value) } } + +impl From for Error { + fn from(value: bastion::Error) -> Self { + Error::Bastion(value) + } +} diff --git a/crates/common/src/error/bastion.rs b/crates/common/src/error/bastion.rs new file mode 100644 index 0000000..a812f80 --- /dev/null +++ b/crates/common/src/error/bastion.rs @@ -0,0 +1,30 @@ +use std::fmt::Display; + +use crate::constant::{MAXIMUM_BASTION_NAME_LENGTH, MINIMUM_BASTION_NAME_LENGTH}; + +#[derive(Debug)] +pub enum Error { + MaximumLength(usize), + MinimumLength(usize), + ASCII, +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::MaximumLength(length) => write!( + f, + "More than maximum length({}) is provided({})", + MAXIMUM_BASTION_NAME_LENGTH, length + ), + Error::MinimumLength(length) => write!( + f, + "Less than minimum length({}) is provided({})", + MINIMUM_BASTION_NAME_LENGTH, length + ), + Error::ASCII => write!(f, "Given input is not ASCII compatible"), + } + } +} diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 8d92856..74649df 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -1,77 +1,7 @@ -use std::sync::Arc; - -use crate::{ - error::Error, - validate::{validate_message, validate_username}, -}; - +pub mod bastion; pub mod constant; pub mod error; +pub mod message; +pub mod network; +pub mod user; pub mod validate; - -#[derive(Debug)] -struct UserInner { - id: u8, - username: String, -} - -impl UserInner { - pub fn new(username: impl ToString) -> Result { - let username = username.to_string(); - validate_username(&username)?; - - Ok(Self { id: 0, username }) - } - - pub fn get_id(&self) -> u8 { - self.id - } - - pub fn get_username(&self) -> &String { - &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) -> u8 { - self.user.get_id() - } - - pub fn get_username(&self) -> &String { - &self.user.get_username() - } -} - -#[derive(Debug)] -pub struct Message { - sender: User, - message: String, -} - -impl Message { - pub fn new(sender: User, message: impl ToString) -> Result { - let message = message.to_string(); - validate_message(&message)?; - - Ok(Self { sender, message }) - } - - pub fn get_sender(&self) -> &User { - &self.sender - } - - pub fn get_message(&self) -> &String { - &self.message - } -} diff --git a/crates/common/src/message.rs b/crates/common/src/message.rs new file mode 100644 index 0000000..9d1347f --- /dev/null +++ b/crates/common/src/message.rs @@ -0,0 +1,54 @@ +use std::sync::Arc; + +use crate::{bastion::Bastion, error::Error, user::User, validate::validate_message}; + +#[derive(Debug)] +struct MessageInner { + id: String, + user: User, + bastion: Bastion, + message: 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, + }) + } + + pub fn get_user(&self) -> &User { + &self.user + } + + 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_user(&self) -> &User { + self.message.get_user() + } + + pub fn get_message(&self) -> &String { + self.message.get_message() + } +} diff --git a/crates/common/src/network.rs b/crates/common/src/network.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/common/src/network.rs @@ -0,0 +1 @@ + diff --git a/crates/common/src/user.rs b/crates/common/src/user.rs new file mode 100644 index 0000000..4359017 --- /dev/null +++ b/crates/common/src/user.rs @@ -0,0 +1,50 @@ +use std::sync::Arc; + +use crate::{error::Error, validate::validate_username}; + +#[derive(Debug)] +struct UserInner { + id: String, + username: String, +} + +impl UserInner { + pub fn new(username: impl ToString) -> Result { + let username = username.to_string(); + validate_username(&username)?; + + Ok(Self { + id: "Tahinli".into(), + username, + }) + } + + pub fn get_id(&self) -> &String { + &self.id + } + + pub fn get_username(&self) -> &String { + &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 9bb4335..a3209fb 100644 --- a/crates/common/src/validate.rs +++ b/crates/common/src/validate.rs @@ -1,10 +1,11 @@ use crate::constant::{ - MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, MINIMUM_MESSAGE_LENGTH, - MINIMUM_USERNAME_LENGTH, + MAXIMUM_BASTION_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, + MINIMUM_BASTION_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; pub fn validate_username(username: &str) -> Result<(), UserError> { if !username.is_ascii() { @@ -37,3 +38,19 @@ 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); + } + + if name.len() < MINIMUM_BASTION_NAME_LENGTH { + return Err(BastionError::MinimumLength(name.len())); + } + + if name.len() > MAXIMUM_BASTION_NAME_LENGTH { + return Err(BastionError::MaximumLength(name.len())); + } + + Ok(()) +}