The conversion of an Array of bytes to a value can now fail gracefully.

This was added so that parsers can detect problems converting
a value and fail in an expected way.
This commit is contained in:
2018-01-05 19:20:02 -05:00
parent 3a1b847c51
commit ff3558ea37
12 changed files with 505 additions and 396 deletions

View File

@ -1,7 +1,9 @@
extern crate weave;
extern crate alchemy;
use weave::Error;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
@ -9,7 +11,6 @@ use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
fn use_converter()
{
let num: f32;
let final_num: f32;
let mut buffer: Vec<u8>;
// Initialize the variables.
@ -25,14 +26,23 @@ fn use_converter()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = PlatformEndian::bytes_to_f32(buffer.as_slice());
println!("The buffer converts back to: {}", final_num);
match PlatformEndian::bytes_to_f32(buffer.as_slice())
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
pub fn use_transmutable()
{
let num: f32;
let final_num: f32;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -50,8 +60,18 @@ pub fn use_transmutable()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a floating point number.
final_num = f32::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
match f32::from_endian_bytes(buffer.as_slice(), endianess)
{
Ok(final_num) =>
{
println!("The buffer converts back to: {}", final_num);
}
Err(error) =>
{
println!("{}\n{}", error, error.get_description());
}
}
}
/// This just help pretty up the printing of an array of bytes.