Browse Source

did some bad documentation comments

master
Isabelle L. 5 years ago
parent
commit
c3c55c624e
2 changed files with 11 additions and 0 deletions
  1. +9
    -0
      src/lib.rs
  2. +2
    -0
      src/message.rs

+ 9
- 0
src/lib.rs View File

@@ -1,3 +1,4 @@
//! # Isabelle's Lazy Message Protocol
#![allow(dead_code)] #![allow(dead_code)]


use futures_util::io::{AsyncReadExt, AsyncWriteExt}; use futures_util::io::{AsyncReadExt, AsyncWriteExt};
@@ -14,17 +15,20 @@ pub type Result<T> = std::result::Result<T, Error>;


struct NetworkPacket(Vec<u8>); struct NetworkPacket(Vec<u8>);


/// A type of data that can be sent
pub trait Sendable: Sized { pub trait Sendable: Sized {
fn to_packet(self) -> Result<Packet>; fn to_packet(self) -> Result<Packet>;
fn from_packet(packet: Packet) -> Result<Self>; fn from_packet(packet: Packet) -> Result<Self>;
} }


/// Data to be sent
pub struct Packet { pub struct Packet {
kind: PacketKind, kind: PacketKind,
contents: Vec<u8>, contents: Vec<u8>,
} }


impl Packet { impl Packet {
/// Create a new `Packet`
pub fn new(kind: PacketKind, contents: Vec<u8>) -> Packet { pub fn new(kind: PacketKind, contents: Vec<u8>) -> Packet {
Packet { kind, contents } Packet { kind, contents }
} }
@@ -44,6 +48,7 @@ impl Packet {
} }
} }


/// reads a `Packet` from a stream
pub async fn read<S>(stream: &mut S) -> Result<Option<Packet>> pub async fn read<S>(stream: &mut S) -> Result<Option<Packet>>
where where
S: AsyncReadExt + Unpin, S: AsyncReadExt + Unpin,
@@ -65,6 +70,7 @@ where
Ok(Some(packet)) Ok(Some(packet))
} }


/// Writes a `Sendable` packet to a stream
pub async fn write<S, P>(stream: &mut S, packet: P) -> Result<()> pub async fn write<S, P>(stream: &mut S, packet: P) -> Result<()>
where where
S: AsyncWriteExt + Unpin, S: AsyncWriteExt + Unpin,
@@ -75,13 +81,16 @@ where
Ok(()) Ok(())
} }


/// Kinds of packets that can be sent
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum PacketKind { pub enum PacketKind {
Message = 0, Message = 0,
PublicKey = 1,
} }


impl PacketKind { impl PacketKind {
/// returns `Option<PacketKind> given valid matching variant
pub fn from_u8(kind: u8) -> Option<PacketKind> { pub fn from_u8(kind: u8) -> Option<PacketKind> {
match kind { match kind {
0 => Some(PacketKind::Message), 0 => Some(PacketKind::Message),


+ 2
- 0
src/message.rs View File

@@ -3,6 +3,7 @@ use chrono::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;


/// a standard message from a user
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message { pub struct Message {
pub timestamp: i64, pub timestamp: i64,
@@ -12,6 +13,7 @@ pub struct Message {
} }


impl Message { impl Message {
/// create a new message
pub fn new(username: String, contents: String) -> Message { pub fn new(username: String, contents: String) -> Message {
let timestamp = Utc::now().timestamp(); let timestamp = Utc::now().timestamp();
let message_id = Uuid::new_v4().as_u128(); let message_id = Uuid::new_v4().as_u128();


Loading…
Cancel
Save