forge
Explore
Log in

Don't you dare go Hollow

Replace bastion with community model

Browse files at this commit

aeaaadafec2f2693265f7ad1647ebd422f1f48bdAhmet Kaan GümüşParent: f240877
  • refactor
    • replace bastion with community model
Show full message
♻️ refactor(common): replace bastion with community model

Flatten user/message into plain bitcode-encodable structs and add
quinn stream helpers so all shared types can cross the wire.
crates/common/src/bastion.rsdeleted+0 -87
@@ -1,87 +0,0 @@
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/community.rsadded+28 -0
@@ -0,0 +1,28 @@
1+use bitcode::{Decode, Encode};
2+
3+#[derive(Debug, Clone, Encode, Decode)]
4+pub struct Community {
5+ community_id: String,
6+ community_name: String,
7+}
8+
9+impl Community {
10+ #[cfg(feature = "server")]
11+ pub fn new(community_id: &str, community_name: &str) -> Self {
12+ let community_id = community_id.to_string();
13+ let community_name = community_name.to_string();
14+
15+ Self {
16+ community_id,
17+ community_name,
18+ }
19+ }
20+
21+ pub fn get_community_id(&self) -> &str {
22+ &self.community_id
23+ }
24+
25+ pub fn get_community_name(&self) -> &str {
26+ &self.community_name
27+ }
28+}
crates/common/src/constant.rsmodified+2 -2
@@ -2,5 +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;5+pub const MAXIMUM_COMMUNITY_NAME_LENGTH: usize = 16;
6-pub const MINIMUM_BASTION_NAME_LENGTH: usize = 3;6+pub const MINIMUM_COMMUNITY_NAME_LENGTH: usize = 3;
crates/common/src/error.rsmodified+70 -7
@@ -1,14 +1,24 @@
1 use std::fmt::Display;1 use std::fmt::Display;
2 2
3-pub mod bastion;3+use bitcode::{Decode, Encode};
4+
5+pub mod community;
6+pub mod decode;
7+pub mod file_operation;
4 pub mod message;8 pub mod message;
9+pub mod network;
5 pub mod user;10 pub mod user;
6 11
7-#[derive(Debug)]12+#[derive(Debug, Clone, Encode, Decode)]
8 pub enum Error {13 pub enum Error {
14+ Authenticate,
9 User(user::Error),15 User(user::Error),
10 Message(message::Error),16 Message(message::Error),
11- Bastion(bastion::Error),17+ Community(community::Error),
18+ FileOperation(file_operation::Error),
19+ Network(network::Error),
20+ Decode(decode::Error),
21+ Server,
12 }22 }
13 23
14 impl std::error::Error for Error {}24 impl std::error::Error for Error {}
@@ -16,9 +26,14 @@ impl std::error::Error for Error {}
16 impl Display for Error {26 impl Display for Error {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 match self {28 match self {
29+ Error::Authenticate => write!(f, "Authenticate"),
19 Error::User(error_value) => error_value.fmt(f),30 Error::User(error_value) => error_value.fmt(f),
20 Error::Message(error_value) => error_value.fmt(f),31 Error::Message(error_value) => error_value.fmt(f),
21- Error::Bastion(error_value) => error_value.fmt(f),32+ Error::Community(error_value) => error_value.fmt(f),
33+ Error::FileOperation(error_value) => error_value.fmt(f),
34+ Error::Network(error_value) => error_value.fmt(f),
35+ Error::Decode(error_value) => error_value.fmt(f),
36+ Error::Server => write!(f, "Server"),
22 }37 }
23 }38 }
24 }39 }
@@ -35,8 +50,56 @@ impl From<message::Error> for Error {
35 }50 }
36 }51 }
37 52
38-impl From<bastion::Error> for Error {53+impl From<community::Error> for Error {
39- fn from(value: bastion::Error) -> Self {54+ fn from(value: community::Error) -> Self {
40- Error::Bastion(value)55+ Error::Community(value)
56+ }
57+}
58+
59+impl From<file_operation::Error> for Error {
60+ fn from(value: file_operation::Error) -> Self {
61+ Error::FileOperation(value)
62+ }
63+}
64+
65+impl From<network::Error> for Error {
66+ fn from(value: network::Error) -> Self {
67+ Error::Network(value)
68+ }
69+}
70+
71+impl From<decode::Error> for Error {
72+ fn from(value: decode::Error) -> Self {
73+ Error::Decode(value)
74+ }
75+}
76+
77+impl From<quinn::ConnectionError> for Error {
78+ fn from(value: quinn::ConnectionError) -> Self {
79+ Self::Network(value.into())
80+ }
81+}
82+
83+impl From<quinn::ReadError> for Error {
84+ fn from(value: quinn::ReadError) -> Self {
85+ Self::Network(value.into())
86+ }
87+}
88+
89+impl From<quinn::ReadToEndError> for Error {
90+ fn from(value: quinn::ReadToEndError) -> Self {
91+ Self::Network(value.into())
92+ }
93+}
94+
95+impl From<quinn::WriteError> for Error {
96+ fn from(value: quinn::WriteError) -> Self {
97+ Self::Network(value.into())
98+ }
99+}
100+
101+impl From<bitcode::Error> for Error {
102+ fn from(value: bitcode::Error) -> Self {
103+ Self::Decode(value.into())
41 }104 }
42 }105 }
crates/common/src/error/bastion.rs → crates/common/src/error/community.rsrenamed+6 -4
@@ -1,8 +1,10 @@
1 use std::fmt::Display;1 use std::fmt::Display;
2 2
3-use crate::constant::{MAXIMUM_BASTION_NAME_LENGTH, MINIMUM_BASTION_NAME_LENGTH};3+use bitcode::{Decode, Encode};
4 4
5-#[derive(Debug)]5+use crate::constant::{MAXIMUM_COMMUNITY_NAME_LENGTH, MINIMUM_COMMUNITY_NAME_LENGTH};
6+
7+#[derive(Debug, Clone, Encode, Decode)]
6 pub enum Error {8 pub enum Error {
7 MaximumLength(usize),9 MaximumLength(usize),
8 MinimumLength(usize),10 MinimumLength(usize),
@@ -17,12 +19,12 @@ impl Display for Error {
17 Error::MaximumLength(length) => write!(19 Error::MaximumLength(length) => write!(
18 f,20 f,
19 "More than maximum length({}) is provided({})",21 "More than maximum length({}) is provided({})",
20- MAXIMUM_BASTION_NAME_LENGTH, length22+ MAXIMUM_COMMUNITY_NAME_LENGTH, length
21 ),23 ),
22 Error::MinimumLength(length) => write!(24 Error::MinimumLength(length) => write!(
23 f,25 f,
24 "Less than minimum length({}) is provided({})",26 "Less than minimum length({}) is provided({})",
25- MINIMUM_BASTION_NAME_LENGTH, length27+ MINIMUM_COMMUNITY_NAME_LENGTH, length
26 ),28 ),
27 Error::ASCII => write!(f, "Given input is not ASCII compatible"),29 Error::ASCII => write!(f, "Given input is not ASCII compatible"),
28 }30 }
crates/common/src/error/decode.rsadded+24 -0
@@ -0,0 +1,24 @@
1+use std::fmt::Display;
2+
3+use bitcode::{Decode, Encode};
4+
5+#[derive(Debug, Clone, Encode, Decode)]
6+pub enum Error {
7+ Inner(String),
8+}
9+
10+impl std::error::Error for Error {}
11+
12+impl Display for Error {
13+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14+ match self {
15+ Error::Inner(error_value) => error_value.fmt(f),
16+ }
17+ }
18+}
19+
20+impl From<bitcode::Error> for Error {
21+ fn from(value: bitcode::Error) -> Self {
22+ Self::Inner(value.to_string())
23+ }
24+}
crates/common/src/error/message.rsmodified+3 -1
@@ -1,8 +1,10 @@
1 use std::fmt::Display;1 use std::fmt::Display;
2 2
3+use bitcode::{Decode, Encode};
4+
3 use crate::constant::{MAXIMUM_MESSAGE_LENGTH, MINIMUM_MESSAGE_LENGTH};5 use crate::constant::{MAXIMUM_MESSAGE_LENGTH, MINIMUM_MESSAGE_LENGTH};
4 6
5-#[derive(Debug)]7+#[derive(Debug, Clone, Encode, Decode)]
6 pub enum Error {8 pub enum Error {
7 MaximumLength(usize),9 MaximumLength(usize),
8 MinimumLength(usize),10 MinimumLength(usize),
crates/common/src/error/network.rsadded+48 -0
@@ -0,0 +1,48 @@
1+use std::fmt::Display;
2+
3+use bitcode::{Decode, Encode};
4+
5+#[derive(Debug, Clone, Encode, Decode)]
6+pub enum Error {
7+ Connection(String),
8+ Read(String),
9+ ReadToEnd(String),
10+ Write(String),
11+}
12+
13+impl std::error::Error for Error {}
14+
15+impl Display for Error {
16+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17+ match self {
18+ Error::Connection(error_value) => error_value.fmt(f),
19+ Error::Read(error_value) => error_value.fmt(f),
20+ Error::ReadToEnd(error_value) => error_value.fmt(f),
21+ Error::Write(error_value) => error_value.fmt(f),
22+ }
23+ }
24+}
25+
26+impl From<quinn::ConnectionError> for Error {
27+ fn from(value: quinn::ConnectionError) -> Self {
28+ Self::Connection(value.to_string())
29+ }
30+}
31+
32+impl From<quinn::ReadError> for Error {
33+ fn from(value: quinn::ReadError) -> Self {
34+ Self::Read(value.to_string())
35+ }
36+}
37+
38+impl From<quinn::ReadToEndError> for Error {
39+ fn from(value: quinn::ReadToEndError) -> Self {
40+ Self::Read(value.to_string())
41+ }
42+}
43+
44+impl From<quinn::WriteError> for Error {
45+ fn from(value: quinn::WriteError) -> Self {
46+ Self::Write(value.to_string())
47+ }
48+}
crates/common/src/error/user.rsmodified+3 -1
@@ -1,8 +1,10 @@
1 use std::fmt::Display;1 use std::fmt::Display;
2 2
3+use bitcode::{Decode, Encode};
4+
3 use crate::constant::{MAXIMUM_USERNAME_LENGTH, MINIMUM_USERNAME_LENGTH};5 use crate::constant::{MAXIMUM_USERNAME_LENGTH, MINIMUM_USERNAME_LENGTH};
4 6
5-#[derive(Debug)]7+#[derive(Debug, Clone, Encode, Decode)]
6 pub enum Error {8 pub enum Error {
7 MaximumLength(usize),9 MaximumLength(usize),
8 MinimumLength(usize),10 MinimumLength(usize),
crates/common/src/lib.rsmodified+5 -1
@@ -1,7 +1,11 @@
1-pub mod bastion;1+pub mod community;
2 pub mod constant;2 pub mod constant;
3 pub mod error;3 pub mod error;
4+pub mod event;
4 pub mod message;5 pub mod message;
5 pub mod network;6 pub mod network;
7+pub mod request;
8+pub mod response;
6 pub mod user;9 pub mod user;
10+pub mod utils;
7 pub mod validate;11 pub mod validate;
crates/common/src/message.rsmodified+29 -41
@@ -1,54 +1,42 @@
1-use std::sync::Arc;1+use bitcode::{Decode, Encode};
2 2
3-use crate::{bastion::Bastion, error::Error, user::User, validate::validate_message};3+#[derive(Debug, Clone, Encode, Decode)]
4-4+pub struct Message {
5-#[derive(Debug)]5+ message_id: String,
6-struct MessageInner {6+ user_id: String,
7- id: String,7+ community_id: String,
8- user: User,8+ message_body: String,
9- bastion: Bastion,
10- message: String,
11 }9 }
12 10
13-impl MessageInner {11+impl Message {
14- pub fn new(user: User, bastion: Bastion, message: impl ToString) -> Result<Self, Error> {12+ #[cfg(feature = "server")]
15- let message = message.to_string();13+ pub fn new(message_id: &str, user_id: &str, community_id: &str, message_body: &str) -> Self {
16- validate_message(&message)?;14+ let message_id = message_id.to_string();
17-15+ let user_id = user_id.to_string();
18- Ok(Self {16+ let community_id = community_id.to_string();
19- id: "replace".to_string(),17+ let message_body = message_body.to_string();
20- user,18+
21- bastion,19+ Self {
22- message,20+ message_id,
23- })21+ user_id,
22+ community_id,
23+ message_body,
24+ }
24 }25 }
25 26
26- pub fn get_user(&self) -> &User {27+ pub fn get_id(&self) -> &str {
27- &self.user28+ &self.message_id
28 }29 }
29 30
30- pub fn get_message(&self) -> &String {31+ pub fn get_community_id(&self) -> &str {
31- &self.message32+ &self.community_id
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 }33 }
46 34
47- pub fn get_user(&self) -> &User {35+ pub fn get_user_id(&self) -> &str {
48- self.message.get_user()36+ &self.user_id
49 }37 }
50 38
51- pub fn get_message(&self) -> &String {39+ pub fn get_message_body(&self) -> &str {
52- self.message.get_message()40+ &self.message_body
53 }41 }
54 }42 }
crates/common/src/network.rsmodified+40 -0
@@ -1 +1,41 @@
1+use quinn::{RecvStream, SendStream};
1 2
3+use crate::{constant::MAXIMUM_MESSAGE_LENGTH, error::Error, request::Request, response::Response};
4+
5+pub const AUTHENTICATION_MAX_READ_LENGHT: usize = 64;
6+pub const REQUEST_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2;
7+pub const RESPONCE_MAX_READ_LENGHT: usize = MAXIMUM_MESSAGE_LENGTH * 2;
8+
9+pub struct Network;
10+
11+impl Network {
12+ pub async fn send_response(
13+ response: &Response,
14+ mut send_stream: SendStream,
15+ ) -> Result<(), Error> {
16+ Ok(send_stream.write_all(&bitcode::encode(response)).await?)
17+ }
18+
19+ pub async fn receive_request(
20+ mut receive_stream: RecvStream,
21+ size_limit: Option<usize>,
22+ ) -> Result<Request, Error> {
23+ let request = match size_limit {
24+ Some(size_limit) => receive_stream.read_to_end(size_limit).await?,
25+ None => receive_stream.read_to_end(REQUEST_MAX_READ_LENGHT).await?,
26+ };
27+
28+ Ok(bitcode::decode::<Request>(&request)?)
29+ }
30+
31+ pub async fn send_request_and_receive_response(
32+ request: &Request,
33+ mut send_stream: SendStream,
34+ mut receive_stream: RecvStream,
35+ ) -> Result<Response, Error> {
36+ send_stream.write_all(&bitcode::encode(request)).await?;
37+ let response = receive_stream.read_to_end(RESPONCE_MAX_READ_LENGHT).await?;
38+
39+ Ok(bitcode::decode::<Response>(&response)?)
40+ }
41+}
crates/common/src/user.rsmodified+12 -37
@@ -1,50 +1,25 @@
1-use std::sync::Arc;1+use bitcode::{Decode, Encode};
2 2
3-use crate::{error::Error, validate::validate_username};3+#[derive(Debug, Clone, Encode, Decode)]
4-4+pub struct User {
5-#[derive(Debug)]5+ user_id: String,
6-struct UserInner {
7- id: String,
8 username: String,6 username: String,
9 }7 }
10 8
11-impl UserInner {9+impl User {
12- pub fn new(username: impl ToString) -> Result<Self, Error> {10+ #[cfg(feature = "server")]
11+ pub fn new(user_id: &str, username: &str) -> Self {
12+ let user_id = user_id.to_string();
13 let username = username.to_string();13 let username = username.to_string();
14- validate_username(&username)?;
15 14
16- Ok(Self {15+ Self { user_id, username }
17- id: "Tahinli".into(),
18- username,
19- })
20 }16 }
21 17
22- pub fn get_id(&self) -> &String {18+ pub fn get_user_id(&self) -> &str {
23- &self.id19+ &self.user_id
24 }20 }
25 21
26- pub fn get_username(&self) -> &String {22+ pub fn get_username(&self) -> &str {
27 &self.username23 &self.username
28 }24 }
29 }25 }
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+11 -11
@@ -1,11 +1,11 @@
1 use crate::constant::{1 use crate::constant::{
2- MAXIMUM_BASTION_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH,2+ MAXIMUM_COMMUNITY_NAME_LENGTH, MAXIMUM_MESSAGE_LENGTH, MAXIMUM_USERNAME_LENGTH,
3- MINIMUM_BASTION_NAME_LENGTH, MINIMUM_MESSAGE_LENGTH, MINIMUM_USERNAME_LENGTH,3+ MINIMUM_COMMUNITY_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+type CommunityError = crate::error::community::Error;
9 9
10 pub fn validate_username(username: &str) -> Result<(), UserError> {10 pub fn validate_username(username: &str) -> Result<(), UserError> {
11 if !username.is_ascii() {11 if !username.is_ascii() {
@@ -23,7 +23,7 @@ pub fn validate_username(username: &str) -> Result<(), UserError> {
23 Ok(())23 Ok(())
24 }24 }
25 25
26-pub fn validate_message(message: &str) -> Result<(), MessageError> {26+pub fn validate_message_body(message: &str) -> Result<(), MessageError> {
27 if !message.is_ascii() {27 if !message.is_ascii() {
28 return Err(MessageError::ASCII);28 return Err(MessageError::ASCII);
29 }29 }
@@ -39,17 +39,17 @@ pub fn validate_message(message: &str) -> Result<(), MessageError> {
39 Ok(())39 Ok(())
40 }40 }
41 41
42-pub fn validate_bastion_name(name: &str) -> Result<(), BastionError> {42+pub fn validate_community_name(community_name: &str) -> Result<(), CommunityError> {
43- if !name.is_ascii() {43+ if !community_name.is_ascii() {
44- return Err(BastionError::ASCII);44+ return Err(CommunityError::ASCII);
45 }45 }
46 46
47- if name.len() < MINIMUM_BASTION_NAME_LENGTH {47+ if community_name.len() < MINIMUM_COMMUNITY_NAME_LENGTH {
48- return Err(BastionError::MinimumLength(name.len()));48+ return Err(CommunityError::MinimumLength(community_name.len()));
49 }49 }
50 50
51- if name.len() > MAXIMUM_BASTION_NAME_LENGTH {51+ if community_name.len() > MAXIMUM_COMMUNITY_NAME_LENGTH {
52- return Err(BastionError::MaximumLength(name.len()));52+ return Err(CommunityError::MaximumLength(community_name.len()));
53 }53 }
54 54
55 Ok(())55 Ok(())