fmt + clippy
parent
bb6f331c75
commit
ef8918d517
|
@ -1,5 +1,5 @@
|
||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use num_bigint::BigUint;
|
use num_bigint::BigUint;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum ByteStreamError {
|
pub enum ByteStreamError {
|
||||||
|
@ -9,7 +9,7 @@ pub enum ByteStreamError {
|
||||||
impl Display for ByteStreamError {
|
impl Display for ByteStreamError {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
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>,
|
data: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ByteStream {
|
impl ByteStream {
|
||||||
pub fn get_bytes(&self, bit_ndx: usize, bit_count: usize) -> Result<Vec<u8>, ByteStreamError> {
|
pub fn get_bytes(&self, bit_ndx: usize, bit_count: usize) -> Result<Vec<u8>, ByteStreamError> {
|
||||||
let byte_ndx = bit_ndx / 8;
|
let byte_ndx = bit_ndx / 8;
|
||||||
let bits_before = (bit_ndx % 8) as u8;
|
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 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 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
|
byte_count + 1
|
||||||
} else {
|
} else {
|
||||||
byte_count
|
byte_count
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::byte_stream::{ByteStream, bit_mask, ByteStreamError};
|
use crate::byte_stream::{bit_mask, ByteStream, 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::formatter::printers::print_bytes_as_array;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum FormatError {
|
pub enum FormatError {
|
||||||
|
@ -32,7 +32,7 @@ impl Display for FormatError {
|
||||||
match self {
|
match self {
|
||||||
FormatError::ByteSteamError(e) => writeln!(f, "Byte stream error: {}", e),
|
FormatError::ByteSteamError(e) => writeln!(f, "Byte stream error: {}", e),
|
||||||
FormatError::NotSupported => write!(f, "Field type not supported"),
|
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 {
|
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)?;
|
let mut bytes = byte_stream.get_bytes(bit_ndx, bit_width)?;
|
||||||
|
|
||||||
if let Some(last_byte) = bytes.last_mut() {
|
if let Some(last_byte) = bytes.last_mut() {
|
||||||
|
@ -78,34 +82,47 @@ impl Field {
|
||||||
let big_int = BigInt::from_signed_bytes_le(&bytes);
|
let big_int = BigInt::from_signed_bytes_le(&bytes);
|
||||||
|
|
||||||
Ok((big_int.to_string(), bit_width))
|
Ok((big_int.to_string(), bit_width))
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
Err(ByteStreamError::OutOfRange.into())
|
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 bytes = byte_stream.get_bytes(bit_ndx, bit_width)?;
|
||||||
|
|
||||||
let big_int = BigUint::from_bytes_le(&bytes);
|
let big_int = BigUint::from_bytes_le(&bytes);
|
||||||
Ok((big_int.to_string(), bit_width))
|
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 bytes = byte_stream.get_bytes(bit_ndx, 32)?;
|
||||||
let mut cursor = Cursor::new(bytes);
|
let mut cursor = Cursor::new(bytes);
|
||||||
|
|
||||||
Ok((cursor.read_f32::<LittleEndian>().unwrap().to_string(), 4))
|
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 bytes = byte_stream.get_bytes(bit_ndx, 64)?;
|
||||||
let mut cursor = Cursor::new(bytes);
|
let mut cursor = Cursor::new(bytes);
|
||||||
|
|
||||||
Ok((cursor.read_f64::<LittleEndian>().unwrap().to_string(), 4))
|
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();
|
let mut string_bytes = Vec::new();
|
||||||
|
|
||||||
for _ in 0..max_byte_len {
|
for _ in 0..max_byte_len {
|
||||||
|
@ -123,14 +140,17 @@ impl Field {
|
||||||
Ok((String::from_utf8(string_bytes)?, byte_count))
|
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> {
|
fn format_bytes(
|
||||||
let data_remaining = byte_stream.len() - bit_ndx/8;
|
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 {
|
let width = if max_byte_len < data_remaining {
|
||||||
max_byte_len*8
|
max_byte_len * 8
|
||||||
}
|
} else {
|
||||||
else {
|
data_remaining * 8
|
||||||
data_remaining*8
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let data = byte_stream.get_bytes(bit_ndx, width)?;
|
let data = byte_stream.get_bytes(bit_ndx, width)?;
|
||||||
|
@ -138,14 +158,18 @@ impl Field {
|
||||||
Ok((print_bytes_as_array(&data), width))
|
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 {
|
match self.field_type {
|
||||||
FieldType::UInt { bit_width } => Self::format_uint(byte_stream, bit_ndx, bit_width),
|
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::Int { bit_width } => Self::format_int(byte_stream, bit_ndx, bit_width),
|
||||||
FieldType::Float => Self::format_float(byte_stream, bit_ndx),
|
FieldType::Float => Self::format_float(byte_stream, bit_ndx),
|
||||||
FieldType::Double => Self::format_double(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::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::Bytes { max_len } => Self::format_bytes(byte_stream, bit_ndx, max_len),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,12 +207,15 @@ impl Format {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::formatter::format::{FieldType, Field};
|
|
||||||
use crate::byte_stream::ByteStream;
|
use crate::byte_stream::ByteStream;
|
||||||
|
use crate::formatter::format::{Field, FieldType};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_int_4_bits() {
|
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 {
|
for i in 0i8..7i8 {
|
||||||
let mut byte_vec = Vec::new();
|
let mut byte_vec = Vec::new();
|
||||||
|
@ -206,7 +233,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_int_5_bits() {
|
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 byte_steam = ByteStream::from(vec![0x1B]);
|
||||||
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
||||||
|
|
||||||
|
@ -215,8 +245,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_int_16_bits() {
|
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 byte_steam = ByteStream::from(vec![0xFC, 0xA5]);
|
||||||
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
||||||
|
@ -226,8 +258,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_int_16_bits_not_aligned() {
|
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();
|
let (output, _) = field.format_data(&byte_steam, 4).unwrap();
|
||||||
|
@ -237,7 +271,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_float() {
|
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 byte_steam = ByteStream::from(b"\x52\x58\xd2\xc3".to_vec());
|
||||||
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
||||||
|
@ -247,7 +284,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_double() {
|
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 byte_steam = ByteStream::from(b"\xD7\xA3\x70\x3D\x0A\x4B\x7A\xC0".to_vec());
|
||||||
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
let (output, _) = field.format_data(&byte_steam, 0).unwrap();
|
||||||
|
@ -257,7 +297,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_float_err() {
|
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());
|
let byte_steam = ByteStream::from(b"\x3D\x70\xA3\xD7".to_vec());
|
||||||
|
|
||||||
|
@ -266,7 +309,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_string() {
|
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());
|
let byte_steam = ByteStream::from(b"Hello World!\0".to_vec());
|
||||||
|
|
||||||
|
@ -277,7 +323,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_bytes() {
|
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]);
|
let byte_steam = ByteStream::from(vec![0xDE, 0xAD, 0xBE, 0xEF]);
|
||||||
|
|
||||||
|
@ -288,7 +337,10 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_format_bytes_max_len_bigger_than_data() {
|
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]);
|
let byte_steam = ByteStream::from(vec![0xDE, 0xAD]);
|
||||||
|
|
||||||
|
@ -299,9 +351,14 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ccsds_apid_issue() {
|
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();
|
let (output, _) = field.format_data(&byte_steam, 5).unwrap();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
pub fn print_bytes_as_array(data: &[u8]) -> String {
|
pub fn print_bytes_as_array(data: &[u8]) -> String {
|
||||||
format!("{:?}", data)
|
format!("{:?}", data)
|
||||||
}
|
}
|
|
@ -35,6 +35,6 @@ fn main() {
|
||||||
|
|
||||||
match format.format_data(&data) {
|
match format.format_data(&data) {
|
||||||
Ok(data_str) => println!("{}", data_str),
|
Ok(data_str) => println!("{}", data_str),
|
||||||
Err(_) => println!("Error formatting data")
|
Err(_) => println!("Error formatting data"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ fn bytes_from_str_array(src: Vec<String>) -> Result<Vec<u8>, ByteArrayParseErr>
|
||||||
str::parse(element)
|
str::parse(element)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|e| e.map_err( ByteArrayParseErr::from ))
|
.map(|e| e.map_err(ByteArrayParseErr::from))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue