@@ 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,
}
}