Remove unwraps from library code

main
_ 2020-03-08 02:32:17 +00:00
parent e980599347
commit b8e0429095
4 changed files with 47 additions and 17 deletions

View File

@ -123,8 +123,8 @@ fn make_object_space_vec (inverse_model_mat: &Mat4, world_space_vec: &Vec3)
fn renderable_from_iqm_file <P> (filename: P) -> RenderableModel
where P: AsRef <std::path::Path>
{
let data = load_small_file (filename, 1024 * 1024);
let model = Model::from_slice (&data);
let data = load_small_file (filename, 1024 * 1024).unwrap ();
let model = Model::from_slice (&data).unwrap ();
RenderableModel::from_iqm (&model)
}
@ -222,6 +222,7 @@ fn main () {
let mesh_pumpkin = renderable_from_iqm_file ("pumpking.iqm");
let mesh_sky = renderable_from_iqm_file ("sky-sphere.iqm");
let mesh_pitch = renderable_from_iqm_file ("pitch.iqm");
let mesh_arrow = renderable_from_iqm_file ("arrow.iqm");
let orange = color_from_255 ((210.0, 125.0, 44.0));
let green = color_from_255 ((52.0, 101.0, 36.0));

View File

@ -3,19 +3,32 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
pub fn load_small_file <P> (name: P, max_size: u64) -> Vec <u8>
#[derive (Debug)]
pub enum LoadFileErr {
FileTooBig,
Io (std::io::Error),
}
impl From <std::io::Error> for LoadFileErr {
fn from (e: std::io::Error) -> Self {
Self::Io (e)
}
}
pub fn load_small_file <P> (name: P, max_size: u64)
-> Result <Vec <u8>, LoadFileErr>
where P: AsRef <Path>
{
let mut f = File::open (name).unwrap ();
let len = f.metadata ().unwrap ().len ();
let mut f = File::open (name)?;
let len = f.metadata ()?.len ();
if len > max_size {
panic! ("File is too big");
return Err (LoadFileErr::FileTooBig);
}
let mut data = vec! [0u8; len.try_into ().unwrap ()];
f.read_exact (&mut data [..]).unwrap ();
f.read_exact (&mut data [..])?;
data
Ok (data)
}

View File

@ -180,9 +180,19 @@ impl VertexArray {
}
}
#[derive (Debug)]
pub enum ModelLoadErr {
ParseHeaderFailed,
ParseMeshFailed (usize),
ParseVertexArrayFailed (usize),
}
impl <'a> Model <'a> {
pub fn from_slice (data: &'a [u8]) -> Model <'a> {
let header = Header::from_slice (data).unwrap ().1;
pub fn from_slice (data: &'a [u8]) -> Result <Model <'a>, ModelLoadErr> {
let header = match Header::from_slice (data) {
Ok ((_, h)) => h,
Err (_) => return Err (ModelLoadErr::ParseHeaderFailed),
};
let text = {
let offset: usize = header.fields [consts::OFS_TEXT].try_into ().unwrap ();
@ -200,7 +210,12 @@ impl <'a> Model <'a> {
let offset = meshes_offset + i * mesh_size;
let mesh_slice = &data [offset..offset + mesh_size];
meshes.push (Mesh::from_slice (mesh_slice).unwrap ().1);
let mesh = match Mesh::from_slice (mesh_slice) {
Ok ((_, m)) => m,
Err (_) => return Err (ModelLoadErr::ParseMeshFailed (i)),
};
meshes.push (mesh);
}
meshes
};
@ -215,9 +230,10 @@ impl <'a> Model <'a> {
let offset = vertexarrays_offset + i * vertexarray_size;
let vertexarray_slice = &data [offset..offset + vertexarray_size];
let vertexarray = VertexArray::from_slice (vertexarray_slice).unwrap ().1;
//println! ("{:?}", vertexarray);
let vertexarray = match VertexArray::from_slice (vertexarray_slice) {
Ok ((_, va)) => va,
Err (_) => return Err (ModelLoadErr::ParseVertexArrayFailed (i)),
};
vertexarrays.push (vertexarray);
}
@ -225,14 +241,14 @@ impl <'a> Model <'a> {
vertexarrays
};
Model {
Ok (Model {
data,
header,
text,
meshes,
vertexarrays,
}
})
}
pub fn get_vertex_slice (&self,

View File

@ -60,7 +60,7 @@ impl ShaderObject {
-> Result <ShaderObject, String>
where P: AsRef <std::path::Path>
{
let src = crate::file::load_small_file (filename, 1024 * 1024);
let src = crate::file::load_small_file (filename, 1024 * 1024).unwrap ();
Self::from_source (shader_type, &src)
}
}