From a72fbcc88ff8f31ed3a8f57bcf5172646d544858 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Mon, 20 Jan 2020 15:48:29 +0100 Subject: [PATCH] Improve error handling. Return to caller error codes as soon as they cannot be handled locally. --- src/database/db_core.rs | 9 ++++----- src/main.rs | 7 ++++--- src/storage/bincode.rs | 42 ++++++++++++++++++++++++----------------- src/storage/json.rs | 30 +++++++++++++++-------------- src/storage/model.rs | 2 +- src/storage/xyz.rs | 2 +- 6 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/database/db_core.rs b/src/database/db_core.rs index cfeec7d..87be942 100644 --- a/src/database/db_core.rs +++ b/src/database/db_core.rs @@ -85,8 +85,7 @@ impl Core { space_objects: Vec, scales: Option>>, max_elements: Option, - ) -> Self - //Result + ) -> Result where S: Into, { @@ -112,18 +111,18 @@ impl Core { for object in filtered.iter_mut() { let position: Vec = object.position().into(); - object.set_position(space.encode(&position).unwrap()); + object.set_position(space.encode(&position)?); } space_dbs.push(SpaceDB::new(&space, filtered, scales.clone(), max_elements)) } - Core { + Ok(Core { title: title.into(), version: version.into(), properties, space_db: space_dbs, - } + }) } pub fn name(&self) -> &String { diff --git a/src/main.rs b/src/main.rs index 691615f..c1959f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,14 +16,15 @@ fn main() { // Convert to binary the JSON data: if true { info_time!("Converting to binary JSON data"); - storage::json::from::>("10k.spaces"); - storage::json::from::>("10k.objects"); + storage::json::from::>("10k.spaces").unwrap(); + storage::json::from::>("10k.objects") + .unwrap(); } // Build a Database Index: if true { info_time!("Building database index"); - storage::bincode::build("10k", "v0.1", None, None); + storage::bincode::build("10k", "v0.1", None, None).unwrap(); } // Load a Database: diff --git a/src/storage/bincode.rs b/src/storage/bincode.rs index 492140a..131c3aa 100644 --- a/src/storage/bincode.rs +++ b/src/storage/bincode.rs @@ -1,5 +1,7 @@ use std::fs::File; use std::io::BufWriter; +use std::io::Error; +use std::io::ErrorKind; use memmap::Mmap; use serde::de::DeserializeOwned; @@ -7,33 +9,39 @@ use serde::Serialize; use super::model; -pub fn load(from: &str) -> T +pub fn load(from: &str) -> Result where T: DeserializeOwned, { - let file_in = - File::open(from).unwrap_or_else(|e| panic!("Unable to read file: {}: {}", from, e)); + let file_in = File::open(from)?; - let mmap = unsafe { - Mmap::map(&file_in) - .unwrap_or_else(|e| panic!("Unable to map in memory the file: {}: {}", from, e)) - }; + let mmap = unsafe { Mmap::map(&file_in)? }; - bincode::deserialize(&mmap[..]) - .unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e)) + match bincode::deserialize(&mmap[..]) { + Ok(data) => Ok(data), + Err(e) => Err(Error::new( + ErrorKind::InvalidData, + format!("Bincode could not deserialize: {:?}", e), + )), + } } -pub fn store(data: T, to: &str) +pub fn store(data: T, to: &str) -> Result<(), Error> where T: Serialize, { - let file_out = - File::create(to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); + let file_out = File::create(to)?; // We create a buffered writer from the file we get let writer = BufWriter::new(&file_out); - bincode::serialize_into(writer, &data).unwrap(); + match bincode::serialize_into(writer, &data) { + Ok(()) => Ok(()), + Err(e) => Err(Error::new( + ErrorKind::InvalidData, + format!("Bincode could not serialize: {:?}", e), + )), + } } pub fn build( @@ -41,19 +49,19 @@ pub fn build( version: &str, scales: Option>>, max_elements: Option, -) { +) -> Result<(), Error> { let fn_spaces = format!("{}.spaces.bin", name); let fn_objects = format!("{}.objects.bin", name); let fn_index = format!("{}.index", name); - let spaces = load::>(&fn_spaces) + let spaces = load::>(&fn_spaces)? .iter() .map(|s| s.into()) .collect::>(); - let objects = load::>(&fn_objects); + let objects = load::>(&fn_objects)?; let core = model::build_index(name, version, &spaces, &objects, scales, max_elements); - store((spaces, core), &fn_index); + store((spaces, core), &fn_index) } diff --git a/src/storage/json.rs b/src/storage/json.rs index da7cada..45992e0 100644 --- a/src/storage/json.rs +++ b/src/storage/json.rs @@ -1,33 +1,35 @@ use std::fs::File; use std::io::BufWriter; +use std::io::Error; +use std::io::ErrorKind; use memmap::Mmap; use serde::de::DeserializeOwned; use serde::Serialize; -fn convert(from: &str, to: &str) +fn convert(from: &str, to: &str) -> Result<(), Error> where T: Serialize + DeserializeOwned, { - let file_in = - File::open(from).unwrap_or_else(|e| panic!("Unable to read file: {}: {}", from, e)); - let file_out = - File::create(to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); + let file_in = File::open(from)?; + let file_out = File::create(to)?; // We create a buffered writer from the file we get let writer = BufWriter::new(&file_out); - let mmap = unsafe { - Mmap::map(&file_in) - .unwrap_or_else(|e| panic!("Unable to map in memory the file: {}: {}", from, e)) - }; - let v: T = serde_json::from_slice(&mmap[..]) - .unwrap_or_else(|e| panic!("Unable to parse the json data from: {}: {}", from, e)); + let mmap = unsafe { Mmap::map(&file_in)? }; + let v: T = serde_json::from_slice(&mmap[..])?; - bincode::serialize_into(writer, &v).unwrap(); + match bincode::serialize_into(writer, &v) { + Ok(()) => Ok(()), + Err(e) => Err(Error::new( + ErrorKind::InvalidData, + format!("Bincode could not serialize: {:?}", e), + )), + } } -pub fn from(name: &str) +pub fn from(name: &str) -> Result<(), Error> where T: Serialize + DeserializeOwned, { @@ -35,5 +37,5 @@ where let fn_in = format!("{}.json", name); let fn_out = format!("{}.bin", name); - convert::(&fn_in, &fn_out); + convert::(&fn_in, &fn_out) } diff --git a/src/storage/model.rs b/src/storage/model.rs index 147998f..52dfd92 100644 --- a/src/storage/model.rs +++ b/src/storage/model.rs @@ -251,7 +251,7 @@ pub fn build_index( objects: &[SpatialObject], scales: Option>>, max_elements: Option, -) -> Core { +) -> Result { let mut properties = vec![]; let mut space_set_objects = vec![]; { diff --git a/src/storage/xyz.rs b/src/storage/xyz.rs index b7286ad..5c6a50f 100644 --- a/src/storage/xyz.rs +++ b/src/storage/xyz.rs @@ -34,7 +34,7 @@ fn convert(string: &str) -> Result, Error> { "SCALE" => (), _ if values[0].starts_with("#A") => { // Update the oid value. - oid = Some(format!("{}", values[0].trim_start_matches("#"))); + oid = Some(values[0].trim_start_matches('#').to_string()); trace!("FOUND OID {:?}", oid); } _ if line.contains("WHS") => {