Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 6 | #[repr(u8)] |
| 7 | pub enum Kind { |
| 8 | Blob = 1, |
| 9 | /// A big file: a list of (chunk blob ID, length). |
| 10 | Chunks = 2, |
| 11 | Tree = 3, |
| 12 | } |
| 13 |
| 14 | impl Kind { |
| 15 | pub fn from_u8(b: u8) -> Option<Kind> { |
| 16 | match b { |
| 17 | 1 => Some(Kind::Blob), |
| 18 | 2 => Some(Kind::Chunks), |
| 19 | 3 => Some(Kind::Tree), |
| 20 | _ => None, |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 |
| 25 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 26 | pub struct Object { |
| 27 | pub kind: Kind, |
| 28 | pub data: Vec<u8>, |
| 29 | } |
| 30 |
| 31 | impl Object { |
| 32 | pub fn new(kind: Kind, data: Vec<u8>) -> Object { |
| 33 | Object { kind, data } |
| 34 | } |
| 35 |
| 36 | pub fn blob(data: Vec<u8>) -> Object { |
| 37 | Object { kind: Kind::Blob, data } |
| 38 | } |
| 39 |
| 40 | pub fn id(&self) -> Id { |
| 41 | Id::of(self.kind as u8, &self.data) |
| 42 | } |
| 43 | } |
| 44 |
| 45 | /// A big file's chunks, in order. |
| 46 | pub fn encode_chunks(chunks: &[(Id, u64)]) -> Vec<u8> { |
| 47 | let mut out = Vec::with_capacity(chunks.len() * 40); |
| 48 | for (id, len) in chunks { |
| 49 | out.extend_from_slice(&id.0); |
| 50 | out.extend_from_slice(&len.to_le_bytes()); |
| 51 | } |
| 52 | out |
| 53 | } |
| 54 |
| 55 | pub fn decode_chunks(data: &[u8]) -> Result<Vec<(Id, u64)>> { |
| 56 | if data.len() % 40 != 0 { |
| 57 | return Err(corrupt("bad chunk list")); |
| 58 | } |
| 59 | Ok(data |
| 60 | .chunks(40) |
| 61 | .map(|c| (Id::from_slice(&c[..32]).unwrap(), u64::from_le_bytes(c[32..40].try_into().unwrap()))) |
| 62 | .collect()) |
| 63 | } |
| 64 |