diff --git a/cangen/RustSynth.py b/cangen/RustSynth.py index 302cf1b..e57103b 100644 --- a/cangen/RustSynth.py +++ b/cangen/RustSynth.py @@ -16,7 +16,7 @@ def parse_messages(self, msgs: List[CANMsg]) -> Result: """ result = Result("", "") result.decode_data += RustSnippets.ignore_clippy - result.decode_data += RustSnippets.bitreader_impl + # result.decode_data += RustSnippets.bitreader_impl result.decode_data += RustSnippets.format_impl result.decode_data += RustSnippets.decode_data_import result.decode_data += RustSnippets.decode_mock @@ -139,19 +139,10 @@ def parse_decoders(self, field: CANPoint) -> str: Helper function that parses the decoders for a given CANUnit by applying the decoders to the data and casting the result to the final type of the CANUnit. """ - base = f"reader.read_bits({field.size})" - - # aware conversion from network to platform endianness - if field.endianness == "little": - base = f"(u{field.get_size_bits()}::from_be({base} as u{field.get_size_bits()}) as i{field.get_size_bits()})" - elif field.endianness == "big": - base = f"u32::from_le({base})" - else: - print("Invalid endianness on point!") - exit(1) - - # if field.signed or field.endianness == "little": - # base = f"({base} as i{field.get_size_bits()})" + size = field.size + if field.size < 8: + size = 8 + base = f"reader.read::({field.size}).unwrap()" return f"{base} as {field.final_type}" @@ -175,40 +166,40 @@ class RustSnippets: "#![allow(clippy::all)]\n" # Ignoring clippy for decode_data because it's autogenerated and has some unnecessary type casting to ensure correct types ) - bitreader_impl: str = (""" -/* Struct to decode a byte array bit by bit, sequentially */ -struct BitReader<'a> { - data: &'a [u8], - byte_pos: usize, - bit_pos: u8, -} - -impl<'a> BitReader<'a> { - fn new(data: &'a [u8]) -> Self { - Self { - data, - byte_pos: 0, - bit_pos: 0, - } - } - - fn read_bits(&mut self, num_bits: u8) -> u32 { - let mut value: u32 = 0; - for _ in 0..num_bits { - let bit = (self.data[self.byte_pos] >> (7 - self.bit_pos)) & 1; - value = (value << 1) | bit as u32; - - self.bit_pos += 1; - if self.bit_pos == 8 { - self.bit_pos = 0; - self.byte_pos += 1; - } - } - value - } -} -""" - ) +# bitreader_impl: str = (""" +# /* Struct to decode a byte array bit by bit, sequentially */ +# struct BitReader<'a> { +# data: &'a [u8], +# byte_pos: usize, +# bit_pos: u8, +# } + +# impl<'a> BitReader<'a> { +# fn new(data: &'a [u8]) -> Self { +# Self { +# data, +# byte_pos: 0, +# bit_pos: 0, +# } +# } + +# fn read_bits(&mut self, num_bits: u8) -> u32 { +# let mut value: u32 = 0; +# for _ in 0..num_bits { +# let bit = (self.data[self.byte_pos] >> (7 - self.bit_pos)) & 1; +# value = (value << 1) | bit as u32; + +# self.bit_pos += 1; +# if self.bit_pos == 8 { +# self.bit_pos = 0; +# self.byte_pos += 1; +# } +# } +# value +# } +# } +# """ +# ) format_impl = """ /** @@ -235,10 +226,13 @@ class RustSnippets: } }""" - bitreader_create = " let mut reader = BitReader::new(data);" + bitreader_create = " let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);" decode_data_import: str = ( - "use super::data::Data;\n" # Importing the Data struct and the FormatData and ProcessData traits + """use super::data::Data; + use std::io::{Read, Cursor}; + use bitstream_io::{BigEndian, BitReader, BitRead};\n + """ # Importing the Data struct and the FormatData and ProcessData traits ) decode_return_type: str = "Vec::" # The return type of any decode function