partialy implement Glue Logic
This commit is contained in:
@ -11,7 +11,7 @@ mod image_convert;
|
||||
mod metadata_file;
|
||||
mod search;
|
||||
|
||||
pub type TextureFileResult = Result<Arc<Vec<u8>>, TextureFileError>;
|
||||
pub type TextureFileResult = Result<Vec<u8>, TextureFileError>;
|
||||
pub enum TextureFileError {
|
||||
NotFound,
|
||||
IoError(io::Error),
|
||||
@ -39,7 +39,7 @@ pub struct DataStore {
|
||||
id_index: HashMap<String, Texture>,
|
||||
name_index: HashMap<String, Texture>,
|
||||
|
||||
preview_cache: HashMap<(TextureFormat, Sha256), Arc<Vec<u8>>>,
|
||||
preview_cache: HashMap<(TextureFormat, Sha256), Vec<u8>>,
|
||||
}
|
||||
|
||||
impl DataStore {
|
||||
@ -75,7 +75,7 @@ impl DataStore {
|
||||
let metadata_file = metadata_file::MetadataFile::load(base_dir)?;
|
||||
|
||||
for texture in metadata_file.textures.iter() {
|
||||
match store.insert(texture.clone())? {
|
||||
match store.insert(texture.clone()) {
|
||||
true => (),
|
||||
false => {
|
||||
panic!("inserting {:#?} failed !!!", texture); // TODO: What should be done?
|
||||
@ -94,17 +94,17 @@ impl DataStore {
|
||||
self.name_index.get(id).cloned()
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, texture: Texture) -> io::Result<bool> {
|
||||
pub fn insert(&mut self, texture: Texture) -> bool {
|
||||
if self.id_index.contains_key(&texture.id) {
|
||||
return Ok(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.name_index.contains_key(&texture.name) {
|
||||
return Ok(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
if !self.is_texture_file_on_disk(&texture.texture_hash)? {
|
||||
return Ok(false);
|
||||
if !self.is_texture_file_on_disk(&texture.texture_hash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.id_index.insert(texture.id.clone(), texture.clone());
|
||||
@ -112,7 +112,7 @@ impl DataStore {
|
||||
.insert(texture.name.clone(), texture.clone());
|
||||
self.textures.insert(texture.clone());
|
||||
|
||||
Ok(true)
|
||||
true
|
||||
}
|
||||
|
||||
/// returns true if successful
|
||||
@ -132,28 +132,23 @@ impl DataStore {
|
||||
}
|
||||
|
||||
/// Check if the texture, given by hash, physically exists on the file system.
|
||||
pub fn is_texture_file_on_disk(&self, hash: &Sha256) -> io::Result<bool> {
|
||||
pub fn is_texture_file_on_disk(&self, hash: &Sha256) -> bool {
|
||||
let file_path = self.texture_file_path(&hash);
|
||||
|
||||
Ok(file_path.is_file())
|
||||
file_path.is_file()
|
||||
}
|
||||
|
||||
// Todo: this return type looks wrong : ImageError can't happen
|
||||
pub fn read_texture_file_by_hash(&mut self, hash: &Sha256) -> TextureFileResult {
|
||||
pub fn read_texture_file_by_hash(&self, hash: &Sha256) -> io::Result<Vec<u8>> {
|
||||
use std::fs::*;
|
||||
use std::io::*;
|
||||
|
||||
if !(self.is_texture_file_on_disk(&hash)?) {
|
||||
return Err(TextureFileError::NotFound);
|
||||
}
|
||||
|
||||
let file_path = self.texture_file_path(&hash);
|
||||
|
||||
let mut file = File::open(file_path)?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
|
||||
Ok(Arc::new(buffer))
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
pub fn store_texture_file(&mut self, data: &[u8]) -> io::Result<()> {
|
||||
|
Reference in New Issue
Block a user