use super::diff::StaticLocationDiffReader;
use super::shared::{StaticLocation, read_block_statics};
use crate::error::{MulReaderError, MulReaderResult};
use crate::mul::MulReader;
use std::fs::File;
use std::io::{Read, Seek};
use std::path::Path;
#[derive(Debug)]
pub struct StaticLocationReader<T: Read + Seek> {
mul_reader: MulReader<T>,
width: u32,
height: u32,
}
impl StaticLocationReader<File> {
pub fn new(
index_path: &Path,
mul_path: &Path,
width_blocks: u32,
height_blocks: u32,
) -> MulReaderResult<StaticLocationReader<File>> {
let mul_reader = MulReader::new(index_path, mul_path)?;
Ok(StaticLocationReader {
mul_reader,
width: width_blocks,
height: height_blocks,
})
}
}
impl<T: Read + Seek> StaticLocationReader<T> {
pub fn from_mul(
mul_reader: MulReader<T>,
width_blocks: u32,
height_blocks: u32,
) -> StaticLocationReader<T> {
StaticLocationReader {
mul_reader,
width: width_blocks,
height: height_blocks,
}
}
pub fn read_block(
&mut self,
id: u32,
patch: Option<&mut StaticLocationDiffReader<T>>,
) -> MulReaderResult<Vec<StaticLocation>> {
match patch {
Some(reader) => reader
.read(id)
.unwrap_or_else(|| read_block_statics(&mut self.mul_reader, id)),
None => read_block_statics(&mut self.mul_reader, id),
}
}
pub fn read_block_from_coordinates(
&mut self,
x: u32,
y: u32,
patch: Option<&mut StaticLocationDiffReader<T>>,
) -> MulReaderResult<Vec<StaticLocation>> {
let width = self.width;
let height = self.height;
if x < width && y < height {
self.read_block(y + (x * height), patch)
} else {
Err(MulReaderError::CoordinatesOutOfBounds { x, y })
}
}
}