~yujiri/libsufec

ab5a30d6751896055e74415402e00d151b02b8e3 — Evin Yulo 2 years ago 729e3df
support file transfers
1 files changed, 8 insertions(+), 1 deletions(-)

M src/message.rs
M src/message.rs => src/message.rs +8 -1
@@ 12,11 12,13 @@ pub struct Message {
#[derive(Debug, Clone)]
pub enum MessageContent {
	Text(String),
	File(String, Vec<u8>),
}
impl MessageContent {
	pub fn to_bytes(&self) -> Vec<u8> {
		match self {
			MessageContent::Text(text) => [&[0], text.as_bytes()].concat()
			MessageContent::Text(text) => [&[0], text.as_bytes()].concat(),
			MessageContent::File(name, content) => [&[1], &[name.len() as u8], name.as_bytes(), content].concat(),
		}
	}
	pub fn from_bytes(bytes: &[u8]) -> Option<MessageContent> {


@@ 25,6 27,11 @@ impl MessageContent {
				let text = String::from_utf8(bytes[1..].to_vec()).ok()?;
				Some(MessageContent::Text(text))
			}
			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()))
			},
			_ => None,
		}
	}