forge
Explore
Log in

Don't you dare go Hollow

Add bastion rooms and split user/message into modules

Browse files at this commit

9c1eb2fc78cd90a1e49e9ad56fd5ce3fff4ad6a1Ahmet Kaan GümüşParent: 335c931
  • feat
    • add bastion rooms and split user/message into modules
Show full message
✨ feat(common): add bastion rooms and split user/message into modules

Introduce Bastion (room/channel) with join/leave events, extract User
and Message into their own modules with Arc-backed inners, and carve
out a network module as a placeholder. Message now carries the owning
bastion so downstream consumers can route by room.
crates/common/src/bastion.rsadded+87 -0
@@ -0,0 +1,87 @@
1+use std::sync::Arc;
2+
3+use crate::{error::Error, user::User, validate::validate_bastion_name};
4+
5+#[derive(Debug)]
6+struct BastionInner {
7+ id: String,
8+ name: String,
9+}
10+
11+impl BastionInner {
12+ pub fn new(name: impl ToString) -> Result<Self, Error> {
13+ let id = "replace".to_string();
14+ let name = name.to_string();
15+
16+ validate_bastion_name(&name)?;
17+
18+ Ok(Self { id, name })
19+ }
20+
21+ pub fn get_id(&self) -> &String {
22+ &self.id
23+ }
24+
25+ pub fn get_name(&self) -> &String {
26+ &self.name
27+ }
28+}
29+
30+#[derive(Debug, Clone)]
31+pub struct Bastion {
32+ bastion: Arc<BastionInner>,
33+}
34+
35+impl Bastion {
36+ pub fn new(name: impl ToString) -> Result<Self, Error> {
37+ Ok(Self {
38+ bastion: BastionInner::new(name)?.into(),
39+ })
40+ }
41+
42+ pub fn get_id(&self) -> &String {
43+ self.bastion.get_id()
44+ }
45+
46+ pub fn get_name(&self) -> &String {
47+ self.bastion.get_name()
48+ }
49+}
50+
51+#[derive(Debug)]
52+pub enum BastionEvent {
53+ JoinBastion(JoinBastion),
54+ LeftBastion(LeftBastion),
55+}
56+
57+#[derive(Debug)]
58+pub struct JoinBastion {
59+ user: User,
60+ bastion: Bastion,
61+}
62+
63+impl JoinBastion {
64+ pub fn new(user: User, bastion: Bastion) -> Self {
65+ Self { user, bastion }
66+ }
67+
68+ pub fn gather(self) -> (User, Bastion) {
69+ (self.user, self.bastion)
70+ }
71+}
72+
73+#[derive(Debug)]
74+pub struct LeftBastion {
75+ user: User,
76+ bastion: Bastion,
77+}
78+
79+impl LeftBastion {
80+ pub fn new(user: User, bastion: Bastion) -> Self {
81+ Self { user, bastion }
82+ }
83+
84+ pub fn gather(self) -> (User, Bastion) {
85+ (self.user, self.bastion)
86+ }
87+}
crates/common/src/constant.rsmodified+2 -0
@@ -2,3 +2,5 @@ pub const MAXIMUM_USERNAME_LENGTH: usize = 16;
2 pub const MINIMUM_USERNAME_LENGTH: usize = 3;2 pub const MINIMUM_USERNAME_LENGTH: usize = 3;
3 pub const MAXIMUM_MESSAGE_LENGTH: usize = 1024;3 pub const MAXIMUM_MESSAGE_LENGTH: usize = 1024;
4 pub const MINIMUM_MESSAGE_LENGTH: usize = 1;4 pub const MINIMUM_MESSAGE_LENGTH: usize = 1;
5+pub const MAXIMUM_BASTION_NAME_LENGTH: usize = 16;
6+pub const MINIMUM_BASTION_NAME_LENGTH: usize = 3;
crates/common/src/error.rsmodified+9 -0
@@ -1,5 +1,6 @@
1 use std::fmt::Display;1 use std::fmt::Display;
2 2
3+pub mod bastion;
3 pub mod message;4 pub mod message;
4 pub mod user;5 pub mod user;
5 6
@@ -7,6 +8,7 @@ pub mod user;
7 pub enum Error {8 pub enum Error {
8 User(user::Error),9 User(user::Error),
9 Message(message::Error),10 Message(message::Error),
11+ Bastion(bastion::Error),
10 }12 }
11 13
12 impl std::error::Error for Error {}14 impl std::error::Error for Error {}
@@ -16,6 +18,7 @@ impl Display for Error {
16 match self {18 match self {
17 Error::User(error_value) => error_value.fmt(f),19 Error::User(error_value) => error_value.fmt(f),
18 Error::Message(error_value) => error_value.fmt(f),20 Error::Message(error_value) => error_value.fmt(f),
21+ Error::Bastion(error_value) => error_value.fmt(f),
19 }22 }
20 }23 }
21 }24 }
@@ -31,3 +34,9 @@ impl From<message::Error> for Error {
31 Error::Message(value)34 Error::Message(value)
32 }35 }
33 }36 }
37+
38+impl From<bastion::Error> for Error {
39+ fn from(value: bastion::Error) -> Self {
40+ Error::Bastion(value)
41+ }
42+}
crates/common/src/error/bastion.rsadded+30 -0
@@ -0,0 +1,30 @@
1+use std::fmt::Display;
2+
3+use crate::constant::{MAXIMUM_BASTION_NAME_LENGTH, MINIMUM_BASTION_NAME_LENGTH};
4+
5+#[derive(Debug)]
6+pub enum Error {
7+ MaximumLength(usize),
8+ MinimumLength(usize),
9+ ASCII,
10+}
11+
12+impl std::error::Error for Error {}
13+
14+impl Display for Error {
15+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16+ match self {
17+ Error::MaximumLength(length) => write!(
18+ f,
19+ "More than maximum length({}) is provided({})",
20+ MAXIMUM_BASTION_NAME_LENGTH, length
21+ ),
22+ Error::MinimumLength(length) => write!(
23+ f,
24+ "Less than minimum length({}) is provided({})",
25+ MINIMUM_BASTION_NAME_LENGTH, length
26+ ),
27+ Error::ASCII => write!(f, "Given input is not ASCII compatible"),
28+ }
29+ }
30+}
crates/common/src/lib.rsmodified+4 -74
@@ -1,77 +1,7 @@
1-use std::sync::Arc;1+pub mod bastion;
2-
3-use crate::{
4- error::Error,
5- validate::{validate_message, validate_username},
6-};
7-
8 pub mod constant;2 pub mod constant;
9 pub mod error;3 pub mod error;
4+pub mod message;
5+pub mod network;
6+pub mod user;
10 pub mod validate;7 pub mod validate;
11-
12-#[derive(Debug)]
13-struct UserInner {
14- id: u8,
15- username: String,
16-}
17-
18-impl UserInner {
19- pub fn new(username: impl ToString) -> Result<Self, Error> {
20- let username = username.to_string();
21- validate_username(&username)?;
22-
23- Ok(Self { id: 0, username })
24- }
25-
26- pub fn get_id(&self) -> u8 {
27- self.id
28- }
29-
30- pub fn get_username(&self) -> &String {
31- &self.username
32- }
33-}
34-
35-#[derive(Debug, Clone)]
36-pub struct User {
37- user: Arc<UserInner>,
38-}
39-
40-impl User {
41- pub fn new(username: impl ToString) -> Result<Self, Error> {
42- Ok(Self {
43- user: UserInner::new(username)?.into(),
44- })
45- }
46-
47- pub fn get_id(&self) -> u8 {
48- self.user.get_id()
49- }
50-
51- pub fn get_username(&self) -> &String {
52- &self.user.get_username()
53- }
54-}
55-
56-#[derive(Debug)]
57-pub struct Message {
58- sender: User,
59- message: String,
60-}
61-
62-impl Message {
63- pub fn new(sender: User, message: impl ToString) -> Result<Self, Error> {
64- let message = message.to_string();
65- validate_message(&message)?;
66-
67- Ok(Self { sender, message })
68- }
69-
70- pub fn get_sender(&self) -> &User {
71- &self.sender
72- }
73-
74- pub fn get_message(&self) -> &String {
75- &self.message
76- }
77-}
crates/common/src/message.rsadded+54 -0
@@ -0,0 +1,54 @@
1+use std::sync::Arc;
2+
3+use crate::{bastion::Bastion, error::Error, user::User, validate::validate_message};
4+
5+#[derive(Debug)]
6+struct MessageInner {
7+ id: String,
8+ user: User,
9+ bastion: Bastion,
10+ message: String,
11+}
12+
13+impl MessageInner {
14+ pub fn new(user: User, bastion: Bastion, message: impl ToString) -> Result<Self, Error> {
15+ let message = message.to_string();
16+ validate_message(&message)?;
17+
18+ Ok(Self {
19+ id: "replace".to_string(),
20+ user,
21+ bastion,
22+ message,
23+ })
24+ }
25+
26+ pub fn get_user(&self) -> &User {
27+ &self.user
28+ }
29+
30+ pub fn get_message(&self) -> &String {
31+ &self.message
32+ }
33+}
34+
35+#[derive(Debug, Clone)]
36+pub struct Message {
37+ message: Arc<MessageInner>,
38+}
39+
40+impl Message {
41+ pub fn new(user: User, bastion: Bastion, message: impl ToString) -> Result<Self, Error> {
42+ Ok(Self {
43+ message: MessageInner::new(user, bastion, message)?.into(),
44+ })
45+ }
46+
47+ pub fn get_user(&self) -> &User {
48+ self.message.get_user()
49+ }
50+
51+ pub fn get_message(&self) -> &String {
52+ self.message.get_message()
53+ }
54+}
crates/common/src/network.rsadded+1 -0
@@ -0,0 +1 @@
1+
crates/common/src/user.rsadded+50 -0
@@ -0,0 +1,50 @@
1+use std::sync::Arc;
2+
3+use crate::{error::Error, validate::validate_username};
4+
5+#[derive(Debug)]
6+struct UserInner {
7+ id: String,
8+ username: String,
9+}
10+
11+impl UserInner {
12+ pub fn new(username: impl ToString) -> Result<Self, Error> {
13+ let username = username.to_string();
14+ validate_username(&username)?;
15+
16+ Ok(Self {
17+ id: "Tahinli".into(),
18+ username,
19+ })
20+ }
21+
22+ pub fn get_id(&self) -> &String {
23+ &self.id
24+ }
25+
26+ pub fn get_username(&self) -> &String {
27+ &self.username
28+ }
29+}
30+
31+#[derive(Debug, Clone)]
32+pub struct User {
33+ user: Arc<UserInner>,
34+}
35+
36+impl User {
37+ pub fn new(username: impl ToString) -> Result<Self, Error> {
38+ Ok(Self {
39+ user: UserInner::new(username)?.into(),
40+ })
41+ }
42+
43+ pub fn get_id(&self) -> &String {
44+ self.user.get_id()
45+ }
46+
47+ pub fn get_username(&self) -> &String {
48+ self.user.get_username()
49+ }
50+}
crates/common/src/validate.rsmodified+19 -2
@@ -1,10 +1,11 @@
1 use crate::constant::{1 use crate::constant::{
2- MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH, MINIMUM_MESSAGE_LENGTH,2+ MAXIMUM_BASTION_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH,
3- MINIMUM_USERNAME_LENGTH,3+ MINIMUM_BASTION_NAME_LENGTH, MINIMUM_MESSAGE_LENGTH, MINIMUM_USERNAME_LENGTH,
4 };4 };
5 5
6 type UserError = crate::error::user::Error;6 type UserError = crate::error::user::Error;
7 type MessageError = crate::error::message::Error;7 type MessageError = crate::error::message::Error;
8+type BastionError = crate::error::bastion::Error;
8 9
9 pub fn validate_username(username: &str) -> Result<(), UserError> {10 pub fn validate_username(username: &str) -> Result<(), UserError> {
10 if !username.is_ascii() {11 if !username.is_ascii() {
@@ -37,3 +38,19 @@ pub fn validate_message(message: &str) -> Result<(), MessageError> {
37 38
38 Ok(())39 Ok(())
39 }40 }
41+
42+pub fn validate_bastion_name(name: &str) -> Result<(), BastionError> {
43+ if !name.is_ascii() {
44+ return Err(BastionError::ASCII);
45+ }
46+
47+ if name.len() < MINIMUM_BASTION_NAME_LENGTH {
48+ return Err(BastionError::MinimumLength(name.len()));
49+ }
50+
51+ if name.len() > MAXIMUM_BASTION_NAME_LENGTH {
52+ return Err(BastionError::MaximumLength(name.len()));
53+ }
54+
55+ Ok(())
56+}