//! The messages of a sync, each a kind byte and its fields: paths as a //! length byte and UTF-8, numbers little-endian. use crate::{Agreed, Entry, Hash}; use alloc::string::String; use alloc::vec::Vec; /// This device's name. pub const HELLO: u8 = 1; /// Entries of the manifest, as many as fit. pub const LIST: u8 = 2; pub const LIST_END: u8 = 3; /// The deciding device asks for a file (its path). pub const GET: u8 = 4; /// The answer to a GET: path, size (`u64::MAX`: gone), hash; its pieces follow. pub const FILE: u8 = 5; /// A file for the other device to write: path, size, hash; its pieces follow. pub const PUT: u8 = 6; pub const PIECE_KIND: u8 = 7; /// A PUT written (1) or not (0). pub const ACK: u8 = 8; /// The other device is to remove a file (its path). pub const REMOVE: u8 = 9; /// The deciding device has no more; the other answers it when it has done /// all it was asked. pub const DONE: u8 = 10; /// The most bytes of a message's fields: under a channel message. const MESSAGE: usize = 4000; pub fn hello(name: &str) -> Vec { let mut m = alloc::vec![HELLO]; m.extend_from_slice(&name.as_bytes()[..name.len().min(64)]); m } pub fn path_message(kind: u8, path: &str) -> Vec { [&[kind][..], path.as_bytes()].concat() } pub fn file(kind: u8, path: &str, size: u64, hash: &Hash) -> Vec { let mut m = alloc::vec![kind]; put_path(&mut m, path); m.extend_from_slice(&size.to_le_bytes()); m.extend_from_slice(hash); m } /// Items in messages of `kind`, as many in each as fit. pub fn pages(kind: u8, items: impl Iterator>) -> Vec> { let mut pages = Vec::new(); let mut page = alloc::vec![kind]; for item in items { if page.len() + item.len() > MESSAGE { pages.push(core::mem::replace(&mut page, alloc::vec![kind])); } page.extend_from_slice(&item); } if page.len() > 1 { pages.push(page); } pages } fn put_path(out: &mut Vec, path: &str) { let b = &path.as_bytes()[..path.len().min(255)]; out.push(b.len() as u8); out.extend_from_slice(b); } pub fn put_entry(out: &mut Vec, e: &Entry) { put_path(out, &e.path); out.extend_from_slice(&e.size.to_le_bytes()); out.extend_from_slice(&e.modified.to_le_bytes()); out.extend_from_slice(&e.hash); } pub fn put_agreed(out: &mut Vec, a: &Agreed) { put_path(out, &a.path); out.extend_from_slice(&a.size.to_le_bytes()); out.extend_from_slice(&a.hash); } pub struct Reader<'a>(pub &'a [u8]); impl Reader<'_> { fn take(&mut self, n: usize) -> Option<&[u8]> { if self.0.len() < n { return None; } let (a, b) = self.0.split_at(n); self.0 = b; Some(a) } pub fn path(&mut self) -> Option { let n = usize::from(*self.take(1)?.first()?); Some(String::from_utf8_lossy(self.take(n)?).into_owned()) } pub fn u64(&mut self) -> Option { Some(u64::from_le_bytes(self.take(8)?.try_into().ok()?)) } pub fn hash(&mut self) -> Option { self.take(32)?.try_into().ok() } pub fn entry(&mut self) -> Option { Some(Entry { path: self.path()?, size: self.u64()?, modified: self.u64()?, hash: self.hash()?, }) } pub fn agreed(&mut self) -> Option { Some(Agreed { path: self.path()?, size: self.u64()?, hash: self.hash()?, }) } }