Get rid of constants
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::encoding::variable_byte_integer::{VariableByteInteger, VariableByteIntegerEncoder};
|
||||
use crate::packet::property::Property;
|
||||
use crate::utils::buffer_reader::{BinaryData, EncodedString, StringPair, TopicFilter};
|
||||
use core::str;
|
||||
use heapless::Vec;
|
||||
use crate::encoding::variable_byte_integer::{VariableByteInteger, VariableByteIntegerEncoder};
|
||||
use crate::packet::property::Property;
|
||||
|
||||
pub struct BuffWriter<'a> {
|
||||
buffer: &'a mut [u8],
|
||||
@@ -10,8 +10,7 @@ pub struct BuffWriter<'a> {
|
||||
}
|
||||
|
||||
impl<'a> BuffWriter<'a> {
|
||||
|
||||
pub fn insert<const LEN: usize>(& mut self, array: [u8; LEN]) {
|
||||
pub fn insert<const LEN: usize>(&mut self, array: [u8; LEN]) {
|
||||
let mut x: usize = 0;
|
||||
if LEN != 0 {
|
||||
loop {
|
||||
@@ -25,7 +24,7 @@ impl<'a> BuffWriter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_ref(& mut self, len: usize, array: &[u8]) {
|
||||
pub fn insert_ref(&mut self, len: usize, array: &[u8]) {
|
||||
let mut x: usize = 0;
|
||||
if len != 0 {
|
||||
loop {
|
||||
@@ -46,70 +45,70 @@ impl<'a> BuffWriter<'a> {
|
||||
};
|
||||
}
|
||||
|
||||
fn increment_position(& mut self, increment: usize) {
|
||||
fn increment_position(&mut self, increment: usize) {
|
||||
self.position = self.position + increment;
|
||||
}
|
||||
|
||||
pub fn write_u8(& mut self, byte: u8) {
|
||||
pub fn write_u8(&mut self, byte: u8) {
|
||||
self.buffer[self.position] = byte;
|
||||
self.increment_position(1);
|
||||
}
|
||||
|
||||
pub fn write_u16(& mut self, two_bytes: u16) {
|
||||
pub fn write_u16(&mut self, two_bytes: u16) {
|
||||
let bytes: [u8; 2] = two_bytes.to_be_bytes();
|
||||
self.insert::<2>(bytes);
|
||||
}
|
||||
|
||||
pub fn write_u32(& mut self, four_bytes: u32) {
|
||||
pub fn write_u32(&mut self, four_bytes: u32) {
|
||||
let bytes: [u8; 4] = four_bytes.to_be_bytes();
|
||||
self.insert::<4>(bytes);
|
||||
}
|
||||
|
||||
pub fn write_string_ref(& mut self, str: & EncodedString<'a>) {
|
||||
pub fn write_string_ref(&mut self, str: &EncodedString<'a>) {
|
||||
self.write_u16(str.len);
|
||||
let bytes = str.string.as_bytes();
|
||||
self.insert_ref(str.len as usize, bytes);
|
||||
}
|
||||
|
||||
pub fn write_string(& mut self, str: EncodedString<'a>) {
|
||||
pub fn write_string(&mut self, str: EncodedString<'a>) {
|
||||
self.write_u16(str.len);
|
||||
let bytes = str.string.as_bytes();
|
||||
self.insert_ref(str.len as usize, bytes);
|
||||
}
|
||||
|
||||
pub fn write_binary_ref(& mut self, bin: & BinaryData<'a>) {
|
||||
pub fn write_binary_ref(&mut self, bin: &BinaryData<'a>) {
|
||||
self.write_u16(bin.len);
|
||||
self.insert_ref(bin.len as usize, bin.bin);
|
||||
}
|
||||
|
||||
pub fn write_binary(& mut self, bin: BinaryData<'a>) {
|
||||
pub fn write_binary(&mut self, bin: BinaryData<'a>) {
|
||||
self.write_u16(bin.len);
|
||||
self.insert_ref(bin.len as usize, bin.bin);
|
||||
}
|
||||
|
||||
pub fn write_string_pair_ref(& mut self, str_pair: & StringPair<'a>) {
|
||||
pub fn write_string_pair_ref(&mut self, str_pair: &StringPair<'a>) {
|
||||
self.write_string_ref(&str_pair.name);
|
||||
self.write_string_ref(&str_pair.value);
|
||||
}
|
||||
|
||||
pub fn write_string_pair(& mut self, str_pair: StringPair<'a>) {
|
||||
pub fn write_string_pair(&mut self, str_pair: StringPair<'a>) {
|
||||
self.write_string(str_pair.name);
|
||||
self.write_string(str_pair.value);
|
||||
}
|
||||
|
||||
pub fn write_variable_byte_int(& mut self, int: u32) {
|
||||
pub fn write_variable_byte_int(&mut self, int: u32) {
|
||||
let x: VariableByteInteger = VariableByteIntegerEncoder::encode(int).unwrap();
|
||||
let len = VariableByteIntegerEncoder::len(x);
|
||||
self.insert_ref(len, &x);
|
||||
}
|
||||
|
||||
pub fn encode_property(& mut self, property: & Property<'a>) {
|
||||
pub fn encode_property(&mut self, property: &Property<'a>) {
|
||||
let x: u8 = property.into();
|
||||
self.write_u8(x);
|
||||
property.encode(self);
|
||||
}
|
||||
|
||||
pub fn encode_properties<const LEN: usize>(& mut self, properties: & Vec<Property<'a>, LEN>) {
|
||||
pub fn encode_properties<const LEN: usize>(&mut self, properties: &Vec<Property<'a>, LEN>) {
|
||||
let mut i = 0;
|
||||
let len = properties.len();
|
||||
if len != 0 {
|
||||
@@ -124,17 +123,22 @@ impl<'a> BuffWriter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_topic_filter_ref(& mut self, sub: bool, topic_filter: & TopicFilter<'a>) {
|
||||
fn encode_topic_filter_ref(&mut self, sub: bool, topic_filter: &TopicFilter<'a>) {
|
||||
self.write_string_ref(&topic_filter.filter);
|
||||
if sub {
|
||||
self.write_u8(topic_filter.sub_options)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_topic_filters_ref<const MAX: usize>(& mut self, sub: bool, len: usize, filters: & Vec<TopicFilter<'a>, MAX>) {
|
||||
pub fn encode_topic_filters_ref<const MAX: usize>(
|
||||
&mut self,
|
||||
sub: bool,
|
||||
len: usize,
|
||||
filters: &Vec<TopicFilter<'a>, MAX>,
|
||||
) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
let topic_filter: & TopicFilter<'a> = filters.get(i).unwrap();
|
||||
let topic_filter: &TopicFilter<'a> = filters.get(i).unwrap();
|
||||
self.encode_topic_filter_ref(sub, topic_filter);
|
||||
i = i + 1;
|
||||
if i == len {
|
||||
@@ -142,4 +146,4 @@ impl<'a> BuffWriter<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user