fmt + clippy

pull/1/head
Joey Hines 2021-10-09 14:00:49 -06:00
parent bb6f331c75
commit ef8918d517
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
5 changed files with 105 additions and 49 deletions

View File

@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use num_bigint::BigUint;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug)]
pub enum ByteStreamError {
@ -9,7 +9,7 @@ pub enum ByteStreamError {
impl Display for ByteStreamError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ByteStreamError::OutOfRange => write!(f, "Requested values out of range")
ByteStreamError::OutOfRange => write!(f, "Requested values out of range"),
}
}
}
@ -33,14 +33,15 @@ pub struct ByteStream {
data: Vec<u8>,
}
impl ByteStream {
pub fn get_bytes(&self, bit_ndx: usize, bit_count: usize) -> Result<Vec<u8>, ByteStreamError> {
let byte_ndx = bit_ndx / 8;
let bits_before = (bit_ndx % 8) as u8;
let bits_in_last_byte = ((bit_ndx + bit_count - bits_before as usize) % 8) as u8;
let byte_count = ((bit_count as f32) / (8.0)).ceil() as usize;
let bytes_needed = if (bits_before as usize + bit_count) % 8 != 0 && !(bits_before as usize + bit_count < 8){
let bytes_needed = if (bits_before as usize + bit_count) % 8 != 0
&& bits_before as usize + bit_count >= 8
{
byte_count + 1
} else {
byte_count

View File

@ -1,11 +1,11 @@
use crate::byte_stream::{ByteStream, bit_mask, ByteStreamError};
use serde::Deserialize;
use std::fmt::{Write, Display, Formatter};
use num_bigint::{BigUint, BigInt};
use std::io::Cursor;
use byteorder::{LittleEndian, ReadBytesExt};
use std::string::FromUtf8Error;
use crate::byte_stream::{bit_mask, ByteStream, ByteStreamError};
use crate::formatter::printers::print_bytes_as_array;
use byteorder::{LittleEndian, ReadBytesExt};
use num_bigint::{BigInt, BigUint};
use serde::Deserialize;
use std::fmt::{Display, Formatter, Write};
use std::io::Cursor;
use std::string::FromUtf8Error;
#[derive(Debug, Clone)]
pub enum FormatError {
@ -32,7 +32,7 @@ impl Display for FormatError {
match self {
FormatError::ByteSteamError(e) => writeln!(f, "Byte stream error: {}", e),
FormatError::NotSupported => write!(f, "Field type not supported"),
FormatError::StringParseError(e) => write!(f, "String parse error: {}", e)
FormatError::StringParseError(e) => write!(f, "String parse error: {}", e),
}
}
}
@ -63,7 +63,11 @@ pub struct Field {
}
impl Field {
fn format_int(byte_stream: &ByteStream, bit_ndx: usize, bit_width: usize) -> Result<(String, usize), FormatError> {
fn format_int(
byte_stream: &ByteStream,
bit_ndx: usize,
bit_width: usize,
) -> Result<(String, usize), FormatError> {
let mut bytes = byte_stream.get_bytes(bit_ndx, bit_width)?;
if let Some(last_byte) = bytes.last_mut() {
@ -78,34 +82,47 @@ impl Field {
let big_int = BigInt::from_signed_bytes_le(&bytes);
Ok((big_int.to_string(), bit_width))
}
else {
} else {
Err(ByteStreamError::OutOfRange.into())
}
}
fn format_uint(byte_stream: &ByteStream, bit_ndx: usize, bit_width: usize) -> Result<(String, usize), FormatError> {
fn format_uint(
byte_stream: &ByteStream,
bit_ndx: usize,
bit_width: usize,
) -> Result<(String, usize), FormatError> {
let bytes = byte_stream.get_bytes(bit_ndx, bit_width)?;
let big_int = BigUint::from_bytes_le(&bytes);
Ok((big_int.to_string(), bit_width))
}
fn format_float(byte_stream: &ByteStream, bit_ndx: usize) -> Result<(String, usize), FormatError> {
fn format_float(
byte_stream: &ByteStream,
bit_ndx: usize,
) -> Result<(String, usize), FormatError> {
let bytes = byte_stream.get_bytes(bit_ndx, 32)?;
let mut cursor = Cursor::new(bytes);
Ok((cursor.read_f32::<LittleEndian>().unwrap().to_string(), 4))
}
fn format_double(byte_stream: &ByteStream, bit_ndx: usize) -> Result<(String, usize), FormatError> {
fn format_double(
byte_stream: &ByteStream,
bit_ndx: usize,
) -> Result<(String, usize), FormatError> {
let bytes = byte_stream.get_bytes(bit_ndx, 64)?;
let mut cursor = Cursor::new(bytes);
Ok((cursor.read_f64::<LittleEndian>().unwrap().to_string(), 4))
}
fn format_string(byte_stream: &ByteStream, mut bit_ndx: usize, max_byte_len: usize) -> Result<(String, usize), FormatError> {
fn format_string(
byte_stream: &ByteStream,
mut bit_ndx: usize,
max_byte_len: usize,
) -> Result<(String, usize), FormatError> {
let mut string_bytes = Vec::new();
for _ in 0..max_byte_len {
@ -123,14 +140,17 @@ impl Field {
Ok((String::from_utf8(string_bytes)?, byte_count))
}
fn format_bytes(byte_stream: &ByteStream, bit_ndx: usize, max_byte_len: usize) -> Result<(String, usize), FormatError> {
let data_remaining = byte_stream.len() - bit_ndx/8;
fn format_bytes(
byte_stream: &ByteStream,
bit_ndx: usize,
max_byte_len: usize,
) -> Result<(String, usize), FormatError> {
let data_remaining = byte_stream.len() - bit_ndx / 8;
let width = if max_byte_len < data_remaining {
max_byte_len*8
}
else {
data_remaining*8
max_byte_len * 8
} else {
data_remaining * 8
};
let data = byte_stream.get_bytes(bit_ndx, width)?;
@ -138,14 +158,18 @@ impl Field {
Ok((print_bytes_as_array(&data), width))
}
fn format_data(&self, byte_stream: &ByteStream, bit_ndx: usize) -> Result<(String, usize), FormatError> {
fn format_data(
&self,
byte_stream: &ByteStream,
bit_ndx: usize,
) -> Result<(String, usize), FormatError> {
match self.field_type {
FieldType::UInt { bit_width } => Self::format_uint(byte_stream, bit_ndx, bit_width),
FieldType::Int { bit_width } => Self::format_int(byte_stream, bit_ndx, bit_width),
FieldType::Float => Self::format_float(byte_stream, bit_ndx),
FieldType::Double => Self::format_double(byte_stream, bit_ndx),
FieldType::String {max_len} => Self::format_string(byte_stream, bit_ndx, max_len),
FieldType::Bytes {max_len} => Self::format_bytes(byte_stream, bit_ndx, max_len)
FieldType::String { max_len } => Self::format_string(byte_stream, bit_ndx, max_len),
FieldType::Bytes { max_len } => Self::format_bytes(byte_stream, bit_ndx, max_len),
}
}
}
@ -183,12 +207,15 @@ impl Format {
#[cfg(test)]
mod tests {
use crate::formatter::format::{FieldType, Field};
use crate::byte_stream::ByteStream;
use crate::formatter::format::{Field, FieldType};
#[test]
fn test_format_int_4_bits() {
let field = Field {field_type: FieldType::Int {bit_width: 4,}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Int { bit_width: 4 },
name: "test".to_string(),
};
for i in 0i8..7i8 {
let mut byte_vec = Vec::new();
@ -206,7 +233,10 @@ mod tests {
#[test]
fn test_format_int_5_bits() {
let field = Field {field_type: FieldType::Int {bit_width: 5,}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Int { bit_width: 5 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0x1B]);
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
@ -215,8 +245,10 @@ mod tests {
#[test]
fn test_format_int_16_bits() {
let field = Field {field_type: FieldType::Int {bit_width: 16,}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Int { bit_width: 16 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0xFC, 0xA5]);
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
@ -226,10 +258,12 @@ mod tests {
#[test]
fn test_format_int_16_bits_not_aligned() {
let field = Field {field_type: FieldType::Int {bit_width: 16,}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Int { bit_width: 16 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0xC0, 0x5F, 0x0A]);
let byte_steam = ByteStream::from(vec![0xC0, 0x5F, 0x0A]);
let (output, _) = field.format_data(&byte_steam, 4).unwrap();
assert_eq!(output, "-23044")
@ -237,7 +271,10 @@ mod tests {
#[test]
fn test_format_float() {
let field = Field {field_type: FieldType::Float, name: "test".to_string()};
let field = Field {
field_type: FieldType::Float,
name: "test".to_string(),
};
let byte_steam = ByteStream::from(b"\x52\x58\xd2\xc3".to_vec());
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
@ -247,7 +284,10 @@ mod tests {
#[test]
fn test_format_double() {
let field = Field {field_type: FieldType::Double, name: "test".to_string()};
let field = Field {
field_type: FieldType::Double,
name: "test".to_string(),
};
let byte_steam = ByteStream::from(b"\xD7\xA3\x70\x3D\x0A\x4B\x7A\xC0".to_vec());
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
@ -257,7 +297,10 @@ mod tests {
#[test]
fn test_format_float_err() {
let field = Field {field_type: FieldType::Double, name: "test".to_string()};
let field = Field {
field_type: FieldType::Double,
name: "test".to_string(),
};
let byte_steam = ByteStream::from(b"\x3D\x70\xA3\xD7".to_vec());
@ -266,7 +309,10 @@ mod tests {
#[test]
fn test_format_string() {
let field = Field {field_type: FieldType::String {max_len: 16}, name: "test".to_string()};
let field = Field {
field_type: FieldType::String { max_len: 16 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(b"Hello World!\0".to_vec());
@ -277,7 +323,10 @@ mod tests {
#[test]
fn test_format_bytes() {
let field = Field {field_type: FieldType::Bytes {max_len: 2}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Bytes { max_len: 2 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0xDE, 0xAD, 0xBE, 0xEF]);
@ -288,7 +337,10 @@ mod tests {
#[test]
fn test_format_bytes_max_len_bigger_than_data() {
let field = Field {field_type: FieldType::Bytes {max_len: 64}, name: "test".to_string()};
let field = Field {
field_type: FieldType::Bytes { max_len: 64 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0xDE, 0xAD]);
@ -299,9 +351,14 @@ mod tests {
#[test]
fn test_ccsds_apid_issue() {
let field = Field {field_type: FieldType::UInt {bit_width: 11}, name: "test".to_string()};
let field = Field {
field_type: FieldType::UInt { bit_width: 11 },
name: "test".to_string(),
};
let byte_steam = ByteStream::from(vec![0xe0, 0xa1, 0xc0, 0x00, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05]);
let byte_steam = ByteStream::from(vec![
0xe0, 0xa1, 0xc0, 0x00, 0x05, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
]);
let (output, _) = field.format_data(&byte_steam, 5).unwrap();

View File

@ -1,5 +1,3 @@
pub fn print_bytes_as_array(data: &[u8]) -> String {
format!("{:?}", data)
}
}

View File

@ -35,6 +35,6 @@ fn main() {
match format.format_data(&data) {
Ok(data_str) => println!("{}", data_str),
Err(_) => println!("Error formatting data")
Err(_) => println!("Error formatting data"),
}
}

View File

@ -27,7 +27,7 @@ fn bytes_from_str_array(src: Vec<String>) -> Result<Vec<u8>, ByteArrayParseErr>
str::parse(element)
}
})
.map(|e| e.map_err( ByteArrayParseErr::from ))
.map(|e| e.map_err(ByteArrayParseErr::from))
.collect()
}