~yujiri/libsufec

7ecbda160017d42a9de938746a9ec9cdfb2afb63 — Evin Yulo 1 year, 9 months ago cb32369
support contact sync messages
3 files changed, 30 insertions(+), 13 deletions(-)

A src/contact.rs
M src/lib.rs
M src/message.rs
A src/contact.rs => src/contact.rs +8 -0
@@ 0,0 1,8 @@
use crate::prelude::*;
use crate::addr::*;

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Contact {
	pub name: String,
	pub addr: SufecAddr,
}

M src/lib.rs => src/lib.rs +6 -11
@@ 2,23 2,18 @@ mod prelude;
mod addr;
mod account;
mod message;
mod contact;
mod room;
mod crypto;
mod error;
mod server;
mod error;
mod store;
mod room;

pub use account::*;
pub use addr::*;
pub use message::*;
pub use error::*;
pub use server::*;
pub use contact::*;
pub use room::*;
pub use server::*;
pub use error::*;
pub use store::*;
use prelude::*;

#[derive(Serialize, Deserialize, Clone)]
pub struct Contact {
	pub name: String,
	pub addr: SufecAddr,
}

M src/message.rs => src/message.rs +16 -2
@@ 1,5 1,6 @@
use crate::prelude::*;
use crate::addr::*;
use crate::contact::*;

#[derive(Debug)]
pub struct Message {


@@ 13,12 14,19 @@ pub struct Message {
pub enum MessageContent {
	Text(String),
	File(String, Vec<u8>),
	Contact(Contact),
}
impl MessageContent {
	pub fn to_bytes(&self) -> Vec<u8> {
		match self {
			MessageContent::Text(text) => [&[0], text.as_bytes()].concat(),
			MessageContent::File(name, content) => [&[1], &[name.len() as u8], name.as_bytes(), content].concat(),
			MessageContent::Contact(contact) => [
				[2].as_slice(),
				&[contact.addr.to_bytes().len() as u8],
				&contact.addr.to_bytes(),
				contact.name.as_bytes(),
			].concat(),
		}
	}
	pub fn from_bytes(bytes: &[u8]) -> Option<MessageContent> {


@@ 29,8 37,14 @@ impl MessageContent {
			}
			1 => {
				let name_len = *bytes.get(1)? as usize;
				let name = String::from_utf8(bytes[1..1+name_len].to_vec()).ok()?;
				Some(MessageContent::File(name, bytes[1+name_len..].to_vec()))
				let name = String::from_utf8(bytes[2..2+name_len].to_vec()).ok()?;
				Some(MessageContent::File(name, bytes[2+name_len..].to_vec()))
			},
			2 => {
				let addr_len = *bytes.get(1)? as usize;
				let addr = SufecAddr::from_bytes(&bytes[2..2+addr_len])?;
				let name = String::from_utf8(bytes[2+addr_len+PUBLICKEYBYTES..].to_vec()).ok()?;
				Some(MessageContent::Contact(Contact{addr, name}))
			},
			_ => None,
		}