Change new to BDFWriter/Reader to accept a File

master
Trivernis 5 years ago
parent 83c776fb4f
commit 886ac5ee49

@ -1,6 +1,6 @@
[package] [package]
name = "bdflib" name = "bdflib"
version = "0.1.5" version = "0.2.0"
authors = ["trivernis <trivernis@gmail.com>"] authors = ["trivernis <trivernis@gmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -13,8 +13,7 @@ use std::io::BufReader;
fn main() { fn main() {
let f = File::open("dictionary.bdf").unwrap(); let f = File::open("dictionary.bdf").unwrap();
let buf_reader = BufReader::new(f); let mut bdf_reader = BDFReader::new(f);
let mut bdf_reader = BDFReader::new(buf_reader);
bdf_reader.read_metadata().unwrap(); bdf_reader.read_metadata().unwrap();
let lookup_table = bdf_reader.read_lookup_table().unwrap(); let lookup_table = bdf_reader.read_lookup_table().unwrap();
let lookup_table = lookup_table.clone(); let lookup_table = lookup_table.clone();
@ -37,9 +36,8 @@ use std::convert::Into;
fn main() { fn main() {
let f = File::create("dictionary.bdf").unwrap(); let f = File::create("dictionary.bdf").unwrap();
let buf_writer = BufWriter::new(f);
let entry_count = 1; let entry_count = 1;
let mut bdf_writer = BDFWriter::new(buf_writer, entry_count, false); let mut bdf_writer = BDFWriter::new(f, entry_count, false);
bdf_writer.add_lookup_entry(HashEntry::new("fakehash".into(), 3)).unwrap(); bdf_writer.add_lookup_entry(HashEntry::new("fakehash".into(), 3)).unwrap();
let mut entry = DataEntry::new("foo".into()); let mut entry = DataEntry::new("foo".into());
entry.add_hash_value("fakehash".into(), vec![0, 2, 3]); entry.add_hash_value("fakehash".into(), vec![0, 2, 3]);

@ -34,12 +34,12 @@ impl BDFWriter {
/// bar for how many entries were read. /// bar for how many entries were read.
/// If the `compress` parameter is true, each data chunk will be compressed /// If the `compress` parameter is true, each data chunk will be compressed
/// using lzma with a default level of 1. /// using lzma with a default level of 1.
pub fn new(writer: BufWriter<File>, entry_count: u64, compress: bool) -> Self { pub fn new(inner: File, entry_count: u64, compress: bool) -> Self {
Self { Self {
metadata: MetaChunk::new(entry_count, ENTRIES_PER_CHUNK, compress), metadata: MetaChunk::new(entry_count, ENTRIES_PER_CHUNK, compress),
lookup_table: HashLookupTable::new(HashMap::new()), lookup_table: HashLookupTable::new(HashMap::new()),
data_entries: Vec::new(), data_entries: Vec::new(),
writer, writer: BufWriter::new(inner),
head_written: false, head_written: false,
compressed: compress, compressed: compress,
compression_level: 1, compression_level: 1,
@ -134,11 +134,11 @@ impl BDFWriter {
impl BDFReader { impl BDFReader {
/// Creates a new BDFReader /// Creates a new BDFReader
pub fn new(reader: BufReader<File>) -> Self { pub fn new(inner: File) -> Self {
Self { Self {
metadata: None, metadata: None,
lookup_table: None, lookup_table: None,
reader, reader: BufReader::new(inner),
compressed: false, compressed: false,
} }
} }
@ -226,6 +226,7 @@ impl BDFReader {
data, data,
crc, crc,
}; };
if gen_chunk.name == DTBL_CHUNK_NAME.to_string() && self.compressed { if gen_chunk.name == DTBL_CHUNK_NAME.to_string() && self.compressed {
gen_chunk.decompress()?; gen_chunk.decompress()?;
} }

@ -5,7 +5,7 @@ mod tests {
use crate::chunks::{DataEntry, HashEntry}; use crate::chunks::{DataEntry, HashEntry};
use crate::io::BDFReader; use crate::io::BDFReader;
use std::fs::{remove_file, File}; use std::fs::{remove_file, File};
use std::io::{BufReader, BufWriter, Error}; use std::io::Error;
const FOO: &str = "foo"; const FOO: &str = "foo";
const BAR: &str = "bar"; const BAR: &str = "bar";
@ -106,16 +106,14 @@ mod tests {
fn new_reader(file_name: &str) -> Result<BDFReader, Error> { fn new_reader(file_name: &str) -> Result<BDFReader, Error> {
let file = File::open(file_name)?; let file = File::open(file_name)?;
let f = BufReader::new(file);
Ok(BDFReader::new(f)) Ok(BDFReader::new(file))
} }
fn new_writer(file_name: &str, entries: u64, compress: bool) -> Result<BDFWriter, Error> { fn new_writer(file_name: &str, entries: u64, compress: bool) -> Result<BDFWriter, Error> {
let file = File::create(file_name)?; let file = File::create(file_name)?;
let f = BufWriter::new(file);
Ok(BDFWriter::new(f, entries, compress)) Ok(BDFWriter::new(file, entries, compress))
} }
} }

Loading…
Cancel
Save