From cad6e55d237acaa1243a2d675c2b36e6d42f7c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 8 Apr 2026 17:35:50 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(common):=20add=20common=20crat?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/common/Cargo.toml | 11 +++++ crates/common/src/constant.rs | 4 ++ crates/common/src/error.rs | 33 +++++++++++++ crates/common/src/error/message.rs | 30 ++++++++++++ crates/common/src/error/user.rs | 30 ++++++++++++ crates/common/src/lib.rs | 77 ++++++++++++++++++++++++++++++ crates/common/src/validate.rs | 39 +++++++++++++++ 7 files changed, 224 insertions(+) create mode 100644 crates/common/Cargo.toml create mode 100644 crates/common/src/constant.rs create mode 100644 crates/common/src/error.rs create mode 100644 crates/common/src/error/message.rs create mode 100644 crates/common/src/error/user.rs create mode 100644 crates/common/src/lib.rs create mode 100644 crates/common/src/validate.rs diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml new file mode 100644 index 0000000..fac8390 --- /dev/null +++ b/crates/common/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "common" +version = "0.1.0" +edition = "2024" + +[features] +client = [] +server = [] + +[dependencies] +bitcode = "0.6.9" diff --git a/crates/common/src/constant.rs b/crates/common/src/constant.rs new file mode 100644 index 0000000..d23f3d5 --- /dev/null +++ b/crates/common/src/constant.rs @@ -0,0 +1,4 @@ +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; diff --git a/crates/common/src/error.rs b/crates/common/src/error.rs new file mode 100644 index 0000000..d5b4dcb --- /dev/null +++ b/crates/common/src/error.rs @@ -0,0 +1,33 @@ +use std::fmt::Display; + +pub mod message; +pub mod user; + +#[derive(Debug)] +pub enum Error { + User(user::Error), + Message(message::Error), +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::User(error_value) => error_value.fmt(f), + Error::Message(error_value) => error_value.fmt(f), + } + } +} + +impl From for Error { + fn from(value: user::Error) -> Self { + Error::User(value) + } +} + +impl From for Error { + fn from(value: message::Error) -> Self { + Error::Message(value) + } +} diff --git a/crates/common/src/error/message.rs b/crates/common/src/error/message.rs new file mode 100644 index 0000000..9da8baa --- /dev/null +++ b/crates/common/src/error/message.rs @@ -0,0 +1,30 @@ +use std::fmt::Display; + +use crate::constant::{MAXIMUM_MESSAGE_LENGTH, MINIMUM_MESSAGE_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_MESSAGE_LENGTH, length + ), + Error::MinimumLength(length) => write!( + f, + "Less than minimum length({}) is provided({})", + MINIMUM_MESSAGE_LENGTH, length + ), + Error::ASCII => write!(f, "Given input is not ASCII compatible"), + } + } +} diff --git a/crates/common/src/error/user.rs b/crates/common/src/error/user.rs new file mode 100644 index 0000000..b42ec7b --- /dev/null +++ b/crates/common/src/error/user.rs @@ -0,0 +1,30 @@ +use std::fmt::Display; + +use crate::constant::{MAXIMUM_USERNAME_LENGTH, MINIMUM_USERNAME_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_USERNAME_LENGTH, length + ), + Error::MinimumLength(length) => write!( + f, + "Less than minimum length({}) is provided({})", + MINIMUM_USERNAME_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 new file mode 100644 index 0000000..8d92856 --- /dev/null +++ b/crates/common/src/lib.rs @@ -0,0 +1,77 @@ +use std::sync::Arc; + +use crate::{ + error::Error, + validate::{validate_message, validate_username}, +}; + +pub mod constant; +pub mod error; +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/validate.rs b/crates/common/src/validate.rs new file mode 100644 index 0000000..9bb4335 --- /dev/null +++ b/crates/common/src/validate.rs @@ -0,0 +1,39 @@ +use crate::constant::{ + MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, MINIMUM_MESSAGE_LENGTH, + MINIMUM_USERNAME_LENGTH, +}; + +type UserError = crate::error::user::Error; +type MessageError = crate::error::message::Error; + +pub fn validate_username(username: &str) -> Result<(), UserError> { + if !username.is_ascii() { + return Err(UserError::ASCII); + } + + if username.len() < MINIMUM_USERNAME_LENGTH { + return Err(UserError::MinimumLength(username.len())); + } + + if username.len() > MAXIMUM_USERNAME_LENGTH { + return Err(UserError::MaximumLength(username.len())); + } + + Ok(()) +} + +pub fn validate_message(message: &str) -> Result<(), MessageError> { + if !message.is_ascii() { + return Err(MessageError::ASCII); + } + + if message.len() < MINIMUM_MESSAGE_LENGTH { + return Err(MessageError::MinimumLength(message.len())); + } + + if message.len() > MAXIMUM_MESSAGE_LENGTH { + return Err(MessageError::MaximumLength(message.len())); + } + + Ok(()) +}