back to all but corruption tests green

This commit is contained in:
Cyryl Płotnicki 2021-05-15 21:07:32 +01:00
parent c6e34c5756
commit a95b5d840f
2 changed files with 16 additions and 6 deletions

View file

@ -1,3 +1,4 @@
use log::error;
use std::collections::HashMap;
use vfs::VfsPath;
@ -93,9 +94,12 @@ impl Index {
}
fn load_from_file(index_file_path: &VfsPath) -> Result<Self> {
let index_text = index_file_path
.read_to_string()
.context(format!("reading index file contents from {}", index_file_path.as_str()))?;
let mut file = index_file_path.open_file()?;
let mut encoded = vec![];
file.read_to_end(&mut encoded)?;
let decoded = error_correcting_encoder::decode(&encoded)?;
let index_text = String::from_utf8(decoded)?;
let index: Index =
serde_json::from_str(&index_text).context(format!("cannot read index from: {}", index_file_path.as_str()))?;

View file

@ -1,6 +1,7 @@
use std::io::Read;
use anyhow::Result;
use anyhow::*;
use reed_solomon::Decoder;
use reed_solomon::Encoder;
const ECC_LENGTH: usize = 8;
@ -11,8 +12,13 @@ pub fn encode(bytes: &[u8]) -> Result<Vec<u8>> {
Ok(encoded.bytes().collect::<Result<Vec<u8>, _>>()?)
}
pub fn decode(bytes: &[u8]) -> Result<&[u8]> {
Ok(bytes)
pub fn decode(bytes: &[u8]) -> Result<Vec<u8>> {
let decoder = Decoder::new(ECC_LENGTH);
let maybe_corrected = decoder.correct(bytes, None);
match maybe_corrected {
Ok(corrected) => Ok(corrected.data().to_vec()),
Err(_) => Err(anyhow!("")),
}
}
mod must {