work
This commit is contained in:
67
src/net/data.rs
Normal file
67
src/net/data.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use rand::TryRngCore;
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ClientMsgInst {
|
||||
CreateAccountV0(CreateAccountV0) = 0,
|
||||
RequestMsgsV0 = 1,
|
||||
SendMsgV0(SendMsgV0) = 2,
|
||||
}
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ServerMsgInst {
|
||||
AccountCreatedV0(AccountCreatedV0) = 0,
|
||||
LoadMsgV0(LoadMsgV0) = 1,
|
||||
LoadMsgsV0(Vec<LoadMsgV0>) = 2,
|
||||
ServerErrorV0(ServerErrorV0) = 3,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct CreateAccountV0 {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub login_key: LoginKeyV0,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct AccountCreatedV0 {}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct LoginKeyV0(Vec<u8>);
|
||||
impl LoginKeyV0 {
|
||||
pub const BIT_LEN: usize = 1024;
|
||||
pub const BYTE_LEN: usize = Self::BIT_LEN / 8;
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut key = [0u8; Self::BYTE_LEN];
|
||||
rand::rngs::OsRng
|
||||
.try_fill_bytes(&mut key)
|
||||
.expect("failed to generate random key");
|
||||
Self(key.to_vec())
|
||||
}
|
||||
}
|
||||
impl From<Vec<u8>> for LoginKeyV0 {
|
||||
fn from(value: Vec<u8>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct SendMsgV0 {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct LoadMsgV0 {
|
||||
pub content: String,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ServerErrorV0 {
|
||||
NotLoggedIn,
|
||||
UnknownUsername,
|
||||
InvalidPassword,
|
||||
UsernameTaken,
|
||||
}
|
||||
Reference in New Issue
Block a user