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,6 +1,11 @@
extern crate weave;
extern crate alchemy;
use weave::Error;
use alchemy::{Converter, Endianess, PlatformEndian, Transmutable};
use alchemy::platform_to_network_order;
@ -9,7 +14,6 @@ use alchemy::platform_to_network_order;
fn use_converter()
{
let num: u16;
let final_num: u16;
let mut buffer: Vec<u8>;
// Initialize the variables.
@ -25,14 +29,23 @@ fn use_converter()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a short number.
final_num = PlatformEndian::bytes_to_u16(buffer.as_slice());
println!("The buffer converts back to: {}", final_num);
match PlatformEndian::bytes_to_u16(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: u16;
let final_num: u16;
let endianess: Endianess;
let mut buffer: Vec<u8>;
@ -50,8 +63,19 @@ pub fn use_transmutable()
println!("Buffer contains: {}", stringify_array(buffer.as_slice()));
// Convert the array of bytes into a short number.
final_num = u16::from_endian_bytes(buffer.as_slice(), endianess);
println!("The buffer converts back to: {}", final_num);
match u16::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.
@ -105,6 +129,9 @@ pub fn main()
println!("");
println!("This turns into a network value of: {}",
platform_to_network_order(32832u16));
match platform_to_network_order(32832u16)
{
Ok(val) => { println!("This turns into a network value of: {}", val); }
Err(error) => { println!("{}\n{}", error, error.get_description()); }
}
}