Init commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
use std::{
|
||||
io::{
|
||||
self,
|
||||
Error,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::GIT_DIR;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IndexEntry {
|
||||
file_size: u32,
|
||||
hash: [u8; 20],
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl IndexEntry {
|
||||
fn from_bytes(bytes: Vec<u8>) -> io::Result<Self> {
|
||||
|
||||
return Err(Error::other("Not implemented"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Index {
|
||||
version: u32,
|
||||
entries: Vec<IndexEntry>,
|
||||
}
|
||||
|
||||
impl Index {
|
||||
pub fn from_bytes(bytes: Vec<u8>) -> io::Result<Self> {
|
||||
let magic: [u8; 4] = match bytes.first_chunk() {
|
||||
None => return Err(Error::other("Error parsing index")),
|
||||
Some(magic) => *magic,
|
||||
};
|
||||
|
||||
match str::from_utf8(&magic) {
|
||||
Ok("DIRC") => (),
|
||||
_ => return Err(Error::other("Invalid index"))
|
||||
};
|
||||
|
||||
let version_bytes = <[u8; 4]>::try_from(&bytes.as_slice()[4..8]).unwrap();
|
||||
let version = u32::from_be_bytes(version_bytes);
|
||||
|
||||
let count_bytes = <[u8; 4]>::try_from(&bytes.as_slice()[8..12]).unwrap();
|
||||
let count = u32::from_be_bytes(count_bytes);
|
||||
|
||||
let entries: Vec<IndexEntry> = Vec::with_capacity(usize::try_from(count).unwrap());
|
||||
|
||||
for i in 0..=count {
|
||||
|
||||
};
|
||||
|
||||
return Ok(Index { version, entries});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user