pub fn next_from_buffer(buffer: &mut BufReader<File>) -> Result<Chunk, ChunkError> {
		let mut chunk = Chunk::empty();
		let mut length_buf = [0u8; 4];

		match buffer.read_exact(&mut length_buf) {
			Ok(_) => { chunk.length = u32::from_be_bytes(length_buf); },
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk length: {}", e)))
		}

		let mut crc_buf = vec![0u8; (chunk.length + 4) as usize];

		match buffer.read_exact(&mut crc_buf[0..4]) {
			Ok(_) => {
				chunk.ch_type[0] = crc_buf[0];
				chunk.ch_type[1] = crc_buf[1];
				chunk.ch_type[2] = crc_buf[2];
				chunk.ch_type[3] = crc_buf[3];
				ChunkProperties::from_type(&chunk.ch_type, &mut chunk.properties);
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk type: {}", e)))
		}

		let crc_value: [u8; 4];

		match buffer.read_exact(&mut crc_buf[4..]) {
			Ok(_) => {
				crc_value = Self::generate_crc(&crc_buf);
				chunk.data = crc_buf[4..].into(); // TODO: Try to make this a no-copy operation
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk data: {}", e)))
		}

		match buffer.read_exact(&mut chunk.crc) {
			Ok(_) => {
				if crc_value != chunk.crc {
					println!("Calculated CRC: {:?}, original CRC: {:?}", crc_value, chunk.crc);
					return Err(ChunkError::new("Chunk data may be corrupted".into()));
				}
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk CRC: {}", e)))
		}

		chunk.location = Chunk::get_location(&chunk.ch_type);
		Ok(chunk)
	}
// TODO: Redo this, it's pretty messy
Redo this, it's pretty messy
Redo this it ' s pretty messy
pub fn next_from_buffer(buffer: &mut BufReader<File>) -> Result<Chunk, ChunkError> {
		let mut chunk = Chunk::empty();
		let mut length_buf = [0u8; 4];
		match buffer.read_exact(&mut length_buf) {
			Ok(_) => { chunk.length = u32::from_be_bytes(length_buf); },
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk length: {}", e)))
		}
		let mut crc_buf = vec![0u8; (chunk.length + 4) as usize];
		match buffer.read_exact(&mut crc_buf[0..4]) {
			Ok(_) => {
				chunk.ch_type[0] = crc_buf[0];
				chunk.ch_type[1] = crc_buf[1];
				chunk.ch_type[2] = crc_buf[2];
				chunk.ch_type[3] = crc_buf[3];
				ChunkProperties::from_type(&chunk.ch_type, &mut chunk.properties);
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk type: {}", e)))
		}
		let crc_value: [u8; 4];
		match buffer.read_exact(&mut crc_buf[4..]) {
			Ok(_) => {
				crc_value = Self::generate_crc(&crc_buf);
				chunk.data = crc_buf[4..].into(); // TODO: Try to make this a no-copy operation
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk data: {}", e)))
		}
		match buffer.read_exact(&mut chunk.crc) {
			Ok(_) => {
				if crc_value != chunk.crc {
					println!("Calculated CRC: {:?}, original CRC: {:?}", crc_value, chunk.crc);
					return Err(ChunkError::new("Chunk data may be corrupted".into()));
				}
			},
			Err(e) => return Err(ChunkError::new(format!("Failed to read chunk CRC: {}", e)))
		}
		chunk.location = Chunk::get_location(&chunk.ch_type);
		Ok(chunk)
	}
pub fn next_from_buffer ( buffer : & mut BufReader < File > ) -> Result < Chunk , ChunkError > { let mut chunk = Chunk :: empty ( ) ; let mut length_buf = [ 0u8 ; 4 ] ; match buffer . read_exact ( & mut length_buf ) { Ok ( _ ) => { chunk . length = u32 :: from_be_bytes ( length_buf ) ; } , Err ( e ) => return Err ( ChunkError :: new ( format ! ( " " e ) ) ) } let mut crc_buf = vec ! [ 0u8 ( chunk length 4 ) as usize ] ; match buffer . read_exact ( & mut crc_buf [ 0 .. 4 ] ) { Ok ( _ ) => { chunk . ch_type [ 0 ] = crc_buf [ 0 ] ; chunk . ch_type [ 1 ] = crc_buf [ 1 ] ; chunk . ch_type [ 2 ] = crc_buf [ 2 ] ; chunk . ch_type [ 3 ] = crc_buf [ 3 ] ; ChunkProperties :: from_type ( & chunk . ch_type , & mut chunk . properties ) ; } , Err ( e ) => return Err ( ChunkError :: new ( format ! ( " " e ) ) ) } let crc_value : [ u8 ; 4 ] ; match buffer . read_exact ( & mut crc_buf [ 4 .. ] ) { Ok ( _ ) => { crc_value = Self :: generate_crc ( & crc_buf ) ; chunk . data = crc_buf [ 4 .. ] . into ( ) ; // TODO: Try to make this a no-copy operation } , Err ( e ) => return Err ( ChunkError :: new ( format ! ( " " e ) ) ) } match buffer . read_exact ( & mut chunk . crc ) { Ok ( _ ) => { if crc_value != chunk . crc { println ! ( " " crc_value chunk crc ) ; return Err ( ChunkError :: new ( " " . into ( ) ) ) ; } } , Err ( e ) => return Err ( ChunkError :: new ( format ! ( " " e ) ) ) } chunk . location = Chunk :: get_location ( & chunk . ch_type ) ; Ok ( chunk ) }
TODO: Redo this, it's pretty messy
TODO : Redo this it ' s pretty messy
buffer
&mut BufReader<File>